Uva 10004 Bicoloring

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=945

主要用dfs

#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 200+10
using namespace std;

int edge[MAXN][MAXN],vis[MAXN],color[MAXN],n;//color数组的值为0、1分别代表2种不同的颜色

bool dfs(int cur)
{
	for(int i=0;i<n;i++)
		if(edge[cur][i])
		{
			if(!vis[i]) 
			{
				vis[i]=1;
				color[i]=!color[cur];
				dfs(i);
			}
			else if(color[cur]==color[i])
				return false;
		}
	return true;
}
int main()
{
	//freopen("in.txt","r",stdin);
	int l;
	while(cin>>n&&n)
	{
		cin>>l;
		memset(vis,0,sizeof(vis));
		memset(edge,0,sizeof(edge));
		for(int i=0;i<l;i++)
		{
			int n1,n2;
			cin>>n1>>n2;
			edge[n1][n2]=edge[n2][n1]=1;
		}
		color[0]=0;
		vis[0]=1;
		if(dfs(0)) cout<<"BICOLORABLE."<<endl;
		else cout<<"NOT BICOLORABLE."<<endl;
	}
	return 0;
}
 

你可能感兴趣的:(uva,10004,Bicoloring)