您的位置:首页 > 其它

UVa 10004 - Bicoloring

2013-09-16 13:45 375 查看
  题目大意:二着色问题:给你一个图,给图中的所有点染色,只有两种颜色可选,使得每条边的两个顶点的颜色不同。二分判定问题,可用DFS或BFS解决。

#include <cstdio>
#include <cstring>
#define MAXN 210

bool G[MAXN][MAXN];
int n, color[MAXN];

bool bipartite(int u)
{
for (int i = 0; i < n; i++)
if (G[u][i])
{
int v = i;
if (color[u] == color[v])  return false;
if (!color[v])
{
color[v] = 3 - color[u];
if (!bipartite(v))  return false;
}
}
return true;
}

int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
while (scanf("%d", &n) && n)
{
int k;
scanf("%d", &k);
int x, y;
memset(G, 0, sizeof G);
for (int i = 0; i < k; i++)
{
scanf("%d%d", &x, &y);
G[x][y] = G[y][x] = 1;
}
memset(color, 0, sizeof color);
color[0] = 1;
if (bipartite(0))  printf("BICOLORABLE.\n");
else  printf("NOT BICOLORABLE.\n");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: