Calvinball minimum teams
Time limit1sMemory limit256 MB
Split up to 20 players into the fewest teams with no disliking pair together, breaking ties lexicographically.
- Level
Medium7 of 10
- Topics
- Backtracking, Graph, Bit manipulation
- Solved
- No attempts yet
Problem
The Calvinball world championship is held in the Czech Republic again this year. One game is played by n players with distinct names, and the players are split into some number of non-empty teams. Some players dislike each other. Disliking is symmetric: if player a dislikes player b, then b also dislikes a.
The International Calvinball Disorganization changed the team selection rule. 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.
Suppose Calvin, Hobbes, Susie, Tom, Jerry, and Batman play. Batman dislikes the other five, and Tom dislikes Jerry and Hobbes. Three teams are enough here: Batman alone, Tom with Susie, and Calvin with Hobbes and Jerry. Two teams are not enough, because Batman, Tom, and Jerry all dislike each other and must go to three different teams.
Given every pair of players who dislike each other, split the players into teams. Several splits can satisfy the rule, so the output section fixes which one to print.
Input
The first line contains the number of players n and the number of disliking pairs m, separated by a space (, ). Players are numbered from 1 to n.
Each of the next m lines contains the numbers and of two players who dislike each other (, ). No pair is given twice, and a pair is never repeated with its two numbers swapped.
Output
Print the minimum number of teams t on the first line. On each of the next t lines, print the numbers of the players on that team in increasing order, separated by spaces.
When several splits use t teams, print the one this rule picks. Let be the number of the team that player v belongs to. Among all splits into t teams, print the one whose sequence is lexicographically smallest. Under this rule the smallest player number on team i grows as i grows.
If n is 0, print only 0 on the first line and print no team lines.