Complete Tripartite
InterviewTime limit2sMemory limit1024 MB
Given an undirected graph, split the vertices into three nonempty sets, each independent, with every pair of sets fully joined by edges, or report -1.
- Level
Medium5 of 10
- Topics
- Hash map, Graph, Implementation
- Solved
- No attempts yet
Problem
You are given a simple undirected graph with vertices and edges. The graph has no self-loops, and there is at most one edge between any pair of vertices. The graph may be disconnected.
For two nonempty, disjoint vertex sets and , define as true if and only if all of the following hold:
- No edge has both endpoints in .
- No edge has both endpoints in .
- For every vertex in and every vertex in , there is an edge between and .
Create three vertex sets , , that satisfy the following:
- All three sets are nonempty.
- Each vertex belongs to exactly one of the sets.
- , , and are all true.
Determine whether such sets exist. If they do, print the set number of each vertex.
Input
The first line contains two integers and (, ), the number of vertices and edges. Each of the next lines contains two integers and (), meaning there is an edge between and . The graph has no self-loops, and there is at most one edge between any pair of vertices.
Output
If a valid assignment exists, print integers. The -th integer is the set number (from 1 to 3) of vertex . Otherwise, print -1. If there are multiple answers, print any of them.
Hint
In the first example, , , and satisfy all conditions. You may assign the vertices differently. Other answers such as "2 3 3 1 1 1" are also accepted.

In the second example, no valid sets can be made.