#P8095. [USACO22JAN] Cereal 2 S

    ID: 9202 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>USACO2022网络流Special Judge图论建模构造

[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 MM different kinds of cereal (2M1052\le M\le 10^5). Unfortunately, there is only one box of each kind of cereal. Each of the NN cows (1N1051\le N\le 10^5) 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 NN cows that can achieve this minimum.

Input Format

The first line contains two space-separated integers NN and MM.

For each 1iN1\le i\le N, line ii contains two space-separated integers fif_i and sis_i (1fi,siM1\le f_i,s_i\le M, and fisif_i\neq s_i), the favorite and second favorite cereal of cow ii.

Output Format

Output the minimum number of hungry cows, and output any permutation of 1N1\ldots N 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 88 cows and 1010 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 [1,2,3][1,2,3], then cow 11 will choose cereal 22, cow 22 will choose cereal 33, and cow 33 will be hungry.

If the first three cows choose in the order [1,3,2][1,3,2], then cow 11 will choose cereal 22, cow 33 will choose cereal 33, and cow 22 will choose cereal 44; 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 [3,1,2][3,1,2], then cow 33 will choose cereal 22, cow 11 will choose cereal 11, and cow 22 will choose cereal 33; similarly, cows [1,2,3][1,2,3] 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 1414 test points, 44 test points satisfy N,M100N,M\le 100.

  • Among the 1414 test points, the remaining 1010 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