Ural Highway
Time limit2sMemory limit1024 MB
Given each stratum's horizontal span and the top-to-bottom layer order seen in each borehole, find a depth order of all strata that agrees with every borehole.
- Level
Medium7 of 10
- Topics
- Topological sort, Intervals, Graph
- Solved
- No attempts yet
Problem
A new highway named Ural is planned. Its durability depends on the rock layers beneath it. A stratum is a geological body made of a single rock.
horizontal strata lie under the planned highway. A geological survey found the points along the highway where each stratum begins and ends. The depth order of the strata could not be determined.
Vertical boreholes were drilled at set points along the highway. Each borehole passes through several of the upper strata beneath its point. For each borehole, the order of the drilled strata from the surface downward is known. If a borehole does not reach a stratum that lies beneath its point, that stratum runs below the bottom of the borehole.

Write a program that finds a possible depth order of the strata that agrees with the data.
Input
The first line contains an integer , the number of strata. The strata are numbered from to in arbitrary order. Each of the next lines contains integers and (), the distances from the start of the highway to the points where the -th stratum begins and ends.
The next line contains an integer , the number of boreholes. Each of the next lines describes one borehole. It starts with two integers () and (), the distance from the start of the highway to the borehole and the number of strata found in it. It is followed by , the stratum numbers in order from top to bottom. The boreholes are listed in increasing order of .
It is guaranteed that a solution exists.
Output
Print integers , the possible depth order of the strata from top to bottom. Each stratum number must appear exactly once. Stratum must never pass above strata or below strata .
If several arrangements are possible, print any of them.
Constraints
- .
- The total number of strata found across all boreholes does not exceed .
Hints
The figure in the statement matches the example.
For the example, the answer 2 3 1 4 is also correct.
The example test does not belong to subtask 1. To be accepted for judging, a solution must pass the example even if it solves only this subtask.