-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSPOJ3377.cc
64 lines (58 loc) · 1.37 KB
/
SPOJ3377.cc
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
// SPOJ 3377: A Bug’s Life
// http://www.spoj.com/problems/BUGLIFE/
//
// Solution: bipartiteness
//
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <iterator>
#include <functional>
#include <algorithm>
using namespace std;
struct edge { int src, dst; };
struct graph {
int n;
vector<vector<edge> > adj;
graph(int n = 0) : n(n), adj(n) { }
void add_edge(int src, int dst) {
n = max(n, max(src, dst)+1);
adj.resize(n);
adj[src].push_back((edge){src, dst});
adj[dst].push_back((edge){dst, src});
}
vector<int> color;
bool dfs(int i, int c) {
color[i] = c;
for (auto e: adj[i]) {
if (color[e.dst] == c) return false;
if (color[e.dst] == -1 && !dfs(e.dst, !c)) return false;
}
return true;
}
bool is_bipartite() {
color.assign(n, -1);
for (int i = 0; i < n; ++i)
if (color[i] == -1)
if (!dfs(i, 0)) return false;
return true;
}
};
int main() {
int ncase;
scanf("%d", &ncase);
for (int icase = 0; icase < ncase; ++icase) {
printf("Scenario #%d:\n", icase+1);
int n, m;
scanf("%d %d", &n, &m);
graph g(n);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
g.add_edge(u-1, v-1);
}
if (g.is_bipartite()) printf("No suspicious bugs found!\n");
else printf("Suspicious bugs found!\n");
}
}