[UVA10004] Bicoloring
2017年8月27日给定一个强连通的无向图,问能否在只用两种颜色的情况下,对所有点进行染色,使得任意两点间的颜色各不相同。
用 DFS 或者 BFS 来写都可以。访问都某一顶点时,挨个对其所连点染色,如果所连的点在之前已经染过色了,判断一下是否与当前点相同。
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
vector<int> G[N];
int n, m;
bool ok;
int color[N];
void dfs(int x, int c)
{
color[x] = c;
for (auto v : G[x]) {
if (!color[v]) {
dfs(v, 3 - c);
} else if (color[v] != 3 - c) {
ok = false;
return;
}
}
}
int main()
{
while (~scanf("%d", &n) && n) {
scanf("%d", &m);
for (int i = 0; i < n; i++) {
G[i].clear();
}
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
ok = true;
memset(color, 0, sizeof(color));
dfs(1, 1);
if (ok) {
printf("BICOLORABLE.\n");
} else {
printf("NOT BICOLORABLE.\n");
}
}
return 0;
}