#include<stdio.h>
#include<string.h>
int mat[1000][1000];
int main()
{
int queue[1000],color[1000];
int front,rear,i,j,n,l,a,b,c,d,start;
while(scanf("%d",&n)==1)
{
if(n==0)
{
break;
}
scanf("%d",&l);
front=rear=0;
memset(color,0,sizeof(color));
memset(mat,0,sizeof(mat));
for(i=0; i<l; i++)
{
scanf("%d%d",&a,&b);
mat[a][b]=1;
mat[b][a]=1;
}
d=0;
queue[rear++]=0;
color[0]=1;
while(1)
{
if(front==rear)
{
break;
}
c=queue[front++];
for(j=0; j<n; j++)
{
if(mat[c][j]==1&&color[c]==1&&(color[j]!=1&&color[j]!=2))
{
color[j]=2;
queue[rear++]=j;
d++;
}
else if(mat[c][j]==1&&color[c]==2&&(color[j]!=2&&color[j]!=1))
{
color[j]=1;
queue[rear++]=j;
d++;
}
else if(mat[c][j]==1&&color[c]==color[j])
{
d=0;
break;
}
}
if(d==0)
{
printf("NOT BICOLORABLE.\n");
break;
}
}
if(d!=0)
{
printf("BICOLORABLE.\n");
}
}
return 0;
}
#include<string.h>
int mat[1000][1000];
int main()
{
int queue[1000],color[1000];
int front,rear,i,j,n,l,a,b,c,d,start;
while(scanf("%d",&n)==1)
{
if(n==0)
{
break;
}
scanf("%d",&l);
front=rear=0;
memset(color,0,sizeof(color));
memset(mat,0,sizeof(mat));
for(i=0; i<l; i++)
{
scanf("%d%d",&a,&b);
mat[a][b]=1;
mat[b][a]=1;
}
d=0;
queue[rear++]=0;
color[0]=1;
while(1)
{
if(front==rear)
{
break;
}
c=queue[front++];
for(j=0; j<n; j++)
{
if(mat[c][j]==1&&color[c]==1&&(color[j]!=1&&color[j]!=2))
{
color[j]=2;
queue[rear++]=j;
d++;
}
else if(mat[c][j]==1&&color[c]==2&&(color[j]!=2&&color[j]!=1))
{
color[j]=1;
queue[rear++]=j;
d++;
}
else if(mat[c][j]==1&&color[c]==color[j])
{
d=0;
break;
}
}
if(d==0)
{
printf("NOT BICOLORABLE.\n");
break;
}
}
if(d!=0)
{
printf("BICOLORABLE.\n");
}
}
return 0;
}
0 comments: (+add yours?)
Post a Comment