-
Notifications
You must be signed in to change notification settings - Fork 33
/
10004.cpp
executable file
·92 lines (80 loc) · 1.87 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Problem: Bicoloring UVa 10004
Programmer: Md. Mahmud Ahsan
Description: Graph + BFS
Compiled: Visual C++ 7.0
Date: 14-12-05
*/
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int MX = 205;
//global declaration
char color[MX];
int graph[MX][MX];
int id[MX], nc = 0;
bool colorable; // ?is given graph is colorable
char varColor = 'r'; //use to fill color
void changeColor(char c){
if (c == 'r')
varColor = 'g';
else
varColor = 'r';
}
int getId(int x){
for (int i = 0; i < nc; ++i){
if (id[i] == x) return i;
}
id[nc] = x;
return nc++;
}
void init(){
colorable = true;
varColor = 'r';
nc = 0;
for (int i = 0; i < MX; ++i){
for (int j = 0; j < MX; ++j)
graph[i][j] = 0;
}
}
void bfs(int start){
int i, u;
for (i = 0; i < nc; ++i)
color[i] = 'w';
queue <int > q;
color[start] = varColor; // varColor is now red color
q.push(start);
while (!q.empty()){
u = q.front(); q.pop();
changeColor(color[u]); // adjacent nodes are colored that is reverse color of current node
for (i = 0; i < nc; ++i){
if ( (u != i) &&(graph[u][i] == 1) && (color[i] == 'w')){
color[i] = varColor;
q.push(i);
}
//already adjacent are colored so have to check is that the same color of current node
else if ((u != i) &&(graph[u][i] == 1) && (color[u] == color[i]))
colorable = false;
}
}
}
int main(){
//freopen("input.txt", "r", stdin);
int n, l, a, b, i;
while (cin >> n){
if (n == 0) break;
init();
cin >> l; // number of edges
for (i = 0; i < l; ++i){
cin >> a >> b;
a = getId(a);
b = getId(b);
graph[a][b] = 1;
graph[b][a] = 1;
}
bfs(id[0]); //starting from the first node
if (colorable) cout << "BICOLORABLE." << endl;
else cout << "NOT BICOLORABLE." << endl;
}
return 0;
}