-
Notifications
You must be signed in to change notification settings - Fork 0
/
10004-Bicoloring.cpp
68 lines (53 loc) · 914 Bytes
/
10004-Bicoloring.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
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
int main()
{
freopen("/home/ornob/Downloads/Practice/input.txt", "r", stdin);
freopen("/home/ornob/Downloads/Practice/output.txt", "w", stdout);
int n;
while(cin>>n)
{
if(n==0)
break;
vector<vector<int> > adj(n);
vector<int>color(n, -1);
bool isBipartite=true;
int l;
cin>>l;
while(l--)
{
int u, v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
queue<int>q;
color[0]=1;
q.push(0);
while(!q.empty() && isBipartite)
{
int u=q.front();
q.pop();
for(int i=0; i<adj[u].size(); i++)
{
int v=adj[u][i];
if(color[v]==color[u])
{
isBipartite=false;
break;
}
else if(color[v]==-1)
{
color[v]=1-color[u];
q.push(v);
}
}
}
if(isBipartite)
cout<<"BICOLORABLE."<<endl;
else
cout<<"NOT BICOLORABLE."<<endl;
}
return 0;
}