Calvinball Championship Team Assignment
Time limit1sMemory limit256 MB
Assign n players to the fewest teams so no rival pair shares a team, breaking ties by lexicographically smallest team numbers.
- Level
Medium6 of 10
- Topics
- Backtracking, Graph, Brute force
- Solved
- No attempts yet
Problem
The Calvinball championship is held again this year. A game is played by players with distinct names, and the players are divided into non-empty teams. There is no limit on the number of teams. Some pairs of players dislike each other. Disliking is symmetric: if player dislikes player , then also dislikes .
The organizing committee changed the rule for forming teams. Two players who dislike each other cannot be on the same team, and subject to that, the number of teams has to 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 and the three of them go to three different teams. Four teams is not an answer either, because a smaller number works.
Given the pairs of players who dislike each other, divide the players into teams under this rule.
Input
The first line contains the number of players and the number of pairs of players who dislike each other , separated by a space. (, ) The players are numbered from to .
Each of the next lines contains two distinct integers and . () This means that player and player dislike each other. The same pair is never given twice.
Output
Print the number of teams on the first line. On each of the next lines, print the numbers of the players on that team in increasing order, separated by spaces.
Several divisions can use the minimum number of teams, so exactly one of them is chosen by the following rule. Let be the number of the team that player belongs to, where teams are numbered from in order of first appearance. That is, , and for every . Among the divisions that use the minimum number of teams, print the one whose sequence is lexicographically smallest. Print the teams in order, from team to team .
If , print only on the first line.