#P16219. [ECUSTPC 2025] 林间小径
[ECUSTPC 2025] 林间小径
Problem Description
Maddy got lost in the forest..., and she saw a strange tree.
In front of her is a tree of size , which is an undirected, acyclic, connected graph with vertices and edges.
However, when Maddy was not paying attention, the shape of the tree changed: a newly grown branch connected two different non-adjacent vertices! That is, the tree became an undirected connected graph with vertices and edges.
To test you, Maddy gives the original shape of the tree and the values for all vertices, where is the sum of distances from vertex to every other vertex in the new graph , i.e., .
You need to tell Maddy which two vertices are connected by the newly grown branch.
Input Format
The first line contains an integer (), indicating the number of test cases.
For each test case, the first line contains an integer (), indicating the number of vertices in the graph.
Then follow lines, each containing two integers and (), indicating that there is an edge between and in the tree .
Then one line contains integers , representing for each vertex the sum of distances to all other vertices in .
It is guaranteed that across all test cases, . Also, in each test case the input graph forms a tree, and the two endpoints of the hidden edge are distinct and non-adjacent in the original graph.
Output Format
For each test case, output one line with two integers and , indicating the two vertices connected by the newly grown branch.
If there are multiple valid answers, you may output any one of them. (For example, if is valid, then is also valid.)
1
7
1 2
1 3
2 4
2 5
3 6
3 7
10 10 10 11 15 15 11
4 7
Hint
Explanation for Sample 1
The following figure shows the shape of .
:::align{center}
:::
Hint
The distance in the graph is defined as the minimum number of edges among all paths starting from and ending at .
Translated by ChatGPT 5