-
Notifications
You must be signed in to change notification settings - Fork 6
/
10004.cpp
59 lines (53 loc) · 1.12 KB
/
10004.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
// Steven Kester Yuwono - UVa 10004
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#define MAX 202
using namespace std;
vector<int> adjList[MAX];
int color[MAX];
void init() {
for(int i=0;i<MAX;i++){
adjList[i].clear();
}
}
bool bfs(int start, int c) {
queue<int> q;
q.push(start);
color[start] = c;
while(!q.empty()) {
int curr = q.front(); q.pop();
int currColor = color[curr];
int neighbourColor = (currColor+1) %2;
for(int i=0;i<(int)adjList[curr].size();i++){
if(color[adjList[curr][i]]==-1){
color[adjList[curr][i]] = neighbourColor;
q.push(adjList[curr][i]);
}
else if (color[adjList[curr][i]] != neighbourColor) return false;
}
}
return true;
}
int main() {
int n,m;
while(scanf("%d %d",&n,&m)==2) {
init();
for(int i=0;i<m;i++){
int from,to;
scanf("%d %d",&from, &to);
adjList[from].push_back(to);
adjList[to].push_back(from);
}
memset(color,-1,sizeof(color));
bool ans = bfs(0,0);
if(!ans) {
memset(color,-1,sizeof(color));
ans = bfs(0,1);
}
if (ans) printf("BICOLORABLE.\n");
else printf("NOT BICOLORABLE.\n");
}
return 0;
}