ユニークビジョンプログラミングコンテスト2024 夏(AtCoder Beginner Contest 359)
G
Sum of Tree Distance
AtCoder
⌀ 2099
原题
翻译中…

Problem Statement

翻译中…

You are given a tree with $N$ vertices. The $i$-th edge connects vertices $u_i$ and $v_i$ bidirectionally.

翻译中…

Additionally, you are given an integer sequence $A=(A_1,\ldots,A_N)$.

翻译中…

Here, define $f(i,j)$ as follows:

翻译中…
  • If $A_i = A_j$, then $f(i,j)$ is the minimum number of edges you need to traverse to move from vertex $i$ to vertex $j$. If $A_i \neq A_j$, then $f(i,j) = 0$.
翻译中…

Calculate the value of the following expression:

翻译中…

$\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(i,j)$.

翻译中…

Constraints

翻译中…
  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq u_i, v_i \leq N$
  • $1 \leq A_i \leq N$
  • The input graph is a tree.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N
u_1 v_1
\vdots
u_{N-1} v_{N-1}
A_1 A_2 \ldots A_N

翻译中…

Output

翻译中…

Print the answer.

翻译中…

样例

Input #1
4
3 4
4 2
1 2
2 1 1 2
Output #1
4
$f(1,4)=2, f(2,3)=2$. For all other $i, j\ (1 \leq i < j \leq N)$, we have $f(i,j)=0$, so the answer is $2+2=4$.
Input #2
8
8 6
3 8
1 4
7 8
4 5
3 4
8 2
1 2 2 2 3 1 1 3
Output #2
19
题目信息

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。