13777 - DS_2022_HW6_Advanced_Graph   

Description

There is a tree consisting of n nodes, in which the root is numbered 1. The nodes are numbered 1, 2, …, n.

Each node has a lucky value v, and the range of the lucky value is in [1-10^9]. The lucky value is NOT distinct.

For example, below is a tree, where each number in the circle indicates the node ID, and the number in the blue square denotes the lucky value, e.g., node 1 has lucky value 2, and node 2 has lucky value 3.


 
For node 1, its subtree is the whole tree. All the distinct lucky values in node 1’s subtree are {1, 2, 3}. 
For node 3, its subtree contains nodes {3,4,5} (red dashed box below). All the distinct lucky values are {1, 2}.


 

We would like you to find out for each node i, the number of distinct lucky values in its subtree.

Input

The first input line contains an integer n: the number of nodes. The nodes are numbered 1, 2, …, n.
 
The next line consists of n integers v1, v2, …, vn: the lucky value of each node.
 
Then, there are n−1 lines describing the edges. Each line contains two integers a and b: there is an edge between nodes a and b.
 
⦁ 1 ≤ n ≤ 200000
 
⦁ 1≤a,b≤n
 
⦁ 1≤vi≤10^9

Output

Print n integers: for integer i, print the number of distinct lucky values in node i's subtree. Every integer needs space behind it.

Please print '\n' at the end of output. 

Sample Input  Download

Sample Output  Download

Tags




Discuss