atcoder#ABC298H. [ABC298Ex] Sum of Min of Length
[ABC298Ex] Sum of Min of Length
Score : points
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.
5
3 4
4 5
2 5
1 5
3
4 1
1 2
5 3
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 .
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
14
16
10
16
14
16
8