-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbipartiteGraph.cpp
More file actions
77 lines (57 loc) · 1.21 KB
/
bipartiteGraph.cpp
File metadata and controls
77 lines (57 loc) · 1.21 KB
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
// Note : Any graph with odd length cycles will not be bipartite
#include<iostream>
#include<vector>
#include<unordered_set>
using namespace std;
bool isBipartite(vector<int> *edges, int n) {
if(n == 0) {
return true;
}
unordered_set<int> sets[2];
vector<int> pending;
sets[0].insert(0);
pending.push_back(0);
while(pending.size() > 0) {
int current = pending.back();
pending.pop_back();
int currentSet = sets[0].count(current) > 0 ? 0 : 1;
for(int i = 0; i < edges[current].size(); i++) {
int neighbor = edges[current][i];
if(sets[0].count(neighbor) == 0 && sets[1].count(neighbor) == 0) {
sets[1 - currentSet].insert(neighbor);
pending.push_back(neighbor);
}
else if(sets[currentSet].count(neighbor) > 0) {
return false;
}
}
}
return true;
}
int main() {
int n;
while(true) {
cin >> n;
if(n == 0) {
break;
}
vector<int> *edges = new vector<int>[n];
int m;
cin >> m;
for(int i = 0; i < m; i++) {
int f, s;
cin >> f >> s;
edges[f].push_back(s);
edges[s].push_back(f);
}
bool ans = isBipartite(edges, n);
if(ans) {
cout << "Bicolourable" << endl;
}
else {
cout << "Not Bicolourable" << endl;
}
delete [] edges;
}
return 0;
}