Agents
Time limit1sMemory limit128 MB
Given a graph of dislikes where at most three agents touch everyone else, decide whether the vertices 3-color into at most three independent sets and output the lexicographically smallest coloring.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Backtracking, Greedy
- Solved
- No attempts yet
Problem
A secret organization must assign every one of its agents to a team. Some pairs of agents cannot work together, so no team may contain a pair of agents who dislike each other. Because of the nature of the mission, the agents can be divided into at most three teams.
The organization has a few especially difficult members — at least one and at most three of them — and every other agent dislikes at least one of these difficult members. Thanks to this structure, deciding whether a valid division exists is always solvable in polynomial time.
Given the agents and the pairs who cannot work together, decide whether the agents can be split into at most three teams so that no team contains a disliking pair, and if so, report the assignment.
Input
The input consists of several test instances.
The first line of each instance contains two integers and (, ), separated by a space. is the number of agents, numbered from to , and is the number of pairs of agents who dislike each other. Each of the next lines contains two integers and (), meaning that agents and dislike each other. Each such pair is listed exactly once.
A blank line follows each instance. The input ends with a line containing two zeros.
Output
For each instance, print one line.
If the agents can be split into at most three teams so that no team contains a pair who dislike each other, print integers separated by single spaces: the -th integer (for from to ) is the team number in assigned to agent . Among all valid assignments, print the lexicographically smallest sequence of team numbers.
If no such division exists, print the string The agents cannot be split.