#P9603. [IOI 2023] 山毛榉树
[IOI 2023] 山毛榉树
Background
This is an interactive problem. You only need to implement the function required in the code.
Your code must not include any additional header files, and you do not need to implement the main function.
This problem only supports C++ submissions.
Problem Description
Vétyem Woods is a famous, colorful forest. The oldest and tallest beech tree is called Ős Vezér.
The tree Ős Vezér can be modeled as a set of nodes and edges. The nodes are numbered from to , and the edges are numbered from to . Each edge connects two different nodes in the tree. Specifically, edge () connects node to node , where . Node is called the parent of node , and node is called a child of node .
Each edge has a color. There are possible colors, numbered from to . The color of edge is . Different edges may have the same color.
Note that in the definition above, the case does not correspond to an edge in the tree. For convenience, we set and .
For example, suppose Ős Vezér has nodes and possible colors, and thus edges. The edges are described by $P = [-1, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 10, 11, 11]$, and the edge colors are $C = [0, 1, 2, 3, 1, 2, 3, 1, 3, 3, 2, 1, 1, 2, 2, 1, 2, 3]$. The tree is shown in the figure below:

Árpád is a talented forest ranger who likes to study parts of a tree called subtrees.
For each with , the subtree of node is a set of nodes with the following properties:
- Node is in .
- If a node is in , then all children of are also in .
- No other nodes are in .
The size of the set is denoted by .
Árpád recently discovered a complex but interesting property of subtrees. His discovery requires a lot of paper-and-pencil work, and he thinks you need to do the same to fully understand it. He will also give you a few examples so that you can analyze them in detail.
Suppose we have some fixed , and a permutation of the nodes in the subtree .
For each with , let be the number of occurrences of the color in the color sequence of length , namely .
(Note that is always , because the sequence in its definition is empty.)
The permutation is called a nice permutation if and only if the following conditions hold:
- .
- For each with , the parent of node is .
For each with , the subtree is called a nice subtree if and only if there exists some nice permutation of the nodes in . Note that, by definition, any subtree that contains only one node is nice.
Consider the example tree given above. You can see that subtrees and are not nice. Subtree is nice because it contains only one node. Next, we will show that subtree is also nice.
Consider the sequence of distinct integers $[v_0, v_1, v_2, v_3, v_4, v_5, v_6] = [1, 4, 5, 12, 13, 6, 14]$. This sequence is a permutation of the nodes in . The figure below shows this permutation. The number next to each node in the sequence is the node’s index in the permutation.

We will verify that this is a nice permutation.
- .
- , because occurs times in the sequence .
- Accordingly, the parent of is . That is, the parent of is . (Formally, .)
- , because occurs times in the sequence .
- Accordingly, the parent of is . That is, the parent of is .
- , because occurs time in the sequence .
- Accordingly, the parent of is . That is, the parent of is .
- , because occurs time in the sequence .
- Accordingly, the parent of is . That is, the parent of is .
- , because occurs times in the sequence .
- Accordingly, the parent of is . That is, the parent of is .
- , because occurs times in the sequence .
- Accordingly, the parent of is . That is, the parent of is .
Since we can find a nice permutation for the nodes in , the subtree is therefore a nice subtree.
Your task is to help Árpád determine whether each subtree of Ős Vezér is nice.
Input Format
You need to implement the following function.
int[] beechtree(int N, int M, int[] P, int[] C)
- : the number of nodes in the tree.
- : the number of possible edge colors in the tree.
- , : two arrays of length describing the edges of the tree.
- The function should return an array of length .
For each with , if is nice, then should be , otherwise it should be . - This function is called exactly once for each test case.
Hint
Samples
Sample 1
Consider the following call:
beechtree(4, 2, [-1, 0, 0, 0], [0, 1, 1, 2])
The tree is shown in the figure below:

, , and each contain a single node, so they are all nice.
is not nice.
Therefore, the function should return .
Sample 2
Consider the following call:
beechtree(18, 3,
[-1, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 10, 11, 11],
[0, 1, 2, 3, 1, 2, 3, 1, 3, 3, 2, 1, 1, 2, 2, 1, 2, 3])
This example has already been given in the statement.
The function should return $[0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]$.
Sample 3
Consider the following call:
beechtree(7, 2, [-1, 0, 1, 1, 0, 4, 5], [0, 1, 1, 2, 2, 1, 1])
This example is shown in the figure below.

is the only subtree that is not nice. The function should return .
Constraints
- (for all with )
- (for all with )
- and
Subtasks
- (9 points) and .
- (5 points) Edge connects node to node . That is, for all with , we have .
- (9 points) Except for node , every other node is connected either to node or to some node that is connected to node .
That is, for all with , either or . - (8 points) For each with , at most two edges have color .
- (14 points) and .
- (14 points) and .
- (12 points) .
- (17 points) .
- (12 points) No additional constraints.
Sample Grader
The sample grader reads input in the following format:
- Line :
- Line :
- Line :
Let be the elements of the array returned by beechtree. The sample grader outputs your answer in the following format, on a single line:
- Line :
Translated by ChatGPT 5