#P9467. [EGOI 2023] Carnival General / 狂欢节总管

    ID: 10666 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>2023Special JudgeO2优化EGOI(欧洲/女生)

[EGOI 2023] Carnival General / 狂欢节总管

Background

Day 2 Problem A.

Translated from EGOI2023 carnival

CC BY-SA 3.0

Problem Description

Every four years, students in Lund gather and host the Lund Carnival. Over a few days, the park fills up with tents and many kinds of celebrations are held. The person responsible for organizing all of this is called the Carnival General.

There are NN carnivals, each with a different general. These generals are numbered from 00 to N1N-1 in chronological order. Each general ii gives their opinion on previous generals: they publish a ranking of generals 0,1,,i10,1,\cdots,i-1 from best to worst.

The next Lund Carnival will be held in 2026. During that time, all former carnival generals got together to take a group photo. However, it will be awkward if generals ii and jj (where i<ji < j) end up standing next to each other, and ii is ranked strictly in the bottom half of the ranking given by jj.

For example:

  • If the ranking given by general 44 is 3 2 1 0, then 44 may stand next to 33 or 22, but may not stand next to 11 or 00.
  • If the ranking given by general 55 is 4 3 2 1 0, then 55 may stand next to 4,3,24,3,2, but may not stand next to 11 or 00.

The figure below shows Sample 11. In it, general 55 stands next to generals 22 and 33, and general 44 stands only next to general 22.

You know all the rankings given by the generals. Your task is to arrange all generals 0,1,,N10,1,\cdots,N-1 in a line such that whenever ii and jj stand next to each other (where i<ji < j), then ii is not ranked strictly in the bottom half of the ranking given by jj.

Input Format

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

The next N1N-1 lines contain all rankings. The first of these lines is the ranking given by general 11, the second is the ranking given by general 22, and so on up to general N1N-1. General 00 is not included in the input, because general 00 has no previous generals to rank.

The ranking of general ii is a list of length ii: pi,0,pi,1,,pi,i1p_{i,0},p_{i,1},\cdots,p_{i,i-1}, where each integer from 00 to i1i-1 appears exactly once. Specifically, in the ranking of general ii, pi,0p_{i,0} is the best general and pi,i1p_{i,i-1} is the worst general.

Output Format

Output a sequence of integers: a permutation of 0,1,,N10,1,\cdots,N-1, such that for every pair of adjacent integers, neither one is in the bottom half of the other one's ranking.

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

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

Hint

Sample 11 Explanation

Sample 11 satisfies the requirements of Subtask 1. In the sample, generals 22 and 33 cannot stand next to general 00, and generals 44 and 55 cannot stand next to generals 00 and 11. The sample output is shown in the figure above.


Sample 22 Explanation

Sample 22 satisfies the requirements of Subtask 2. In the sample, general 22 cannot stand next to general 11, general 33 cannot stand next to general 22, and general 44 cannot stand next to generals 33 and 22.


Sample 33 Explanation

Sample 33 satisfies the requirements of Subtask 3. In the sample, only the two pairs (1,3)(1,3) and (0,2)(0,2) of generals cannot stand next to each other. Therefore, if the arrangement is 3 0 1 2, there will be no conflict. Another possible answer is 0 1 2 3.


Constraints

For all testdata, 2N1032\le N\le 10^3, 0pi,0,pi,1,,pi,i1i10\le p_{i,0},p_{i,1},\cdots,p_{i,i-1}\le i-1

  • Subtask 1 (1111 points): the ranking given by general ii is i1,i2,,0i-1,i-2,\cdots,0.
  • Subtask 2 (2323 points): the ranking given by general ii is 0,1,,i10,1,\cdots,i-1.
  • Subtask 3 (2929 points): N8N\le 8.
  • Subtask 4 (3737 points): no additional constraints.

Translated by ChatGPT 5