-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathUVa 10004 - Bicoloring.cpp
59 lines (55 loc) · 1.36 KB
/
UVa 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
static const int BLACK = 0;
static const int WHITE = 1;
static const int UNKNOWN = 2;
int main()
{
int n;
while (cin >> n, n != 0)
{
vector<vector<int> > G(n);
vector<int> color(n, UNKNOWN);
bool isBipartite = true;
int l;
cin >> l;
while ( l-- )
{
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
queue<int> q;
// Since the graph is strongly connected, we can reach all the
// vertices starting from any vertex.
color[0] = BLACK;
q.push(0);
while (!q.empty() && isBipartite)
{
int u = q.front();
q.pop();
for (int i = 0; i < G[u].size(); ++i)
{
int v = G[u][i];
if (color[v] == color[u])
{
isBipartite = false;
break;
}
else if (color[v] == UNKNOWN)
{
color[v] = 1 - color[u];
q.push(v);
}
}
}
if (isBipartite)
cout << "BICOLORABLE." << endl;
else
cout << "NOT BICOLORABLE." << endl;
}
return 0;
}