Carnival General

Time limit: 1s
Memory limit: 64MB
Input:
Output:

Every four years, the students of Lund come together to organize the Lund Carnival. For a few days, a park fills with tents where all kinds of festive activities take place. The person in charge of making this happen is the carnival general.

In total, there have been NN carnivals, each with a different general. The generals are numbered from 00 to N1N − 1 in chronological order. Every general ii has given their opinion on how good their predecessors were, by publishing a ranking of the generals 0,1,,i10, 1,…,i − 1 in order from best to worst.

The next Lund Carnival will be in 2026. In the meantime, all past carnival generals have gathered to take a group photo. However, it would be awkward if generals ii and j (j \ (where i<j)i < j) end up next to each other if ii is strictly in the second half of jj's ranking.

For example:

  • If general 44 has given the ranking 3 2 1 0, then 44 can stand next to 33, or 22, but not 11 or 00.
  • If general 55 has given the ranking 4 3 2 1 0, then 55 can stand next to 4,34, 3 or 22, but not 11 or 00. Note that it is fine if one general is exactly in the middle of another's ranking.

The following figure illustrates sample 1. Here, general 55 stands next to generals 22 and 33, and general 44 stands next to general 22 only.

You are given the rankings that the generals published. Your task is to arrange the generals 0,1,,N10, 1,…, N − 1 in a row, so that if ii and jj are adjacent ((where i<j)i < j) then ii is not strictly in the second half of jj's ranking.

Input

The first line contains the positive integer NN, the number of generals.

The following N1N − 1 lines contain the rankings. The first of these lines contains general 11's ranking, the second line contains general 22's ranking, and so on until general N1N − 1. General 00 is absent since general 00 didn't have any predecessors to rank.

The ranking of general ii is a list with ii integers pi,0,pi,1,,pi,i1p_{i, 0}, p_{i, 1}, …, p_{i, i - 1} in which every integer from 00 to i1i − 1 occurs exactly once. Specifically, pi,0p_{i, 0} is the best and pi,i1p_{i, i - 1} is the worst general according to general ii.

Print a list of integers, an ordering of the numbers 0,1,,N10, 1,…, N − 1, such that for each pair of adjacent numbers, neither is strictly in the second half of the other's ranking.

It can be proven that a solution always exists. If there are multiple solutions, you may print any of them.

Constraints and Scoring

  • 1N1 0001 \leq N \leq 1 \ 000
  • 0pi,0,pi,1,...,pi,i1i10 \leq p_{i, 0}, p_{i, 1}, ..., p_{i, i - 1} \leq i - 1 for i=0,1,...,N1i = 0, 1, ..., N - 1

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group, you need to solve all test cases in the test group.

# Score Limits
1 11 The ranking of general ii will be i1,i2,...,0i - 1, i - 2, ..., 0 for all ii such that 1iN11 ≤ i ≤ N − 1
2 23 The ranking of general ii will be 0,1,...,i10, 1, ..., i - 1 for all ii such that 1iN11 ≤ i ≤ N − 1
3 29 N8N \leq 8
4 37 No additional constraints.

Example 1

stdin

6
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0

stdout

4 2 5 3 1 0

Explication

The first sample matches the condition of test group 11. In this sample, neither general 22 nor 33 can stand next to general 00, and neither general 44 nor 55 can stand next to generals 00 and 11. The sample output was illustrated in the figure above.

Example 2

stdin

5
0
0 1
0 1 2
0 1 2 3

stdout

2 0 4 1 3

Explication

The second sample matches the condition of test group 22. In this sample, general 22 can't stand next to general 11, general 33 can't stand next to general 22, and general 44 can't stand next to generals 33 and 22.

Example 3

stdin

4
0
1 0
0 2 1

stdout

3 0 1 2

Explication

The third sample matches the condition of test group 33. In this sample, the only pairs of generals that can't stand next to each other are (1,3)(1, 3) and (0,2)(0, 2). Hence, there are no conflicts if they are arranged 3 0 1 2. Another possible answer is 0 1 2 3.

Problem info

ID: 983

Editors:

Author:

Source: EGOI 2023: Day 2, Problem 1

Tags:

EGOI 2023 Day 2

Log in or sign up to be able to send submissions!