problems / 154

Pruning the Sorting Chutes

easy binary-search-tree trees dfs stack 1000 ms · 256 MB

Pruning the Sorting Chutes

A parcel sorting machine is built from n junctions, numbered from 1 to n. Every junction carries a tag, and all tags are distinct. Parcels enter at junction 1. At each junction a chute may leave to the left and a chute may leave to the right; each chute either ends (the parcel drops into a bin) or leads to another junction. Every junction except junction 1 is reached by exactly one chute, and every junction can be reached from junction 1.

The machine is wired consistently: for every junction J, every junction reachable through J's left chute (directly or through further junctions) has a tag smaller than J's tag, and every junction reachable through J's right chute has a tag larger than J's tag.

The operator decides to keep only the junctions whose tags lie in the range [L, R] (inclusive) and to decommission every other one. The rebuilt part of the machine hanging below any spot is defined as follows, starting from the entry spot of junction 1:

  • If the spot holds no junction, nothing is built there.
  • If the spot holds a junction with tag t where L ≤ t ≤ R, the junction stays. Its left chute leads to the rebuilt version of what was below its left chute, and its right chute leads to the rebuilt version of what was below its right chute.
  • If t < L, the junction is removed and everything below its left chute is scrapped (all of those tags are smaller than t, too). The rebuilt version of what was below its right chute moves up and takes the junction's place.
  • If t > R, symmetrically, the junction and everything below its right chute are scrapped, and the rebuilt version of what was below its left chute takes the junction's place.

After rebuilding, an inspection robot starts at the entry and visits the remaining junctions in the following order: it inspects the junction it is at, then inspects the whole system hanging off that junction's left chute in the same manner, and then the whole system hanging off its right chute.

Print the tags in the order the robot inspects them.

Input

The first line contains three integers n, L and R (1 ≤ n ≤ 2·10^5, 1 ≤ L ≤ R ≤ 10^9).

The i-th of the next n lines describes junction i with three integers t_i, a_i and b_i (1 ≤ t_i ≤ 10^9, 0 ≤ a_i, b_i ≤ n): its tag, and the junction its left chute and its right chute lead to, where 0 means that chute ends in a bin.

The tags are distinct, and the description satisfies all the rules above: junction 1 is the entry, every other junction is the end of exactly one chute, all junctions are reachable from junction 1, and the wiring is consistent with the tags. Chains of junctions may be up to n long.

Output

On the first line print k, the number of junctions that remain. On the second line print the k tags in the order the robot inspects them, separated by spaces. If no junction remains, print 0 and leave the second line empty.

Notes

In the first example the junction tagged 3 is below the range, so its left chute (the junction tagged 1) is scrapped and the junction tagged 6, with both of its chutes, moves up to become the left neighbour of the entry junction 8. The junction tagged 14 is above the range and is removed with the junction 13 below it. The robot visits 8, then the left system 6, 4, 7, then 10.

In the second example the entry junction itself (tag 2) is removed, so the junction tagged 9 becomes the new entry.

Example 1

Input

9 4 10
8 2 3
3 4 5
10 0 6
1 0 0
6 7 8
14 9 0
4 0 0
7 0 0
13 0 0

Output

5
8 6 4 7 10

Example 2

Input

6 5 9
2 4 3
12 0 0
9 6 2
1 0 0
7 0 0
5 0 5

Output

3
9 5 7

Hints · reveal one at a time if you're stuck

Hint 1

The junctions and chutes form a binary search tree keyed by tag. Think about what happens to the tree when a junction's tag falls outside [L, R]: which of its subtrees can still contribute, and where do they end up?

Hint 2

The robot visits junctions in preorder, so you only need the original preorder with out-of-range tags removed. Walk the tree with an explicit stack, and skip any subtree that cannot hold an in-range tag, because the tags on each side are ordered.

report an issue Prefer your own editor? sooks prep 154 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.