UVa 10004 - Bicoloring

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

 1 #include <cstdio>
 2 #include <cstring>
 3 #define MAXN 210
 4 
 5 bool G[MAXN][MAXN];
 6 int n, color[MAXN];
 7 
 8 bool bipartite(int u)
 9 {
10     for (int i = 0; i < n; i++)
11         if (G[u][i])
12         {
13             int v = i;
14             if (color[u] == color[v])  return false;
15             if (!color[v])
16             {
17                 color[v] = 3 - color[u];
18                 if (!bipartite(v))  return false;
19             }
20         }
21     return true;
22 }
23 
24 int main()
25 {
26 #ifdef LOCAL
27     freopen("in", "r", stdin);
28 #endif
29     while (scanf("%d", &n) && n)
30     {
31         int k;
32         scanf("%d", &k);
33         int x, y;
34         memset(G, 0, sizeof G);
35         for (int i = 0; i < k; i++)
36         {
37             scanf("%d%d", &x, &y);
38             G[x][y] = G[y][x] = 1;
39         }
40         memset(color, 0, sizeof color);
41         color[0] = 1;
42         if (bipartite(0))  printf("BICOLORABLE.\n");
43         else  printf("NOT BICOLORABLE.\n");
44     }
45     return 0;
46 }
View Code

 

posted @ 2013-09-16 13:45  xiaobaibuhei  阅读(212)  评论(0编辑  收藏  举报