#P4376. [USACO18OPEN] Milking Order G
[USACO18OPEN] Milking Order G
Description
Farmer John has cows (), numbered , 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 observations () 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 , , , then Farmer John should milk cow sometime before cow , and milk cow sometime before cow .
The observations are prioritized, and his goal is to maximize the value such that his milking order can satisfy the first observations. When multiple milking orders satisfy the first 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 is lexicographically smaller than a milking order if, for some , holds for all , and (that is, the two orders are identical up to some position, and at that position is smaller than ).
Please help Farmer John determine the best order in which to milk the cows.
Input Format
The first line contains and . Each of the next lines describes one observation. Line describes observation : the first number is the number of cows in the observation, , followed by a list of integers giving the order of cows in that observation. The sum of all is at most .
Output Format
Output space-separated integers, a permutation of , 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 before cow , and cow before cow (first observation); cow before cow (second observation); cow before cow , and cow before cow (third observation). The first two observations can be satisfied together, but Farmer John cannot satisfy all rules simultaneously, since that would require cow before cow while also having cow before cow .
This means there are two possible milking orders: and , and the first is lexicographically smaller.
Problem source: Jay Leeds.
Translated by ChatGPT 5
京公网安备 11011102002149号