#P5588. 小猪佩奇爬树
小猪佩奇爬树
Description
Peppa and George are climbing a tree.
Given a tree with nodes, the color of node is , and it is guaranteed that .
For each , output how many pairs satisfy , and the path from to passes through all nodes whose color is . For other nodes whose color is not , the path may pass through them or not.
A tree path is defined as a sequence such that , , and for all , there is an edge in , and there are no repeated elements in . It can be proven that for any pair , the tree path is unique.
Input Format
The first line contains one positive integer .
The second line contains positive integers, where the -th integer is .
The next lines each contain two positive integers , indicating that there is an edge in .
Output Format
Output lines, each containing one positive integer. On the -th line, output the number of paths that contain all nodes whose color is .
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 , the pairs satisfy the condition.
For color , the pairs satisfy the condition.
For color , the pairs satisfy the condition.
For color , since there is no node with color in the graph, all pairs satisfy the condition.
Constraints
For of the testdata, .
For of the testdata, .
For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号