luogu#P4845. LJJ 爱数树

LJJ 爱数树

Problem Description

Eden accidentally obtained a treasure map. Notably, it is an undirected connected graph with nn nodes and n1n-1 edges of length 11, i.e., a tree. Each node is labeled with the amount of treasure at that location, wiw_i.

However, there is too much treasure to move all at once. Eden spent all his savings to buy kk cameras. Each camera has an observation range of rr (we define the distance between any two points on the tree as the length of their shortest path). To ensure that the treasure is not taken by others as much as possible, Eden will place cameras at at most kk nodes on the tree (so all nodes whose distance to that node is r\le r can be observed).

Eden wants to maximize the sum of treasure amounts (i.e., wiw_i) of the nodes that can be observed, so he gave this problem to LJJ, who loves counting trees. He asked LJJ to enumerate all possible camera placements to find the best plan.

Halfway through counting, LJJ realized there were far too many plans, so he threw the problem to you. Please output the maximum possible sum of wiw_i over the nodes that can be observed.

Input Format

This problem contains multiple test cases.

Line 11: a positive integer TT, indicating there are TT test cases.

For each test case:
Line 11: three positive integers n,k,rn, k, r separated by spaces.
Line 22: nn positive integers separated by spaces; the ii-th number denotes wiw_i.
Lines 33 to n+1n+1: each line contains two integers x,yx, y, indicating there is an edge between xx and yy.

Output Format

For each test case, output one integer per line, indicating the maximum possible sum of wiw_i of the nodes that can be observed.

2
9 1 1
2 2 2 2 2 3 4 3 4
1 2
1 3
1 4
1 5
2 6
6 7
3 8
8 9
9 2 1
2 2 2 2 2 3 4 3 4
1 2
1 3
1 4
1 5
2 6
6 7
3 8
8 9
10
18
3
10 1 1
2 8 9 6 1 9 9 2 8 9 
1 2
2 3
2 4
4 5
3 6
2 7
7 8
4 9
7 10
10 2 1
2 8 9 6 1 9 9 2 8 9 
1 2
2 3
2 4
4 5
3 6
2 7
7 8
4 9
7 10
10 3 1
2 8 9 6 1 9 9 2 8 9 
1 2
2 3
2 4
4 5
3 6
2 7
7 8
4 9
7 10
34
46
61

Hint

For 10%10\% of the testdata, it is guaranteed that n20n\le20, r20r\le 20, wi20w_i\le20.

For 40%40\% of the testdata, it is guaranteed that n50n\le50, r50r\le 50, wi50w_i\le50.

For another 20%20\% of the testdata, it is guaranteed that k2k\le2.

For another 10%10\% of the testdata, it is guaranteed that the given tree is a chain.

For 100%100\% of the testdata, it is guaranteed that 1kn1031\le k \le n\le10^3, 1r1031\le r\le 10^3, 1wi1061\le w_i\le10^6, 1T51\le T\le 5, 1x,yn1\le x,y\le n.

For the official editorial, please see https://www.cnblogs.com/Blog-of-Eden/p/9367521.html.

Translated by ChatGPT 5