zoukankan      html  css  js  c++  java
  • Uva 10004 Bicoloring

    Bicoloring 

    In 1976 the ``Four Color Map Theorem" was proven with the assistance of a computer. This theorem states that every map can be colored using only four colors, in such a way that no region is colored using the same color as a neighbor region.在1976年,四色定理在计算机的辅助下被证明了,这个定理的内容指的是地图中每个相邻的区域用不同的颜色填充,那么按这种方式填充一张地图能被四种颜色涂满

    Here you are asked to solve a simpler similar problem. You have to decide whether a given arbitrary connected graph can be bicolored. That is, if one can assign colors (from a palette of two) to the nodes in such a way that no two adjacent nodes have the same color. To simplify the problem you can assume:这里有个简单地问题,你要判断一张上面画着无规律的连接点的图能够被两种颜色涂满,也就是说,如果你能从画板上拿两种颜色将连接点涂满,是每两个相邻的点有不同的颜色,为了简化问题你可以认为:

    • no node will have an edge to itself. 没有点的边是连接它自己的
    • the graph is nondirected. That is, if a node a is said to be connected to a node b, then you must assume that b is connected to a.图是无向图,也就是说,如果 a 连向 b, 那么 b 也连向 a
    • the graph will be strongly connected. That is, there will be at least one path from any node to any other node. 图是强连通的,也就是说,每个点至少有一条路径可以通向另外一个点

    Input 

    The input consists of several test cases. Each test case starts with a line containing the number n ( 1 < n< 200) of different nodes. The second line contains the number of edges l. After this, l lines will follow, each containing two numbers that specify an edge between the two nodes that they represent. A node in the graph will be labeled using a number a ( $0 \le a < n$).输入有多组测试数据,每组测试数据开始第一行是一个数 n,表示有节点的数目,第二行是一个数 l,接下来是 l 行数据,每行包含两个数字说明在这两个数之间有边连接

    An input with n = 0 will mark the end of the input and is not to be processed.

    Output 

    You have to decide whether the input graph can be bicolored or not, and print it as shown below.

    Sample Input 

    3
    3
    0 1
    1 2
    2 0
    9
    8
    0 1
    0 2
    0 3
    0 4
    0 5
    0 6
    0 7
    0 8
    0
    

    Sample Output 

    NOT BICOLORABLE.
    BICOLORABLE.
    

    Miguel Revilla 
    2000-08-21
     
    #include<stdio.h>
    #include<string.h>
    #define MAXN 210
    int maze[MAXN][MAXN];
    int flag[MAXN], visit[MAXN];
    int Traverse(int current, int x, int n)
    {
        int i, j;
        visit[current] = 1;
        for(i=0; i<n; ++i)
        {
            if(maze[current][i] == 1)
            {
                if(flag[i] == 0)
                {
                    flag[i] = x;
                    return Traverse(i, -x, n);
                }
                else if(flag[i] == x)
                {
                    if(!visit[i]) return Traverse(i, -x, n);
                }
                else return 0;
            }
        }
        return 1;
    }
    
    int main()
    {
    
        int n, m, l, i, j, x, y;
        while(scanf("%d", &n) != EOF && n)
        {
            memset(flag, 0, sizeof(flag));
            memset(visit, 0, sizeof(visit));
            memset(maze, 0, sizeof(maze));
            scanf("%d", &l);
            for(i=0; i<l; ++i)
            {
                scanf("%d%d", &x, &y);
                maze[x][y] = maze[y][x] = 1;
            }
            x = 1;
            flag[0] = x;
            if(Traverse(0, -x, n)) printf("BICOLORABLE.\n");
            else printf("NOT BICOLORABLE.\n");
            
        }
        return 0;
    }

    解题思路:简单题

    随便给一个点定一个值(flag数组)然后判断跟其相连的点是否已有值,如果有的话看两者是否相同,如果相同就说明不行,直接返回,否则就给他赋值或继续,注意访问的点就不用再访问了

  • 相关阅读:
    存储器多级结构
    649. Dota2 参议院
    pycharm安装第三方库失败
    python -m pip install --upgrade pip升级失败
    P1149 火柴棒等式
    HTTP详解
    ref与out
    EF查询数据库框架的搭建
    EF查询数据库框架的搭建
    css清除浮动
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3006257.html
Copyright © 2011-2022 走看看