#P15447. 「IXOI R1」柚社子

    ID: 15041 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心线段树洛谷原创O2优化洛谷月赛

「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 nn nodes. For node i(1in)i(1\le i \le n), its parent in the forest is xix_i (if xi=0x_i=0, it means this node is the root of a tree).

For each tree, a pair of parameters (la,ra)(l_a, r_a) is given, where aa 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 aa satisfying xa=0x_a=0, the number of selected nodes in the tree rooted at aa must be within [la,ra][l_a, r_a].
  • 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 ii, there exists an ancestor whose position in the sequence is jj, where i<ji<j).

Find the lexicographically smallest valid sequence.

Input Format

The first line contains a positive integer nn, representing the number of nodes.

The next nn lines: line i+1(1in)i+1(1\le i\le n) contains one to three integers. The first integer xix_i represents the parent of node ii in the forest. If xi=0x_i=0, it is followed by two space separated positive integers [li,ri][l_i, r_i], whose meaning has been given in the problem description.

Output Format

Output two lines.

The first line contains an integer mm, representing the length of the lexicographically smallest sequence.

The second line contains mm 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 2,1,42,1,4 with length 33, and it can be proved that no lexicographically smaller solution exists.

Constraints

This problem uses bundled testing.

Subtask Id nn\le Special Property Points
00 2020 No 1010
11 5×1035\times 10^3 2020
22 5×1055\times 10^5 A 1010
33 B 3030
44 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 11, and all nodes ii with degree greater than 11 satisfy xi=0x_i=0.

Special property B: It is guaranteed that i,li=ri\forall i,l_i=r_i.

For all data, it is guaranteed that:

1n5×1051\le n\le 5\times 10^5, xi,0xin\forall x_i,0\le x_i\le n. It is guaranteed that for any [l,r][l,r] of a tree, 1lrsz1\le l\le r\le sz, where szsz denotes the number of nodes in the corresponding tree. It is guaranteed that the input graph is a forest.