Cereal 2
Time limit1sMemory limit1024 MB
Model cows as edges between their two cereals and find an order that assigns as many cows as possible to distinct cereal boxes, minimizing hunger.
Problem
Farmer John's cows like cereal for breakfast more than anything else. Their appetites are so large that each cow eats an entire box of cereal in one meal.
The farm has just received a shipment of types of cereal . There is only one box of each type. Each of the cows has a favorite cereal and a second-favorite cereal. When a cow gets a chance to choose cereal, she does the following:
- If the box of her favorite cereal is still available, she takes it and leaves.
- Otherwise, if the box of her second-favorite cereal is still available, she takes it and leaves.
- Otherwise, she moos with disappointment and leaves without taking any cereal.
Find the minimum number of cows that go hungry when the cows are ordered optimally. Also find any permutation of the cows that achieves this minimum.
Input
The first line contains two space-separated integers and .
For each , the -th line contains two space-separated integers and ( and ), where is the favorite cereal and is the second-favorite cereal of cow .
Output
Print the minimum number of cows that go hungry, followed by any permutation of that achieves this minimum. If several permutations achieve the minimum, any one of them is accepted.
Hint
In this case, there are cows and types of cereal.
The first three cows can be solved independently of the last five because they share no favorite cereal.
If the first three cows choose in the order [1,2,3], then cow chooses cereal , cow chooses cereal , and cow goes hungry.
If the first three cows choose in the order [1,3,2], then cow chooses cereal , cow chooses cereal , and cow chooses cereal ; none of these cows goes hungry.
There are other permutations in which none of the first three cows goes hungry. For example, if they choose in the order [3,1,2], then cow chooses cereal , cow chooses cereal , and cow chooses cereal ; again, none of cows [1,2,3] goes hungry.
One can show that at least one of the last five cows must go hungry.