luogu#P4845. LJJ 爱数树
LJJ 爱数树
Problem Description
Eden accidentally obtained a treasure map. Notably, it is an undirected connected graph with nodes and edges of length , i.e., a tree. Each node is labeled with the amount of treasure at that location, .
However, there is too much treasure to move all at once. Eden spent all his savings to buy cameras. Each camera has an observation range of (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 nodes on the tree (so all nodes whose distance to that node is can be observed).
Eden wants to maximize the sum of treasure amounts (i.e., ) 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 over the nodes that can be observed.
Input Format
This problem contains multiple test cases.
Line : a positive integer , indicating there are test cases.
For each test case:
Line : three positive integers separated by spaces.
Line : positive integers separated by spaces; the -th number denotes .
Lines to : each line contains two integers , indicating there is an edge between and .
Output Format
For each test case, output one integer per line, indicating the maximum possible sum of 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 of the testdata, it is guaranteed that , , .
For of the testdata, it is guaranteed that , , .
For another of the testdata, it is guaranteed that .
For another of the testdata, it is guaranteed that the given tree is a chain.
For of the testdata, it is guaranteed that , , , , .
For the official editorial, please see https://www.cnblogs.com/Blog-of-Eden/p/9367521.html.
Translated by ChatGPT 5