-
Notifications
You must be signed in to change notification settings - Fork 1
/
uva-10004.cpp
94 lines (74 loc) · 1.77 KB
/
uva-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
93
94
#include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define RED 1
#define BLUE 2
vector<vector<int> >adj;
vector<int>color;
queue<int>Q;
bool BiColorable(int source, int nodes);
void clearQ();
int main()
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int n, L, i, j, u, v;
while (scanf("%d", &n) && n != 0) {
scanf("%d", &L);
adj.assign(n, vector<int>());
for (i = 1; i <= L; i++) {
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
/*for (i = 0; i < n; i++) {
printf("%d -> ", i);
for (j = 0; j < adj[i].size(); j++) {
printf("%d ", adj[i][j]);
}
printf("\n");
}*/
bool possible = BiColorable(0, n);
if (possible) {
printf("BICOLORABLE.\n");
}
else {
printf("NOT BICOLORABLE.\n");
}
}
return 0;
}
bool BiColorable(int source, int nodes)
{
int u, v, i;
color.assign(nodes, WHITE);
clearQ();
Q.push(source);
color[source] = RED;
while (!Q.empty()) {
u = Q.front();
Q.pop();
for (i = 0; i < adj[u].size(); i++) {
v = adj[u][i];
if (color[v] == WHITE) {
if (color[u] == RED) {
color[v] = BLUE;
}
else {
color[v] = RED;
}
Q.push(v);
}
if (color[u] == color[v]) {
return false;
}
}
}
return true;
}
void clearQ()
{
while (!Q.empty()) {
Q.pop();
}
}