This page is still under construction.

Parts of this page are still being built. What you see may change.

Fire drill

Time limit1sMemory limit1024 MB

Summary
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 NN 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 TT, NN, and SS. TT is the test index, NN is the number of buildings, and SS is the fault threshold used in evaluation. Buildings are numbered from 11 to NN.

Each of the next NN lines describes one document. On the ii-th line, the first integer is how many buildings appear in building ii'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 NN lines, each with one building number. The first line is evacuated first, then the second, and so on. Every building appears exactly once.

Constraints

N=1000N = 1000

Examples1

  1. Example 1

    Input
    0 4 1
    2 2 3
    0
    1 4
    1 1
    
    Expected output
    4
    1
    2
    3