#P10053. [CCO 2022] Bi-ing Lottery Treekets
[CCO 2022] Bi-ing Lottery Treekets
Problem Description
In a parallel universe, everyone got a perfect score on CCO. Therefore, Troy needs to use a lottery to choose the winner. Each contestant will pick some numbers to create a ticket. A ticket is an array of size , indexed from to , where each element is a number from to .
The winning ticket is determined by dropping balls (numbered from to ) into a rooted binary tree in a random order. This tree has nodes (numbered from to ), and node is the root.
Each ball has a designated drop node, where it will be dropped. When a ball is dropped into an empty node, or enters an empty node, one of the following three situations happens:
- If all children of the current node are occupied by balls (or if a node has no children), then the current ball will stay at the current node. That is, it remains there and will not move anymore.
- If the current node has exactly one empty child, then the current ball will move to that child.
- If the current node has two empty children, and if the current ball was just dropped, it may move left or right. Otherwise, it will continue moving in the same direction as before.
If not all balls can be dropped, then there is no determined winning ticket. This happens when a ball is dropped and its drop node is already occupied by another ball.
If all balls are dropped successfully, then the final positions of the balls determine the winning ticket. The -th element of the winning ticket is the label of the ball that stops at node , or if no ball stops at node .
Troy wants to know the number of possible winning tickets. Since the answer can be very large, you only need to output it modulo .
Input Format
The first line contains two integers and separated by spaces, representing the number of nodes in the binary tree and the number of balls.
The second line contains integers separated by spaces, where the -th integer is the designated drop node of ball .
The last lines each contain two integers separated by spaces. The -th of these lines contains and , representing the left child and right child of node , where means the child does not exist.
Output Format
Output the number of winning tickets modulo .
5 2
1 3
2 3
0 0
4 5
0 0
0 0
4
4 3
1 2 4
0 2
0 3
0 4
0 0
2
Hint
Explanation for Sample 1

Consider the case where ball is dropped first. If ball moves left, then it will stop at node . Afterwards, ball is dropped and may stop at node or . If ball moves right, it will stop at node . Then ball will stop at node .
Consider the case where ball is dropped first. Ball may move left or right and stop at node or , respectively. Then, if ball moves left after being dropped, it will stop at node . However, if ball moves right, it will stop at node or , depending on which position ball did not occupy.
The possible winning tickets are , , , and .
Explanation for Sample 2

This sample satisfies the condition of the second subtask.
Ball must be dropped first, because if ball or ball is dropped before ball , it would stop at node , which would prevent ball from being dropped.
Therefore, we can drop ball first, then ball , and finally ball , or we can drop ball first, then ball , and finally ball . The corresponding winning tickets are and .
Constraints
For all testdata, .
| Subtask ID | Points | Range of | Range of | Additional constraints |
|---|---|---|---|---|
| None | ||||
| All nodes have no left child | ||||
| The drop nodes are all distinct | ||||
| None |
Translated by ChatGPT 5