题目
分析
用两种颜色染一强连通
无向
图。
代码
#include <cstdio>
#include <cstring>
#include <vector>
#define MAX_V 205
using std::vector;
vector<int> G[MAX_V];
int V, E;
int color[MAX_V];
bool dfs(int v, int c)
{
color[v] = c;
for (int i = 0; i < G[v].size(); i++) {
if (color[G[v][i]] == c) return false;
if (color[G[v][i]] == 0 && !dfs(G[v][i], -c)) return false;
}
return true;
}
bool solve()
{
memset(color, 0, sizeof(color));
for (int i = 0; i < V; i++)
if (color[i] == 0)
if (!dfs(i, 1)) return false;
return true;
}
int main()
{
while (~scanf("%d", &V), V) {
for (int i = 0; i < V; i++) G[i].clear();
scanf("%d", &E);
int v1, v2;
for (int i = 0; i < E; i++) {
scanf("%d%d", &v1, &v2);
G[v1].push_back(v2);
G[v2].push_back(v1);
}
printf(solve() ? "BICOLORABLE.\n" : "NOT BICOLORABLE.\n");
}
return 0;
}