#P8095. [USACO22JAN] Cereal 2 S
[USACO22JAN] Cereal 2 S
Problem Description
Farmer John’s cows, of course, love cereal for breakfast. In fact, their appetites are so big that each cow can eat an entire box of cereal in one meal.
Recently, the farm received a package containing different kinds of cereal (). Unfortunately, there is only one box of each kind of cereal. Each of the cows () has a favorite cereal and a second favorite cereal. Given some available cereals, a cow will follow this process:
-
If her favorite cereal is still available, she takes it and leaves.
-
Otherwise, if her second favorite cereal is still available, she takes it and leaves.
-
Otherwise, she moos in disappointment and leaves without taking any cereal.
When you arrange these cows in the best possible order, find the minimum number of hungry cows. Also, output any ordering of the cows that can achieve this minimum.
Input Format
The first line contains two space-separated integers and .
For each , line contains two space-separated integers and (, and ), the favorite and second favorite cereal of cow .
Output Format
Output the minimum number of hungry cows, and output any permutation of that can achieve this minimum. If there are multiple valid permutations, output any one of them.
8 10
2 1
3 4
2 3
6 5
7 8
6 7
7 5
5 8
1
1
3
2
8
4
6
5
7
Hint
【Sample Explanation】
In this example, there are cows and kinds of cereal.
Note that we solve the first three cows independently from the last five cows, because they do not like any cereal in common.
If the first three cows choose in the order , then cow will choose cereal , cow will choose cereal , and cow will be hungry.
If the first three cows choose in the order , then cow will choose cereal , cow will choose cereal , and cow will choose cereal ; no cow will be hungry.
Of course, there are other orderings that make all three of the first cows not hungry. For example, if the first three cows choose in the order , then cow will choose cereal , cow will choose cereal , and cow will choose cereal ; similarly, cows will all not be hungry.
It can be proven that among the last five cows, at least one cow will be hungry.
【Constraints】
-
Among the test points, test points satisfy .
-
Among the test points, the remaining test points have no additional constraints.
【Notes】
This problem uses a self-written Special Judge. If you have questions about this or want to hack, please message the author privately or make a post.
Translated by ChatGPT 5