#P15265. [USACO26JAN2] Dynamic Instability P
[USACO26JAN2] Dynamic Instability P
Description
Farmer Nhoj has trapped Bessie on a rooted tree with () nodes, where node is the root. Scared and alone, Bessie makes the following move each second:
- If Bessie's current node has no children, then she will move to a random ancestor of the current node (excluding the node itself).
- Otherwise, Bessie will move to a random child of the current node.
Initially, Bessie is at node , and her only way out is the exit located at node (). For () independent queries of and , compute the expected number of seconds it would take Bessie to reach node for the first time if she started at node , modulo .
Input Format
The first line contains and .
The next line contains integers describing the tree (). For each , there is an edge between nodes and .
Each of the next lines contains integers and representing the nodes for that query.
Output Format
For each query, output the expected number of seconds for Bessie to reach node for the first time starting at node , modulo .
5 5
1 2 2 1
1 1
2 1
3 1
4 1
5 1
0
4
3
3
1
5 5
1 2 2 1
1 1
1 2
1 3
1 4
1 5
0
3
500000011
500000011
6
13 10
1 2 2 4 3 1 5 6 4 7 8 10
1 12
10 6
5 12
1 13
13 10
6 4
7 12
3 1
12 8
2 1
166666700
21
2
166666701
500000023
18
166666704
750000018
800000021
500000018
Hint
Sample 1 Explanation
- In the st query, the expected time to reach node from itself is .
- In the rd query, after second, Bessie will be at node with probability and at node with probability . Since the expected time to reach node from node is , the expected time for Bessie to reach node starting at node is .
Sample 2 Explanation
In the rd query, the expected time to reach node from node is .
SCORING:
- Inputs 4-8: For all queries, .
- Inputs 9-13: For all queries, .
- Inputs 14-18: For each , is uniformly randomly chosen from the range .
- Inputs 19-23: No additional constraints.
京公网安备 11011102002149号