Beech Tree
문제
Vétyem Woods is a famous woodland with lots of colorful trees. One of the oldest and tallest beech trees is called Ős Vezér.
The tree Ős Vezér can be modeled as a set of nodes and edges. Nodes are numbered from to and edges are numbered from to . Each edge connects two distinct nodes of 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 edge colors numbered from to . The color of edge is . Different edges may have the same color.
Note that in the definitions above, the case does not correspond to an edge of the tree. For convenience, we let and .
For example, suppose that Ős Vezér has nodes and possible edge colors, with edges described by connections and colors . The tree is displayed in the following figure:
Árpád is a talented forester who likes to study specific parts of the tree called subtrees. For each such that , the subtree of node is the set of nodes with the following properties:
- Node belongs to .
- Whenever a node belongs to , all children of also belong to .
- No other nodes belong to .
The size of the set is denoted as .
Árpád recently discovered a complicated but interesting subtree property. Árpád's discovery involved a lot of playing with pen and paper, and he suspects you might need to do the same to understand it. He will also show you multiple examples you can then analyze in detail.
Suppose we have a fixed and a permutation of the nodes in the subtree .
For each such that , let be the number of times the color appears in the following sequence of colors: .
(Note that is always because the sequence of colors in its definition is empty.)
The permutation is a beautiful permutation if and only if all the following properties hold:
- .
- For each such that , the parent of node is node .
For any such that , the subtree is a beautiful subtree if and only if there exists a beautiful permutation of the nodes in . Note that according to the definition every subtree which consists of a single node is beautiful.
Consider the example tree above. It can be shown that the subtrees and of this tree are not beautiful. The subtree is beautiful, as it consists of a single node. Below, we will show that the subtree is also beautiful.
Consider the sequence of distinct integers . This sequence is a permutation of the nodes in . The figure below depicts this permutation. The labels attached to the nodes are the indices at which those nodes appear in the permutation.
Clearly, the above sequence is a permutation of the nodes in . We will now verify that it is beautiful.
- .
- since appears times in the sequence .
- Correspondingly, the parent of is . That is, the parent of node is node . (Formally, .)
- since appears times in the sequence .
- Correspondingly, the parent of is . That is, the parent of is .
- since appears time in the sequence .
- Correspondingly, the parent of is . That is, the parent of is .
- since appears time in the sequence .
- Correspondingly, the parent of is . That is, the parent of is .
- since appears times in the sequence .
- Correspondingly, the parent of is . That is, the parent of is .
- since appears times in the sequence .
- Correspondingly, the parent of is . That is, the parent of is $5
As we could find a beautiful permutation of the nodes in , the subtree is indeed beautiful.
Your task is to help Árpád decide for every subtree of Ős Vezér whether it is beautiful.