#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 nn, which is an undirected, acyclic, connected graph TT with nn vertices and n1n-1 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 TT became an undirected connected graph GG' with nn vertices and nn edges.
To test you, Maddy gives the original shape of the tree and the values Di\sum D_i for all vertices, where Di\sum D_i is the sum of distances from vertex ii to every other vertex in the new graph GG', i.e., Di=jVDG(i,j)\sum D_i = \sum_{j \in V} D_{G'}(i, j).
You need to tell Maddy which two vertices are connected by the newly grown branch.

Input Format

The first line contains an integer TT (1T1051 \le T \le 10^5), indicating the number of test cases.
For each test case, the first line contains an integer nn (3n1053 \le n \le 10^5), indicating the number of vertices in the graph.
Then follow n1n-1 lines, each containing two integers uu and vv (1u,vn,uv1 \le u, v \le n, u \ne v), indicating that there is an edge between uu and vv in the tree TT.
Then one line contains nn integers D1,D2,,Dn\sum D_1, \sum D_2, \dots, \sum D_n, representing for each vertex the sum of distances to all other vertices in GG'.
It is guaranteed that across all test cases, n3×105\sum n \le 3 \times 10^5. 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 xx and yy, 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 1 21\ 2 is valid, then 2 12\ 1 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 GG'.

:::align{center} :::

Hint

The distance Di,jD_{i,j} in the graph is defined as the minimum number of edges among all paths starting from ii and ending at jj.

Translated by ChatGPT 5