-
Notifications
You must be signed in to change notification settings - Fork 6
/
ABugsLife.cpp
79 lines (72 loc) · 1.87 KB
/
ABugsLife.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
#include <cstdio>
#include <vector>
#include <list>
#include <queue>
using namespace std;
class Graph {
long long V;
long long nEdges;
list<long long> *adj;
public:
Graph(long long V) : nEdges(0) {
this->V = V;
adj = new list<long long>[V];
}
void addEdge(long long v, long long w) {
adj[v].push_back(w);
adj[w].push_back(v);
nEdges++;
}
list<long long>& getNeighbours(long long v) { return adj[v]; }
long long getNodes() const { return V; }
long long getEdges() const { return nEdges; }
};
bool isBipartite(Graph& gg);
bool isBipartiteUtil(Graph& gg, vector<long long>& colors);
int main() {
long long T, nCase = 1;
scanf("%lld", &T);
while(T--) {
long long N, E;
scanf("%lld%lld", &N, &E);
Graph gg(N);
for(long long i = 0; i < E; i++) {
long long a, b;
scanf("%lld%lld", &a, &b);
gg.addEdge(a-1, b-1);
}
printf("Scenario #%lld:\n", nCase++);
if(!isBipartite(gg))
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
}
}
bool isBipartiteUtil(Graph& gg, vector<long long>& colors, long long src) {
queue<long long> qq;
colors[src] = 1;
qq.push(src);
while(!qq.empty()) {
long long curNode = qq.front();
qq.pop();
list<long long> curNeighbours = gg.getNeighbours(curNode);
list<long long>::iterator ii = curNeighbours.begin();
while(curNeighbours.end() != ii) {
if(colors[*ii] == -1) {
colors[*ii] = 1 - colors[curNode];
qq.push(*ii);
}
else if(colors[*ii] == colors[curNode])
return false;
ii++;
}
}
return true;
}
bool isBipartite(Graph& gg) {
vector<long long> colors(gg.getNodes(), -1);
for(long long i = 0; i < gg.getNodes(); i++)
if(colors[i] == -1 && isBipartiteUtil(gg, colors, i) == false)
return false;
return true;
}