6991.Graph Coloring

Time Limit: 1s Memory Limit: 512MB

You are given a simple graph with $n$ nodes and $m$ edges. Your task is to use the minimum possible number of colors to color each node such that no edge connects two nodes of the same color.

Input Format(From the terminal/stdin)

The first line has two integers $n$ and $m$ : the number of nodes and edges. The nodes are numbered $1, 2,\dots, n$ .

Then there are $m$ lines describing the edges. Each line has two integers $a$ and $b$ : there is an edge connecting nodes $a$ and $b$ .

  • $1 \le n \le 16$
  • $0 \le m \le \frac{n(n-1)}{2}$

Output Format(To the terminal/stdout)

First, print an integer $k$ : the minimum number of colors.

After this, print $n$ integers $c_1, c_2,\dots, c_n$ : the colors of the nodes. The colors should satisfy $1 \le c_i \le k$ .

You may print any valid solution.

Sample Input

Copy
4 4
1 2
2 3
3 4
4 1
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output special judge

Copy
2
1 2 1 2
 \n
 · · · \n
Source: CSES, Advanced Graph Problems, 3308

Submit

请先 登录

© 2025 FAQs