题意:输入一个n(3=<n<=9999)个点m条边(2=<m<=100000)的连通图,n保证为奇数,设k为最小的奇数。使得每个点 的度数不超过k,你的任务是给每个点涂色,使得相邻两点颜色不同。
分析:贪心,将涂色的点周围点都列出来,找没被涂色最小的点。
#include<iostream>
#include<string.h>
#include<sstream>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
using namespace std;
const int maxn = 100000 + 10;
struct edge {
int u, next;
}edge[maxn<<1];
int head[10000 + 10];
int vis[10000+10];
int coloruse[10000 + 100];
int color[10000 + 10];
int ans = 0;
int maxdegree = 0;
void init() {
ans = 0; maxdegree = 0;
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));
memset(color, -1, sizeof(color));
}
void addedge(int u,int v) {
edge[ans].u = v;
edge[ans].next = head[u];//把u点周围的点连成一串
head[u] = ans++;
}
void bfs() {
queue<int>q;
q.push(1);
vis[1] = 1;
while (!q.empty()) {
int u = q.front(); q.pop();
memset(coloruse, 0, sizeof(coloruse));
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].u;
if (color[v] != -1) {
coloruse[color[v]] = 1;
}
else if (!vis[v]) {
vis[v] = 1;
q.push(v);
}
}
for (int i = 1; i <= maxdegree; i++) {
if (!coloruse[i]) {
color[u] = i;
break;
}
}
}
}
int main() {
int n, m; int flag = 0;
while (cin >> n >> m) {
int degree[10000 + 10];
init();
memset(degree, 0, sizeof(degree));
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
if (u == v)continue;
addedge(u, v);
addedge(v, u);
degree[u]++;
degree[v]++;
}
for (int i = 1; i <= n; i++)maxdegree = max(maxdegree, degree[i]);
maxdegree % 2 == 0 ? cout << ++maxdegree << endl : cout << maxdegree << endl;
bfs();
for (int i = 1; i <= n; i++)
cout << color[i] << endl;
if (!flag) { cout << endl; flag = 1; }
}
return 0;
}