Logo
(追記) (追記ここまで)

32499번 - Colorful Trees 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 (추가 시간 없음) 1024 MB91484556.250%

문제

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 $n$ (2ドル≤n≤10^5$), which is the number of nodes in the tree. The nodes are numbered from 1ドル$ to $n$.

Each of the next $n$ lines contains a single integer $c$ (1ドル≤c≤n$). These are the colors of the nodes, in order.

Each of the next $n-1$ lines contains two integers $a$ and $b$ (1ドル≤a<b≤n$), denoting an undirected edge from node $a$ to node $b$.

출력

Output $n-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.

제한

예제 입력 1

6
3
1
2
1
2
2
2 6
4 5
1 4
3 4
1 2

예제 출력 1

2
2
3
2
3

예제 입력 2

4
2
2
2
2
3 4
2 4
1 2

예제 출력 2

3
4
3

힌트

출처

ICPC > Regionals > North America > North America Qualification Contest > ICPC North America Qualifier 2024 D번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /