#P4376. [USACO18OPEN] Milking Order G

    ID: 3319 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2018二分USACO排序图的建立,建图拓扑排序

[USACO18OPEN] Milking Order G

Description

Farmer John has NN cows (1N1051 \leq N \leq 10^5), numbered 1N1 \ldots N, who have recently gotten bored. They have developed a complex social hierarchy related to the order in which Farmer John milks them each morning.

After several weeks of study, Farmer John has made MM observations (1M50,0001 \leq M \leq 50{,}000) about the social structure of his cows. Each observation is an ordered sequence of certain cows, indicating that these cows should be milked in the order shown by the sequence. For example, if one observation is the sequence 22, 55, 11, then Farmer John should milk cow 22 sometime before cow 55, and milk cow 55 sometime before cow 11.

The observations are prioritized, and his goal is to maximize the value XX such that his milking order can satisfy the first XX observations. When multiple milking orders satisfy the first XX observations, Farmer John follows a long tradition: cows with smaller IDs outrank those with larger IDs, so he will milk the smallest-numbered cow first. More formally, if multiple milking orders satisfy these observations, Farmer John uses the lexicographically smallest one. A milking order xx is lexicographically smaller than a milking order yy if, for some jj, xi=yix_i = y_i holds for all i<ji < j, and xj<yjx_j < y_j (that is, the two orders are identical up to some position, and at that position xx is smaller than yy).

Please help Farmer John determine the best order in which to milk the cows.

Input Format

The first line contains NN and MM. Each of the next MM lines describes one observation. Line i+1i+1 describes observation ii: the first number is the number of cows in the observation, mim_i, followed by a list of mim_i integers giving the order of cows in that observation. The sum of all mim_i is at most 200,000200{,}000.

Output Format

Output NN space-separated integers, a permutation of 1N1 \ldots N, giving the order in which Farmer John should milk his cows.

4 3
3 1 2 3
2 4 2
3 3 4 1
1 4 2 3

Hint

In this example, Farmer John has four cows, and his milking order should satisfy the following rules: cow 11 before cow 22, and cow 22 before cow 33 (first observation); cow 44 before cow 22 (second observation); cow 33 before cow 44, and cow 44 before cow 11 (third observation). The first two observations can be satisfied together, but Farmer John cannot satisfy all rules simultaneously, since that would require cow 11 before cow 33 while also having cow 33 before cow 11.

This means there are two possible milking orders: 1 4 2 31\ 4\ 2\ 3 and 4 1 2 34\ 1\ 2\ 3, and the first is lexicographically smaller.

Problem source: Jay Leeds.

Translated by ChatGPT 5