#abc298h. Ex - Sum of Min of Length
Ex - Sum of Min of Length
Score : points
问题描述
你被给定一棵包含 个顶点的树。顶点按从 到 编号,第 条边连接顶点 和顶点 。
令 表示在该树中顶点 和顶点 之间的距离。这里,顶点 和顶点 之间的距离是指从顶点 到顶点 的最短路径上的边数。
按照顺序回答 个查询。第 个查询如下:
- 给定整数 和 ,求 $\displaystyle\sum_{j = 1}^{N} \min(d(j, L_i), d(j, R_i))$。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a tree with vertices. The vertices are numbered to , and the -th edge connects vertex and vertex .
Let denote the distance between vertex and in this tree. Here, the distance between vertex and is the number of edges on the shortest path from vertex to .
Answer queries in order. The -th query is as follows.
- You are given integers and . Find $\displaystyle\sum_{j = 1}^{N} \min(d(j, L_i), d(j, R_i))$.
Constraints
- The given graph is a tree.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer to the -th query.
Sample Input 1
5
3 4
4 5
2 5
1 5
3
4 1
1 2
5 3
Sample Output 1
4
6
3
Let us explain the first query.
Since and , we have .
Since and , we have .
Since and , we have .
Since and , we have .
Since and , we have .
, so you should print .
Sample Input 2
8
4 2
4 1
5 6
6 1
7 6
8 1
3 7
7
8 4
4 4
7 2
4 4
5 3
4 4
6 1
Sample Output 2
14
16
10
16
14
16
8
update @ 2024/3/10 12:23:30