#P7581. 「RdOI R2」路径权值(distance)
「RdOI R2」路径权值(distance)
Description
You are given a rooted tree with nodes and weighted edges. The root is node . Define the -son of a node as all nodes in the subtree of whose depth (number of edges on the path) is exactly greater than that of .
There are queries. For each query, compute the sum of distances between every pair of nodes among the -sons of a node . You need to output the result modulo .
Input Format
The first line contains two integers .
The next lines each contain three integers , indicating that there is an edge of weight between and .
The next lines each contain two integers , indicating a query.
Output Format
For each query, output one line containing the answer.
5 3
1 2 2
1 3 1
2 4 1
2 5 2
1 1
1 2
2 1
3
3
3
10 5
1 2 1
1 3 3
2 4 2
2 5 2
3 6 3
3 7 1
5 8 2
6 9 1
6 10 3
1 2
3 2
6 1
1 3
2 2
40
4
4
30
0
Hint
Sample Explanation
Below is the tree in the sample.

Sample Explanation
Below is the tree in the sample.

Constraints
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, , , , . It is guaranteed that the given graph is a tree.
Translated by ChatGPT 5
京公网安备 11011102002149号