-
Notifications
You must be signed in to change notification settings - Fork 0
/
10004 - Bicoloring.cpp
74 lines (56 loc) · 1.37 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
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
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
bool visited = false;
bool color = false;
vector<Node*> children;
};
bool isBicorolable(vector<Node*> &nodes);
int main() {
int n, l;
cin >> n;
while (n != 0) {
cin >> l;
vector<Node*> nodes;
nodes.reserve(n);
for (int i = 0; i < n; i++) {
nodes[i] = new Node;
}
while (l--) {
int x, y;
cin >> x >> y;
nodes[x]->children.push_back(nodes[y]);
nodes[y]->children.push_back(nodes[x]);
}
if (isBicorolable(nodes)) {
cout << "BICOLORABLE." << endl;
} else {
cout << "NOT BICOLORABLE." << endl;
}
for (int i = 0; i < n; i++) {
delete nodes[i];
}
cin >> n;
}
return 0;
}
bool isBicorolable(vector<Node*> &nodes) {
queue<Node*> q;
q.push(nodes[0]);
nodes[0]->visited = true;
while (!q.empty()) {
Node *node = q.front();
q.pop();
for (auto child : node->children) {
if (child->visited && child->color == node->color)
return false;
if (!child->visited) {
child->visited = true;
child->color = !node->color;
q.push(child);
}
}
}
return true;
}