#P6778. [Ynoi2009] rpdq
[Ynoi2009] rpdq
Problem Description
Given an undirected tree with nodes and weighted edges. Each node has a label, and the labels form a permutation of .
There are queries. Each query gives . You need to find the sum of distances on the tree over all pairs of node labels that satisfy . The distance between two nodes is defined as the sum of edge weights along the simple path connecting them.
Input Format
The first line contains two space-separated integers and .
The next lines each contain three space-separated integers , meaning there is an edge between and with weight .
The next lines each contain two space-separated integers , representing one query.
Output Format
Output lines. Each line is the answer for the corresponding query, taken modulo .
6 6
2 1 1
5 1 1
3 1 3
4 5 1
6 3 3
2 5
1 5
1 4
3 6
2 6
1 1
19
26
18
28
44
0
Hint
Idea: nzhtl1477, Solution: nzhtl1477, Code: zx2003, Data: nzhtl1477.
For of the testdata, , and all values are integers.
Translated by ChatGPT 5