Under construction

Still being built. Things may change

Cereal 2

Time limit1sMemory limit1024 MB

Summary
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.
Level

Medium7 of 10

Topics
Graph, Greedy, DFS
Solved
No attempts yet

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 MM types of cereal (2≤M≤105)(2\le M\le 10^5). There is only one box of each type. Each of the NN cows (1≤N≤105)(1\le N\le 10^5) has a favorite cereal and a second-favorite cereal. When a cow gets a chance to choose cereal, she does the following:

  1. If the box of her favorite cereal is still available, she takes it and leaves.
  2. Otherwise, if the box of her second-favorite cereal is still available, she takes it and leaves.
  3. 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 NN cows that achieves this minimum.

Input

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

For each 1≤i≤N1\le i\le N, the ii-th line contains two space-separated integers fif_i and sis_i (1≤fi,si≤M1\le f_i,s_i\le M and fi≠sif_i\neq s_i), where fif_i is the favorite cereal and sis_i is the second-favorite cereal of cow ii.

Output

Print the minimum number of cows that go hungry, followed by any permutation of 1…N1\ldots N that achieves this minimum. If several permutations achieve the minimum, any one of them is accepted.

Hint

In this case, there are 88 cows and 1010 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 11 chooses cereal 22, cow 22 chooses cereal 33, and cow 33 goes hungry.

If the first three cows choose in the order [1,3,2], then cow 11 chooses cereal 22, cow 33 chooses cereal 33, and cow 22 chooses cereal 44; 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 33 chooses cereal 22, cow 11 chooses cereal 11, and cow 22 chooses cereal 33; again, none of cows [1,2,3] goes hungry.

One can show that at least one of the last five cows must go hungry.

Examples1

  1. Example 1

    Input
    8 10
    2 1
    3 4
    2 3
    6 5
    7 8
    6 7
    7 5
    5 8
    
    Expected output
    1
    1
    3
    2
    8
    4
    6
    5
    7