-
Notifications
You must be signed in to change notification settings - Fork 1
/
P10004.cpp
54 lines (52 loc) · 1.34 KB
/
P10004.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
#include <iostream>
#include <vector>
#include <stack>
int main() {
int colors[200]; // 0 = uncolored, 1 = red, 2 = black.
bool handled[200];
std::vector<int> adjacencyList[200];
while(true) {
int n, l;
std::cin >> n;
if(n == 0)
return 0;
for(int i = 0; i < n; ++i) {
colors[i] = 0;
handled[i] = false;
adjacencyList[i].clear();
}
colors[0] = 1;
std::cin >> l;
//std::cerr << "n: " << n << ", l:" << l << std::endl;
for(int i = 0; i < l; ++i) {
int a, b;
std::cin >> a >> b;
adjacencyList[a].push_back(b);
adjacencyList[b].push_back(a);
}
std::stack<int> s;
s.push(0);
bool notBicolorable = false;
while(!s.empty() && !notBicolorable) {
int t = s.top();
s.pop();
if(handled[t])
continue; // Already handled
for(std::vector<int>::iterator it = adjacencyList[t].begin(); it != adjacencyList[t].end(); ++it) {
int neighbour = *it;
if(colors[neighbour] == 0) {
colors[neighbour] = 3-colors[t];
s.push(neighbour);
}
else if(colors[neighbour] == colors[t]) {
std::cout << "NOT BICOLORABLE." << std::endl;
notBicolorable = true;
break;
}
} // for iterator
handled[t] = true;
} // while(!s.empty() && !done)
if(!notBicolorable)
std::cout << "BICOLORABLE." << std::endl;
} // while(true)
}