UVA 1613 K度图染色

define N 300101

include <bits/stdc++.h>

using namespace std;
struct edg {
int to, nex;
}e[N];
int n, m, k, cnt, lin[N], degree[N], color[N], flag[N];
inline void add(int f, int t)
{
degree[f]++;
e[++cnt].to = t;
e[cnt].nex = lin[f];
lin[f] = cnt;
}
void dfs(int now)//now的颜色要取除去与它相连的颜色 的最小值,因为这样才能将每个颜色都利用
{
// int sum = 0;
memset(flag, 0, sizeof(flag));
for (int i = lin[now]; i; i = e[i].nex)
{
int to = e[i].to;
if (color[to])
flag[color[to]] = 1;
}
for (int i = 1; i <= k; i++)
if (!flag[i])
{
color[now] = i;
break;
}
for (int i = lin[now]; i; i = e[i].nex)
if (!color[e[i].to])
dfs(e[i].to);
}
inline void clear()
{
memset(e, 0, sizeof(e));
memset(lin, 0, sizeof(lin));
memset(degree, 0, sizeof(degree));
memset(color, 0, sizeof(color));
k = cnt = 0;
}
int main()
{
while (cin >> n >> m)
{
clear();
for (int i = 1; i <= m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
}
for (int i = 1; i <= n; i++)
k = max(k, degree[i]);
k |= 1;
printf("%d\n", k);
dfs(1);
for (int i = 1; i <= n; i++)
printf("%d\n", color[i]);
puts("");
}
}

猜你喜欢

转载自www.cnblogs.com/liuwenyao/p/11493139.html