#P10879. 「KDOI-07」对树链剖分的爱
「KDOI-07」对树链剖分的爱
Background
The person downstairs is right, but in NOI 2024 D2T2, sszcdjr used a clever method to defeat my brute-force heavy-light decomposition.
The person upstairs is right, but heavy-light decomposition got me to , so I made this problem to show my love for heavy-light decomposition.
Problem Description
You are given a rooted tree with nodes, rooted at . For each node , its parent is chosen uniformly at random from . Each edge of the tree has an edge weight, initially .
Now there are operations. The -th operation adds to the weights of all edges on the path from . After all operations, for all , compute the expected value of the weight on edge , modulo .
Input Format
The first line contains one positive integer .
In the next lines, the -th line contains two positive integers .
The next line contains one positive integer .
In the next lines, the -th line contains three positive integers .
Output Format
Output one line with positive integers. The -th integer denotes the expected edge weight of edge , modulo .
3
1 1
1 2
1
1 3 2
1 2
5
1 1
1 2
3 3
2 4
9
2 5 497855355
1 5 840823564
3 1 295265328
2 3 457999227
4 4 235621825
2 1 86836399
5 2 800390742
5 3 869167938
2 4 269250165
405260353 409046983 606499796 13504540
Hint
Sample Explanation 1
There are possible cases for the parents of all nodes:
-
. In this case, the edge weights on are , respectively.
-
. In this case, the edge weights on are , respectively.
So the expected edge weight of is , and the expected edge weight of is .
Constraints
This problem uses bundled testdata.
| Score | |||
|---|---|---|---|
For all testdata, it is guaranteed that , , , and .
Translated by ChatGPT 5