Calvinball team division
Time limit1sMemory limit256 MB
Color up to 14 players with the fewest teams so rivals differ, breaking ties by the smallest team-number sequence.
- Level
Medium6 of 10
- Topics
- Graph, Backtracking, Brute force
- Solved
- No attempts yet
Problem
players enter a Calvinball championship. The players are numbered through , and every player joins exactly one non-empty team. There is no limit on the number of teams.
Some pairs of players dislike each other. Disliking is symmetric: if player dislikes player , then player dislikes player .
The organizers changed the team selection rule. Two players who dislike each other must not share a team, and among all divisions that respect this, the number of teams must be as small as possible.
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 impossible, because Batman, Tom and Jerry dislike each other, so the three of them need three different teams. Four teams is not the answer either, because a division into fewer teams exists.
Given who dislikes whom, divide the players into teams.
Input
The first line contains the number of players and the number of disliking pairs , separated by a space. (, )
Each of the next lines contains two different integers and (, ), meaning that players and dislike each other. No pair is given twice.
Output
Print the smallest possible number of teams on the first line.
The teams are numbered through . On the -th of the next lines print the numbers of the players on team in increasing order, separated by single spaces.
Several divisions can use teams, so print the one picked by this rule. Let be the number of the team that player belongs to. Print the division whose sequence is lexicographically smallest. That is, keeping the number of teams at the minimum , make as small as possible, then as small as possible, and so on. Exactly one division satisfies this rule.
If , print on the first line and nothing else.