uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

时间:2021-05-26 02:41:44

Description

In  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 (  < n < ) 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 ( $ \le a < n$).
An input with n = 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


Sample Output

NOT BICOLORABLE.
BICOLORABLE.

dfs二分染色,和hdu 4751代码差不多

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 206
#define inf 1e12
int n,m;
vector<int>g[N];
int color[N]; bool dfs(int u,int c){
color[u]=c;
for(int i=;i<g[u].size();i++){
int num=g[u][i];
if(color[num]!=-){
if(color[num]==c){
return false;
}
continue;
}
if(!dfs(num,!c)) return false;
}
return true;
}
int main()
{
while(scanf("%d",&n)== && n!=){
for(int i=;i<N;i++){
g[i].clear();
}
scanf("%d",&m);
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
} memset(color,-,sizeof(color));
int flag=;
for(int i=;i<n;i++){
if(color[i]==- && !dfs(i,)){
flag=;
break;
}
}
if(flag){
printf("BICOLORABLE.\n");
}
else{
printf("NOT BICOLORABLE.\n");
}
}
return ;
}