-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10004 - Bicoloring_v1.cpp
82 lines (80 loc) · 1.67 KB
/
10004 - Bicoloring_v1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<vector>
#include<cstring>
#include<string.h>
#include<cstring>
#include<algorithm>
#define sz 201
void bfs();
using namespace std;
queue<int>q;
vector<int>v[sz];
int color[sz],flag;
int main()
{
#ifndef ONLINE_JUDGE
// freopen("H:\\C_Codes\\input.txt","r",stdin);
#endif
int t,i,j,k,n1,n2,l,col,n,edge;
while(scanf("%d",&n)&&n)
{
scanf("%d",&edge);
memset(color,0,sizeof(color));
flag=0;
for(i=0; i<edge; i++)
{
scanf("%d %d",&n1,&n2);
v[n1].push_back(n2);
v[n2].push_back(n1);
}
color[0]=1;
q.push(0);
// printf("ok\n");
// printf("node %d col %d\n",i,color[i]);
bfs();
// for(i=0; i<n; i++)
// printf("%d ",color[i]);
// cout<<endl;
if(flag==1)
printf("NOT BICOLORABLE.\n");
else printf("BICOLORABLE.\n");
for(i=0; i<=n; i++)
v[i].clear();
while(!q.empty())
q.pop();
}
return 0;
}
void bfs()
{
int f,i,j,l;
while(!q.empty())
{
f=q.front();
q.pop();
l=v[f].size();
for(i=0; i<l; i++)
{
if(color[v[f][i]]==0)
{
if(color[f]==1)
color[v[f][i]]=2;
else
color[v[f][i]]=1;
q.push(v[f][i]);
}
else if(color[v[f][i]]==color[f])
{
flag=1;
break;
}
}
if(flag==1)
break;
}
return;
}