#abc359g. G - Sum of Tree Distance
G - Sum of Tree Distance
Score : points
问题陈述
给定一个有 个顶点的树。第 条边双向连接顶点 和 。
此外,给定一个整数序列 。
这里,定义 如下:
- 如果 ,则 是从顶点 移动到顶点 需要经过的最少边数。如果 ,则 。
计算以下表达式的值:
$\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(i,j)$。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a tree with vertices. The -th edge connects vertices and bidirectionally.
Additionally, you are given an integer sequence .
Here, define as follows:
- If , then is the minimum number of edges you need to traverse to move from vertex to vertex . If , then .
Calculate the value of the following expression:
$\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(i,j)$.
Constraints
- The input graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
4
3 4
4 2
1 2
2 1 1 2
Sample Output 1
4
. For all other , we have , so the answer is .
Sample 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
Sample Output 2
19