- Code: Select all
AC
Moderator: Board moderators
AC

#include<iostream>
#include<stdio.h>
#include<queue>
#define SIZE 201
#define YES -1 // defines G is NOT BICOLORABLE
#define NO -2 // defines it is still Bicolorable
using namespace std;
int main()
{
struct graph{int connect;}G[200][200];
int visit[200],color[200];
queue<int> Q;
int n,l,i,x,y,check,u,j;
while( scanf("%d",&n) == 1 && n != 0 )
{
scanf("%d",&l);
for(i=0;i<n;i++) // Graph initialisation
{
color[i] = NO; // initially all nodes not colored
visit[i] = NO;
for(j=0;j<n;j++)
G[i][j].connect = NO;
}
for(i=0;i<l;i++)
{
scanf("%d %d",&x,&y);
G[x][y].connect = YES;
G[y][x].connect = YES;
}
check = NO;
color[0] = 0; // coloring 0th node as 0
Q.push(0);
visit[0] = YES;
while(Q.size()>0 && check==NO)
{
u = Q.front();
Q.pop();
for(i=0;i<n && check==NO ;i++)
if(G[u][i].connect==YES && (u!=i) )
{
if(visit[i]==NO )
{
visit[i] = YES;
if(color[i]==NO)
{
if(color[u]==0)
color[i] = 1;
else
color[i] = 0;}
Q.push(i);
}
else if(color[i]==color[u])
{
printf("NOT BICOLORABLE.\n");
check = YES;} // check is basically used to break out of loop
}
}
if(check == NO)
printf("BICOLORABLE.\n");
}
return 0;
}
anmol_gulati wrote:I cant understand why am I getting a WA here,seems to be an easy problem, I have tried this many times checked manually on many test cases but still getting WA..
3
3
1 0
2 0
1 2
2
1
1 0
0
NOT BICOLORABLE.
BICOLORABLE.
Users browsing this forum: No registered users and 1 guest