#P5588. 小猪佩奇爬树

小猪佩奇爬树

Description

Peppa and George are climbing a tree.

Given a tree T(V,E)T(V,E) with nn nodes, the color of node ii is wiw_i, and it is guaranteed that 1win1 \leq w_i \leq n.

For each 1in1 \leq i \leq n, output how many pairs (u,v)(u,v) satisfy u<vu<v, and the path from uu to vv passes through all nodes whose color is ii. For other nodes whose color is not ii, the path may pass through them or not.

A tree path (u,v)(u,v) is defined as a sequence {f}\{f\} such that f1=uf_1=u, ff=vf_{|f|}=v, and for all 1i<f1 \leq i < |f|, there is an edge (fi,fi+1)(f_i,f_{i+1}) in TT, and there are no repeated elements in {f}\{f\}. It can be proven that for any pair (u,v)(u,v), the tree path is unique.

Input Format

The first line contains one positive integer nn.

The second line contains nn positive integers, where the ii-th integer is wiw_i.

The next n1n-1 lines each contain two positive integers u,vu,v, indicating that there is an edge (u,v)(u,v) in TT.

Output Format

Output nn lines, each containing one positive integer. On the ii-th line, output the number of paths that contain all nodes whose color is ii.

4
1 2 2 3
1 2
2 3
3 4
3
4
3
6
10
9 7 4 2 3 4 4 5 8 5
2 1
3 2
4 2
5 2
6 4
7 4
8 1
9 4
10 4
45
35
9
0
1
45
34
9
17
45

Hint

For the first sample.

For color 11, the pairs (1,2),(1,3),(1,4)(1,2),(1,3),(1,4) satisfy the condition.

For color 22, the pairs (1,3),(1,4),(2,3),(2,4)(1,3),(1,4),(2,3),(2,4) satisfy the condition.

For color 33, the pairs (1,4),(2,4),(3,4)(1,4),(2,4),(3,4) satisfy the condition.

For color 44, since there is no node with color 44 in the graph, all pairs satisfy the condition.

Constraints

For 40%40\% of the testdata, n102n \leq 10^2.

For 60%60\% of the testdata, n103n \leq 10^3.

For 100%100\% of the testdata, n106n \leq 10^6.

Translated by ChatGPT 5