#P6279. [USACO20OPEN] Favorite Colors G
[USACO20OPEN] Favorite Colors G
Description
Farmer John has cows, and each cow has a favorite color. The cows are numbered , and each color can also be represented by an integer in .
There are pairs of cows where cow admires cow . It is possible that , in which case a cow admires herself. For any color , if cows and both admire a cow whose favorite color is , then cows and have the same favorite color.
Given this information, find an assignment of favorite colors to cows such that the number of distinct favorite colors among all cows is maximized. Since there may be multiple assignments that achieve this, output the lexicographically smallest one (meaning you should minimize, in order, the colors assigned to cows ).
Input Format
The first line contains and .
The next lines each contain two space-separated integers and (), indicating that cow admires cow . The same pair of cows may appear multiple times in the input.
Output Format
For each in , output the color assigned to cow on its own line.
9 12
1 2
4 2
5 8
4 6
6 9
2 9
8 7
8 3
7 1
9 4
3 5
3 4
1
2
3
1
1
2
3
2
3
Hint
Sample Explanation:
In the figure below, cows with a thick-bordered circle are those whose favorite color is .

Constraints: for of the testdata, .
There are test points. Point is the sample, and the remaining points have the following properties:
Test points satisfy .
Test points have no additional constraints.
Problem authors: William Lin, Benjamin Qi.
Translated by ChatGPT 5
京公网安备 11011102002149号