5324: ABC163——Task F:path pass i
Description
We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and bibi. Additionally, each vertex is painted in a color, and the color of Vertex ii is cici. 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.
Input
N c1 c2 ... cN a1 b1 . . aN bN
Output
Sample 1 Input
3
1 2 1
1 2
2 3
Sample 1 Output
5
4
0