Colorful Trees

시간 제한1초메모리 제한1024 MB

요약
색이 칠해진 트리에서 각 간선마다 그 간선을 지나는 경로를 가진 같은 색 정점 쌍의 개수를 구한다.
난이도

보통10점 중 6점

유형
트리, DFS, 조합론, 누적 합
정답자
아직 제출이 없습니다

문제

Given a tree with colored vertices, for each edge, how many pairs of vertices with the same color have that edge on the path between them? Note that since it’s a tree, each pair of nodes has exactly one path between them.

입력

The first line of input contains a single integer nn (2≤n≤1052≤n≤10^5), which is the number of nodes in the tree. The nodes are numbered from 11 to nn.

Each of the next nn lines contains a single integer cc (1≤c≤n1≤c≤n). These are the colors of the nodes, in order.

Each of the next n−1n-1 lines contains two integers aa and bb (1≤a\<b≤n1≤a\<b≤n), denoting an undirected edge from node aa to node bb.

출력

Output n−1n-1 lines. On each line, output a single integer, which is the number of pairs of vertices with the same color that have that edge on the path between them. Output these answers for the edges in the order that they appear in the input.

예제2

  1. 예제 1

    입력
    6
    3
    1
    2
    1
    2
    2
    2 6
    4 5
    1 4
    3 4
    1 2
    
    예상 출력
    2
    2
    3
    2
    3
    
  2. 예제 2

    입력
    4
    2
    2
    2
    2
    3 4
    2 4
    1 2
    
    예상 출력
    3
    4
    3