-
Notifications
You must be signed in to change notification settings - Fork 0
/
crime.cpp
64 lines (61 loc) · 1.18 KB
/
crime.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
/* Perfect Police Patrol System: color a graph with two colors. */
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
int
main(void)
{
int n, m;
while(scanf("%d %d", &n, &m) != EOF) {
int s = 0;
int c[1000] = {0};
vector<vector<int>> g;
for (int i = 0; i < n; i++)
g.push_back(vector<int> ());
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
g[a - 1].push_back(b - 1);
g[b - 1].push_back(a - 1);
}
/* BFS: color the connected components of the graph
* colors : 1, 2. Initialised at 0.
*/
for (int i = 0; i < n; i++) {
if (c[i] != 0)
continue;
int s1 = 0, s2 = 0;
queue<int> q;
vector<int> tmp;
tmp.push_back(i);
c[i] = 1;
q.push(i);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto w : g[v]) {
if (c[w] == 0) {
c[w] = (c[v] == 1 ? 2 : 1);
q.push(w);
tmp.push_back(w);
} else if (c[v] + c[w] != 3) {
goto err_impossible;
}
}
}
for (auto j : tmp) {
if (c[j] == 1)
s1++;
else
s2++;
}
s += ((s1 > s2) ? s2 : s1);
}
printf("%d\n", s);
continue;
err_impossible:
printf("Impossible\n");
}
return 0;
}