zoukankan      html  css  js  c++  java
  • UVa 10004

      题目大意:二着色问题:给你一个图,给图中的所有点染色,只有两种颜色可选,使得每条边的两个顶点的颜色不同。二分判定问题,可用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.
    ");
    43         else  printf("NOT BICOLORABLE.
    ");
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    hud 1166 敌兵布阵
    zznu 1914 asd的甩锅计划
    poj 1860 Currency Exchange
    poj 3026 Borg Maze (BFS + Prim)
    poj 2349 Arctic Network
    POJ 1502 MPI Maelstrom
    poj 1308 Is It A Tree?
    hdu 1272 小希的迷宫
    poj 1679 http://poj.org/problem?id=1679
    POJ 2492 A Bug's Life
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3324119.html
Copyright © 2011-2022 走看看