#P1805E. There Should Be a Lot of Maximums

    ID: 8358 Type: RemoteJudge 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>data structuresdfs and similardsugraphstreestwo pointers

There Should Be a Lot of Maximums

No submission language available for this problem.

Description

You are given a tree (a connected graph without cycles). Each vertex of the tree contains an integer. Let's define the MAD\mathrm{MAD} (maximum double) parameter of the tree as the maximum integer that occurs in the vertices of the tree at least 22 times. If no number occurs in the tree more than once, then we assume MAD=0\mathrm{MAD}=0.

Note that if you remove an edge from the tree, it splits into two trees. Let's compute the MAD\mathrm{MAD} parameters of the two trees and take the maximum of the two values. Let the result be the value of the deleted edge.

For each edge, find its value. Note that we don't actually delete any edges from the tree, the values are to be found independently.

The first line contains one integer nn (2n1052 \le n \le 10^5) — the number of vertices in the tree.

Each of the next n1n - 1 lines contains two integers uu and vv (1u,vn1 \le u, v \le n) — the ends of an edge of the tree. It's guaranteed that the given edges form a valid tree.

The last line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9) — the numbers in the vertices.

For each edge in the input order, print one number — the maximum of the MAD\mathrm{MAD} parameters of the two trees obtained after removing the given edge from the initial tree.

Input

The first line contains one integer nn (2n1052 \le n \le 10^5) — the number of vertices in the tree.

Each of the next n1n - 1 lines contains two integers uu and vv (1u,vn1 \le u, v \le n) — the ends of an edge of the tree. It's guaranteed that the given edges form a valid tree.

The last line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9) — the numbers in the vertices.

Output

For each edge in the input order, print one number — the maximum of the MAD\mathrm{MAD} parameters of the two trees obtained after removing the given edge from the initial tree.

Sample Input 1

5
1 2
2 3
2 4
1 5
2 1 3 2 1

Sample Output 1

0
2
1
2

Sample Input 2

6
1 2
1 3
1 4
4 5
4 6
1 2 3 1 4 5

Sample Output 2

1
1
0
1
1

Note

In the first example, after removing edge (1,2)(1, 2) no number repeats 22 times in any of the resulting subtrees, so the answer is max(0,0)=0\max(0, 0)=0.

After removing edge (2,3)(2, 3), in the bigger subtree, 11 is repeated twice, and 22 is repeated twice, so the MAD\mathrm{MAD} of this tree is 22.

After removing edge (2,4)(2, 4), in the bigger subtree, only the number 11 is repeated, and in the second subtree, only one number appears, so the answer is 11.

In the second example, if edge 141 \leftrightarrow 4 is not removed, then one of the subtrees will have two 11, so the answer — 11. And if edge 141 \leftrightarrow 4 is deleted, both subtrees have no repeating values, so the answer is 00.