#16053. [ICPC 2021 Nanjing R] Paimon's Tree
[ICPC 2021 Nanjing R] Paimon's Tree
[ICPC 2021 Nanjing R] Paimon's Tree
题目描述
Paimon has found a tree with $(n + 1)$ initially white vertices in her left pocket and decides to play with it. A tree with $(n + 1)$ nodes is an undirected connected graph with $n$ edges.Paimon will give you an integer sequence of length . We first need to select a vertex in the tree and paint it black. Then we perform the following operation times.
During the -th operation, we select a white vertex which is directly connected with a black vertex by an edge, set the weight of that edge to and also paint in black. After these operations we get a tree whose edges are all weighted.
What's the maximum length of the diameter of the weighted tree if we select the vertices optimally? The diameter of a weighted tree is the longest simple path in that tree. The length of a simple path is the sum of the weights of all edges in that path.
输入格式
There are multiple test cases. The first line of the input contains an integer $T$ ($1 \le T \le 5 \times 10^3$) indicating the number of test cases. For each test case:The first line contains an integer () indicating the length of the sequence.
The second line contains integers () indicating the sequence.
For the following lines, the -th line contains two integers and () indicating that there is an edge connecting vertex and in the tree.
It's guaranteed that there is at most test cases satisfying .
输出格式
For each test case output one line containing one integer indicating the maximum length of the diameter of the tree.2
5
1 7 3 5 4
1 3
2 3
3 4
4 5
4 6
1
1000000000
1 2
16
1000000000
提示
For the first sample test case, we select the vertices in the order of $1, 3, 4, 5, 2, 6$, resulting in the weighted tree of the following image. It's obvious that the longest simple path is of length $16$.
标签: P9846|动态规划 DP|2021|Special Judge|O2优化|ICPC|南京