#5414. Problem 3. Dynamic Instability
Problem 3. Dynamic Instability
Problem 3. Dynamic Instability
USACO 2026 Second Contest, Platinum
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 (input arrives from the terminal / stdin):
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 (print output to the terminal / stdout):
For each query, output the expected number of seconds for Bessie to reach node for the first time starting at node , modulo .
SAMPLE INPUT:
5 5 1 2 2 1 1 1 2 1 3 1 4 1 5 1
SAMPLE OUTPUT:
0 4 3 3 1
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 INPUT:
5 5 1 2 2 1 1 1 1 2 1 3 1 4 1 5
SAMPLE OUTPUT:
0 3 500000011 500000011 6
In the rd query, the expected time to reach node from node is .
SAMPLE INPUT:
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
SAMPLE OUTPUT:
166666700 21 2 166666701 500000023 18 166666704 750000018 800000021 500000018
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.
Problem credits: Avnith Vijayram