Calvinball team assignment
Time limit1sMemory limit256 MB
Assign up to 24 participants to the fewest teams so no disliking pair shares a team, and print the lexicographically smallest optimal assignment.
- Level
Medium7 of 10
- Topics
- Backtracking, Graph, Bit manipulation
- Solved
- No attempts yet
Problem
The Calvinball championship is held again this year. One game is played by participants with distinct names, and the participants are divided into some number of non-empty teams. Some participants dislike each other. Disliking is symmetric, so if dislikes , then dislikes .
The organizers changed the rule for forming teams. Two participants who dislike each other cannot be on the same team, and under that condition the number of teams must be as small as possible.
For example, suppose Calvin, Hobbes, Susie, Tom, Jerry and Batman play the game, 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, because Batman, Tom and Jerry dislike each other and need three different teams. Four teams are not an answer either, because three teams are possible.
Given who dislikes whom, find a division into the smallest possible number of teams. When several such divisions exist, the output section picks one of them.
Input
The first line contains the number of participants and the number of mutually disliking pairs , separated by a space, where and . The participants are numbered from to .
The -th of the next lines contains two distinct integers and with , meaning that participants and dislike each other. The same pair is never given twice.
Output
Print the number of teams on the first line. The -th of the next lines contains the numbers of the participants on team in increasing order, separated by spaces. If is , print only on the first line and no team lines.
Several divisions can use the minimum number of teams, so pick one by the following rule. First fix the team numbers. Team is the team of participant , and team is the team of the smallest-numbered participant who is not on teams through . Let be the number of the team that participant is on. Among all divisions using the minimum number of teams, print the one whose sequence comes first in lexicographic order.