Calvinball championship, again
Time limit1sMemory limit256 MB
Color n players with the fewest teams so no disliking pair shares a team, breaking ties by the lexicographically smallest assignment.
- Level
Medium7 of 10
- Topics
- Backtracking, Graph, Bit manipulation
- Solved
- No attempts yet
Problem
The Calvinball championship is held again this year. A game is played by players with distinct names. Every player belongs to exactly one team, and no team is empty. Some players dislike each other. Disliking is symmetric: if player dislikes player , then dislikes .
The organizers changed the rule for forming teams. Two players who dislike each other may not be on the same team, and subject to that, the number of teams must be as small as possible.
For example, suppose Calvin, Hobbes, Susie, Tom, Jerry and Batman play, Batman dislikes the other five, and Tom dislikes Jerry and Hobbes. Three teams are enough: Batman alone, Tom with Susie, and Calvin with Hobbes and Jerry. Two teams are not enough, because Batman, Tom and Jerry dislike each other pairwise and need three different teams. Four teams are not an answer either, since three teams are possible and the number of teams has to be smallest.
Several divisions can satisfy the rule, so exactly one of them is asked for. Number the teams through and let be the number of the team that player belongs to. Among all divisions into the smallest number of teams and all ways of numbering those teams, the answer is the one whose sequence is lexicographically smallest. A sequence comes before a sequence lexicographically if holds the smaller value at the first position where the two differ. Under this rule player always belongs to team .
Input
The first line contains the number of players and the number of disliking pairs (, ). The players are numbered through . Each of the next lines contains two different integers and (, ), meaning that players and dislike each other. The same pair is never given twice, in either order.
Output
Print the smallest possible number of teams on the first line. Then print lines, where line contains the numbers of the players on team in increasing order, separated by single spaces. The team numbering must follow the lexicographically smallest rule stated above.