-
Notifications
You must be signed in to change notification settings - Fork 11
/
SPOJ3916.cc
68 lines (63 loc) · 1.43 KB
/
SPOJ3916.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
65
66
67
68
// SPOJ 3916: Bicolor
// http://www.spoj.com/problems/BICOLOR/
//
// Solution: Graph (bipartiteness test)
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;
#define ALL(c) c.begin(), c.end()
#define FOR(i,c) for(typeof(c.begin())i=c.begin();i!=c.end();++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define fst first
#define snd second
struct Graph {
int n;
vector< vector<int> > adj;
Graph(int n) : n(n), adj(n) { }
void addEdge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> depth;
bool visit(int u, int v) {
FOR(w, adj[v]) {
if (depth[*w] < 0) {
depth[*w] = depth[v] + 1;
if (!visit(v, *w)) return false;
} else {
if (*w != u && depth[*w] % 2 == depth[v] % 2)
return false;
}
}
return true;
}
bool isBipartite() {
depth.assign(n, -1);
REP(u, n) {
if (depth[u] < 0) {
depth[u] = 0;
if (!visit(-1, u)) return false;
}
}
return true;
}
};
int main() {
int n, m;
while (scanf("%d %d", &n, &m) == 2 && n != 0) {
Graph G(n);
REP(i,m) {
int u, v; scanf("%d %d", &u, &v);
G.addEdge(u, v);
}
if (G.isBipartite()) printf("BICOLORABLE\n");
else printf("NOT BICOLORABLE\n");
}
}