dfs,又一次加深了印象。我感觉用BFS更简单。。。
#include<stdio.h>
#include<string.h>
struct list
{
int num[300];
int leap;
int color;
}s[500];
int leap;
void dfs(int x,int y)
{
int i;
if(s[x].color!=-1&&s[x].color!=y)
{
leap=0;
return ;
}
else
{
if(s[x].color==-1)
{
s[x].color=y;
}
else
return ;
}
for(i=0;i<s[x].leap;i++)
{
dfs(s[x].num[i],(y+1)%2);
}
}
int main()
{
int i,m,n,a,b;
while(scanf("%d",&n)&&n)
{
leap=1;
for(i=0;i<300;i++)
{
s[i].leap=0;
s[i].color=-1;
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
s[a].num[s[a].leap]=b;
s[a].leap++;
s[b].num[s[b].leap]=a;
s[b].leap++;
}
dfs(0,0);
if(leap==1)
printf("BICOLORABLE.\n");
else
printf("NOT BICOLORABLE.\n");
}
return 0;
}