-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathUVa 11080 - Place the Guards.cpp
72 lines (65 loc) · 1.73 KB
/
UVa 11080 - Place the Guards.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
#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 T;
cin >> T;
while ( T-- )
{
int v, e;
cin >> v >> e;
vector<vector<int> > G(v);
vector<int> color(v, UNKNOWN);
bool isBipartite = true;
while ( e-- )
{
int f, t;
cin >> f >> t;
G[f].push_back(t);
G[t].push_back(f);
}
int totalCount = 0;
for (int j = 0; j < G.size() && isBipartite; ++j)
{
if (color[j] != UNKNOWN)
continue;
queue<int> q;
int count[2] = {0};
color[j] = BLACK;
++count[color[j]];
q.push(j);
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];
++count[color[v]];
q.push(v);
}
}
}
// Each connected component needs at least 1 guard.
totalCount += max(1, min(count[BLACK], count[WHITE]));
}
if (isBipartite)
cout << totalCount << endl;
else
cout << -1 << endl;
}
return 0;
}