Accept: 108 Submit: 884
Time Limit: 3000 mSec
Problem Description
Input
The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
The first line of the input file contains two integer numbers n and m, where n is the number of vertices in the graph (3 ≤ n ≤ 9999, n is odd), m is the number of edges in the graph (2 ≤ m ≤ 100000). The following m lines describe edges of the graph, each edge is described by two integers ai, bi (1 ≤ ai,bi ≤ n,ai = bi) — the vertex numbers connected by this edge. Each edge is listed at most once. The graph in the input file is connected, so there is a path between any pair of vertices.
Output
On the first line of the output file write a single integer number k — the minimal odd integer number, such that the degree of any vertex does not exceed k. Then write n lines with one integer number ci (1 ≤ ci ≤ k) on a line that denotes the color of i-th vertex. The colors of any two adjacent vertices must be different. If the graph has multiple different colorings, print any of them. At least one such coloring always exists.
Sample Input
1 3
3 2
1 4
4 2
2 6
6 3
3 7
4 5
5 6
5 2
Sample Output
1
1
2
1
1
1
2
3
2
2
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 10000 + 10, maxm = 100000 + 10; 6 7 int n, m, k; 8 9 struct Edge { 10 int to, next; 11 }edge[maxm << 1]; 12 13 int tot, head[maxn]; 14 int col[maxn], deg[maxn]; 15 bool vis[maxn]; 16 17 void init() { 18 tot = 0; 19 memset(head, -1, sizeof(head)); 20 memset(deg, 0, sizeof(deg)); 21 memset(col, -1, sizeof(col)); 22 } 23 24 void AddEdge(int u,int v){ 25 edge[tot].to = v; 26 edge[tot].next = head[u]; 27 head[u] = tot++; 28 } 29 30 void dfs(int u) { 31 memset(vis, false, sizeof(vis)); 32 //printf("%d %d\n", fa, u); 33 for (int i = head[u]; i != -1; i = edge[i].next) { 34 int v = edge[i].to; 35 if (col[v] != -1) vis[col[v]] = true; 36 } 37 38 for (int i = 1; i <= k; i++) { 39 if (!vis[i]) { 40 col[u] = i; 41 break; 42 } 43 } 44 45 for (int i = head[u]; i != -1; i = edge[i].next) { 46 int v = edge[i].to; 47 if (col[v] == -1) { 48 dfs(v); 49 } 50 } 51 } 52 53 int main() 54 { 55 //freopen("input.txt", "r", stdin); 56 bool flag = false; 57 while (~scanf("%d%d", &n, &m)) { 58 if(flag) printf("\n"); 59 flag = true; 60 init(); 61 int x, y; 62 for (int i = 0; i < m; i++) { 63 scanf("%d%d", &x, &y); 64 deg[x]++, deg[y]++; 65 AddEdge(x, y); 66 AddEdge(y, x); 67 } 68 69 int Max = 0; 70 for (int i = 1; i <= n; i++) { 71 Max = Max > deg[i] ? Max : deg[i]; 72 } 73 74 k = (Max & 1) ? Max : Max + 1; 75 dfs(1); 76 printf("%d\n", k); 77 for (int i = 1; i <= n; i++) { 78 printf("%d\n", col[i]); 79 } 80 } 81 return 0; 82 }