#ABC163F. [ABC163F] path pass i

[ABC163F] path pass i

Score : 600600 points

Problem Statement

We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aia_i and bib_i. Additionally, each vertex is painted in a color, and the color of Vertex ii is cic_i. Here, the color of each vertex is represented by an integer between 11 and NN (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.

For each k=1,2,...,Nk=1, 2, ..., N, solve the following problem:

  • Find the number of simple paths that visit a vertex painted in the color kk one or more times.

Note: The simple paths from Vertex uu to vv and from vv to uu are not distinguished.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1ciN1 \leq c_i \leq N
  • 1ai,biN1 \leq a_i,b_i \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

c1c_1 c2c_2 ...... cNc_N

a1a_1 b1b_1

::

aN1a_{N-1} bN1b_{N-1}

Output

Print the answers for k=1,2,...,Nk = 1, 2, ..., N in order, each in its own line.

3
1 2 1
1 2
2 3
5
4
0

Let Pi,jP_{i,j} denote the simple path connecting Vertex ii and jj.

There are 55 simple paths that visit a vertex painted in the color 11 one or more times: P1,1,P_{1,1}\,,\, P1,2,P_{1,2}\,,\, P1,3,P_{1,3}\,,\, P2,3,P_{2,3}\,,\, P3,3P_{3,3}

There are 44 simple paths that visit a vertex painted in the color 22 one or more times: P1,2,P_{1,2}\,,\, P1,3,P_{1,3}\,,\, P2,2,P_{2,2}\,,\, P2,3P_{2,3}

There are no simple paths that visit a vertex painted in the color 33 one or more times.

1
1
1
2
1 2
1 2
2
2
5
1 2 3 4 5
1 2
2 3
3 4
3 5
5
8
10
5
5
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
18
15
0
14
23
0
23
0