Bicoloring - UVa 10004 dfs

来源:互联网 发布:mac系统重装多少钱 编辑:程序博客网 时间:2024/06/08 19:28

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.

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$).

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 

330 11 22 0980 10 20 30 40 50 60 70 80

Sample Output 

NOT BICOLORABLE.BICOLORABLE.

题意:给你一些相邻的点,问是否存在只用两个颜色的方式使得相邻的点都有不同的颜色。

思路:第一个点随便涂,然后与其相邻的都变成另一种颜色,如果出现已经涂上颜色,并且不符合要求的情况,那就是Not Bicoloring。

AC代码如下:

#include<cstdio>#include<cstring>#include<vector>using namespace std;int n,m,col[210];vector<int> vc[210];bool flag;void dfs(int u){    int i,j,k,v,a,b,len=vc[u].size();    if(col[u]==1)    {        a=1;b=2;    }    else    {        a=2;b=1;    }    for(i=0;i<len;i++)    {        v=vc[u][i];        if(col[v]==0)        {            col[v]=b;            dfs(v);        }        else if(col[v]!=b)          flag=false;    }}int main(){    int i,j,k,u,v;    while(~scanf("%d",&n) && n>0)    {        scanf("%d",&m);        for(i=1;i<=n;i++)           vc[i].clear();        for(i=1;i<=m;i++)        {            scanf("%d%d",&u,&v);            u++;v++;            vc[u].push_back(v);            vc[v].push_back(u);        }        memset(col,0,sizeof(col));        col[1]=1;        flag=true;        dfs(1);        if(flag)          printf("BICOLORABLE.\n");        else          printf("NOT BICOLORABLE.\n");    }}



0 0
原创粉丝点击