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
  • 相关阅读:
    delphi之动态库调用和串口通讯
    git如何使用 svn如何使用
    delphi学习笔记1
    sql
    linux连接与传输等命令
    jdbc
    list/tstringlist/tobjectlist
    SQL GRANT
    Invalidate介绍
    FORM 的创建
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3324119.html
Copyright © 2011-2022 走看看