Colorful Trees
Time limit1sMemory limit1024 MB
For each edge of a vertex-colored tree, count the same-color vertex pairs whose connecting path passes through that edge.
Problem
You are given a tree whose vertices are colored. For each edge, count the pairs of vertices with the same color whose path between them contains that edge. Since the graph is a tree, every pair of vertices is joined by exactly one path.
Input
The first line contains a single integer (), the number of vertices. The vertices are numbered from 1 to .
Each of the next lines contains a single integer (), the color of each vertex in order.
Each of the next lines contains two integers and (), denoting an undirected edge between vertices and .
Output
Output lines. Each line contains one integer, the answer for the edge at the corresponding position in the input, in input order.