-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path10004-Bicoloring.cpp
36 lines (35 loc) · 1.01 KB
/
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,l,a,b;
while(scanf("%d",&n),n){
scanf("%d",&l);
unordered_map<int,unordered_set<int>> graph;
for(int i=0;i<l;i++){
scanf("%d %d",&a,&b);
graph[a].insert(b);
graph[b].insert(a);
}
queue<int> q; q.push(0);
vector<int> color(n);
color[0] = 1;
bool isBipartile = true;
while(!q.empty() & isBipartile){
int cur = q.front(); q.pop();
int curColor = color[cur];
for(int neighbor : graph[cur]){
if(color[neighbor] == curColor){
isBipartile = false;
break;
} else if(color[neighbor] == 0){
// not yet colored
color[neighbor] = -curColor;
q.push(neighbor);
}
}
}
if(isBipartile) printf("BICOLORABLE.\n");
else printf("NOT BICOLORABLE.\n");
}
}