读书人

uva10004 Bicoloring(交织染色法)

发布时间: 2013-02-24 17:58:56 作者: rapoo

uva10004 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.
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.
the graph will be strongly connected. That is, there will be at least one path from any node to any other node.

题目翻译:
1976年“四色定理”在计算机的帮助下被证明。 这个定理宣告任何一个地图都可以只用四种颜色来填充, 并且没有相邻区域的颜色是相同的。
现在让你解决一个更加简单的问题。 你必须决定给定的任意相连的图能不能够用两种颜色填充。 就是说,如果给其中一个分配一种颜色, 要让所有直接相连的两个节点不能是相同的颜色。 为了让问题更简单,你可以假设:
1. 没有节点是连接向它自己的。
2. 是无向图。 即如果a连接b, 那么b也是连接a的
3. 图是强连接的。就是说至少有一条路径可走向所有节点。

样例输入:
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


样例输出:
NOT BICOLORABLE.
BICOLORABLE.

#include<stdio.h>int f,n,m,top,end,queue[200],map[200][200],color[200];void bfs(){    int p=queue[top];    if(f==0) return ;    for(int i=0;i<n;i++)    {        if(map[p][i]==1)        {            if(color[i]==2)            {                end++;                queue[end]=i;                color[i]=-color[p];            }            else {if(color[i]==color[p]) f=0;}        }    }    top++;    if(top<=end) bfs();    return ;}int main(){    while(scanf("%d",&n)&&n!=0)    {        for(int i=0;i<n;i++)        {            for(int j=i;j<n;j++)                map[i][j]=map[j][i]=0;            color[i]=2;        }        scanf("%d",&m);        int a,b;        for(int i=0;i<m;i++)        {            scanf("%d%d",&a,&b);            map[a][b]=map[b][a]=1;            color[a]=2;            color[b]=2;        }        top=1;        end=1;        queue[1]=a;        color[a]=1;        f=1;        bfs();        if (f==0) printf("NOT BICOLORABLE.\n");        else  printf("BICOLORABLE.\n");    }    return 0;}




读书人网 >编程

热点推荐