Fire drill
Time limit1sMemory limit1024 MB
Order N buildings to minimize how many times a building is evacuated before one of its listed predecessors. Output the permutation.
- Level
Medium6 of 10
- Topics
- Topological sort, Graph, Greedy
- Solved
- No attempts yet
Problem
A fire drill must empty buildings. Each building has a document that lists building numbers that must be evacuated before it. When a building is evacuated while at least one listed predecessor is still occupied, one penalty is added. Find an evacuation order that minimizes the number of penalties.
Input
The first line contains three integers , , and . is the test index, is the number of buildings, and is the fault threshold used in evaluation. Buildings are numbered from to .
Each of the next lines describes one document. On the -th line, the first integer is how many buildings appear in building 's document, followed by those building numbers. No two buildings appear in each other's documents. No document lists itself, and no number repeats within a document.
Output
Print lines, each with one building number. The first line is evacuated first, then the second, and so on. Every building appears exactly once.