Graph Coloring

시간 제한2초메모리 제한1024 MB

요약
차수가 5 이하인 무방향 그래프의 각 정점을 3가지 색으로 칠하되, 모든 정점이 같은 색인 이웃을 최대 하나만 갖도록 색을 배정하고, 불가능하면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
그래프, 그리디, 백트래킹, 구현
정답자
아직 제출이 없습니다

문제

You are given a bidirectional graph where the degree of each vertex is at most 5. Paint its vertices in 3 colors in such a way that each vertex vv has no more than one neighbor of the same color as vv.

입력

On the first line, there are two integers nn and mm: the number of vertices and the number of edges (1≤n≤100,0001 \le n \le 100\\,000).

Each of next mm lines contains two integers aa and bb: the numbers of vertices connected by an edge (1≤a,b≤n1 \le a, b \le n).

It is guaranteed that there are no loops or multiple edges in the graph, and the degree of each vertex is at most 5.

출력

It there is no valid coloring, print one number "-1" (without quotes). Otherwise, print nn integers c_1c\_1, c_2c\_2, …\ldots, c_nc\_n: the colors of all vertices (1≤c_i≤31 \le c\_i \le 3). If there is more than one solution, print any one of them.

예제3

  1. 예제 1

    입력
    3 3
    1 2
    2 3
    1 3
    
    예상 출력
    2 1 1
    
  2. 예제 2

    입력
    6 15
    1 2
    1 3
    1 4
    1 5
    1 6
    2 3
    2 4
    2 5
    2 6
    3 4
    3 5
    3 6
    4 5
    4 6
    5 6
    
    예상 출력
    2 2 3 3 1 1
    
  3. 예제 3

    입력
    3 1
    1 2
    
    예상 출력
    1 1 1