#P15447. 「IXOI R1」柚社子
「IXOI R1」柚社子
Description
Note: The input and output for this problem are large, please choose appropriate input/output methods.
You are given a forest of rooted trees consisting of nodes. For node , its parent in the forest is (if , it means this node is the root of a tree).
For each tree, a pair of parameters is given, where is the root of this tree.
Now you need to select several nodes and arrange them into a sequence. A valid sequence is defined as:
- For each node satisfying , the number of selected nodes in the tree rooted at must be within .
- For each selected node, if it is a root or none of its ancestors are selected, then there is no restriction; otherwise, at least one of its ancestors must appear later in the sequence (if this node's position in the sequence is , there exists an ancestor whose position in the sequence is , where ).
Find the lexicographically smallest valid sequence.
Input Format
The first line contains a positive integer , representing the number of nodes.
The next lines: line contains one to three integers. The first integer represents the parent of node in the forest. If , it is followed by two space separated positive integers , whose meaning has been given in the problem description.
Output Format
Output two lines.
The first line contains an integer , representing the length of the lexicographically smallest sequence.
The second line contains integers separated by spaces, describing the lexicographically smallest sequence.
It can be proved that under the given constraints, at least one valid sequence always exists.
10
0 2 2
1
10
6
3
0 1 2
5
1
3
1
3
2 1 4
Hint
Example Explanation
The smallest lexicographically valid sequence is with length , and it can be proved that no lexicographically smaller solution exists.
Constraints
This problem uses bundled testing.
| Subtask Id | Special Property | Points | |
|---|---|---|---|
| No | |||
| A | |||
| B | |||
| No |
Special property A: It is guaranteed that every tree is a star, i.e., each tree has at most one node with degree greater than , and all nodes with degree greater than satisfy .
Special property B: It is guaranteed that .
For all data, it is guaranteed that:
, . It is guaranteed that for any of a tree, , where denotes the number of nodes in the corresponding tree. It is guaranteed that the input graph is a forest.
京公网安备 11011102002149号