Canadian Computing Olympiad: 2022 Day 2, Problem 1
In a parallel universe, everyone scored perfect on the CCO. As a result, Troy needs to pick
the winner based on a lottery. Each contestant will choose numbers to create a ticket. A
ticket is an array of size indexed from
to
where each entry is a number from
to
.
The winning ticket is determined by dropping balls (numbered from
to
) in a random
sequence into a rooted binary tree. The tree has
nodes (numbered from
to
) and is
rooted at node
.
Each ball has a designated drop node that it will drop at. When a ball is dropped at an unoccupied node or enters an unoccupied node, one of three things happens:
- If all of the current node's children are occupied by balls (or if a node has no children), the current ball rests at the current node. That is, it remains there and does not move again.
- If the current node only has one unoccupied child, the current ball will move to this child.
- If the current node has two unoccupied children, and if the current ball was just dropped, it could go either left or right. Otherwise, it will continue in the direction of its previous movement.
If all balls cannot be dropped, a winning ticket is not determined. This happens when a ball is dropped and its drop node is occupied by another ball.
If all balls have been dropped, the balls' resting positions determine the winning lottery
ticket. The
entry of the winning lottery ticket is the number of the ball that rests at
node
or
if no ball rests at node
.
Troy would like to know the number of possible winning tickets (which could be zero).
Input Specification
The first line contains two space-separated integers and
, denoting the number of nodes
in the binary tree and the number of balls, respectively.
The next line contains space-separated integers, where the
integer denotes the designated drop node of the ball numbered
.
The last lines each contain two space-separated integers. The
line contains
and
denoting the
node's left and right child, respectively, where
means no such child exists.
Marks Awarded | Bounds on |
Bounds on |
Additional constraints |
---|---|---|---|
None | |||
All nodes do not have a left child | |||
The | |||
None |
Output Specification
Output the remainder of the number of winning lottery tickets divided by .
Sample Input 1
5 2
1 3
2 3
0 0
4 5
0 0
0 0
Output for Sample Input 1
4
Explanation of Output for Sample Input 1
The tree looks like this:
Consider when ball is dropped first. If ball
goes left, then it will rest at node
. Afterward,
ball
is dropped and can rest at node
or
. If ball
goes right, it will rest at node
.
Then, ball
will rest at node
.
Consider when ball is dropped first. Ball
can go left or right, resting at nodes
or
,
respectively. Then if ball
moves left after being dropped, it will rest at node
. However, if
ball
moves right, it will rest at either node
or
, whichever place ball
does not occupy.
The possible winning tickets are
,
,
, and
.
Sample Input 2
4 3
1 2 4
0 2
0 3
0 4
0 0
Output for Sample Input 2
2
Explanation of Output for Sample Input 2
The tree looks like this:
This test case satisfies the second subtask.
Ball must be dropped first because if either ball
or ball
are dropped before ball
, it
would rest at node
, which wouldn't allow ball
to be dropped.
Thus, we can either first drop ball , then ball
and finally ball
, or we can first drop ball
, then ball
and finally ball
.
The possible winning tickets are and
.
Formal Definitions
A binary tree is a set of nodes that are either empty or a root node with a left subtree and a right subtree, both of which are binary trees. Given a node , if its left subtree is not empty,
then the root of that subtree is called the left child of
. Similarly, given a node
, if its
right subtree is not empty, then the root of that subtree is called the right child of
.
Comments