#abc315e. E - Prerequisites

E - Prerequisites

Score : 425425 points

问题描述

我们有 NN 本书,编号从 11NN

ii 本书假设你已经阅读了 CiC_i 本书,其中第 jj 本是书 Pi,jP_{i,j}:在阅读第 ii 本书之前,你必须阅读所有这 CiC_i 本书。

在此条件下,你可以按照某种顺序阅读所有书籍。

你现在正尝试以最少的读书数量来读到书 11
请按阅读顺序输出除书 11 以外,你必须阅读的书的编号。在这种情况下,要读的书籍集合是唯一确定的。
如果存在多个满足条件的阅读顺序,你可以任选其中一个进行输出。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have NN books numbered 11 to NN.
Book ii assumes that you have read CiC_i books, the jj-th of which is book Pi,jP_{i,j}: you must read all these CiC_i books before reading book ii.
Here, you can read all the books in some order.

You are trying to read the minimum number of books required to read book 11.
Print the numbers of the books you must read excluding book 11 in the order they should be read. Under this condition, the set of books to read is uniquely determined.
If there are multiple reading orders that satisfy the condition, you may print any of them.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 0Ci<N0 \leq C_i < N
  • i=1NCi2×105\sum_{i=1}^{N} C_i \leq 2 \times 10^5
  • C11C_1 \geq 1
  • 1Pi,jN1 \leq P_{i,j} \leq N
  • Pi,jPi,kP_{i,j} \neq P_{i,k} for 1j<kCi1 \leq j < k \leq C_i.
  • It is possible to read all the books.

Input

The input is given from Standard Input in the following format:

NN

C1C_1 P1,1P_{1,1} \ldots P1,C1P_{1,C_1}

C2C_2 P2,1P_{2,1} \ldots P2,C2P_{2,C_2}

\vdots

CNC_N PN,1P_{N,1} \ldots PN,CNP_{N,C_N}

Output

Print the numbers of the books you must read to read book 11 in the order they should be read, with spaces in between.

Sample Input 1

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

Sample Output 1

5 3 4 2

To read book 11, you must read books 2,3,42,3,4; to read book 22, you must read books 3,53,5; to read book 44, you must read book 55. To read books 3,5,63,5,6, you do not have to read any other books.

For example, if you read books 5,3,4,25,3,4,2 in this order, you can read book 11. This is a correct answer, because you will never be able to read book 11 with three or fewer books read. As another example, reading books 3,5,4,23,5,4,2 in this order also allows you to read book 11 with 44 books read.

Sample Input 2

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

Sample Output 2

6 5 4 3 2

Sample Input 3

8
1 5
1 6
1 7
1 8
0
0
0
0

Sample Output 3

5

update @ 2024/3/10 09:00:59