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
twhereL ≤ 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 thant, 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.