-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva11080.cpp
More file actions
83 lines (77 loc) · 2.07 KB
/
uva11080.cpp
File metadata and controls
83 lines (77 loc) · 2.07 KB
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
73
74
75
76
77
78
79
80
81
82
83
/* Rishikesh
* UVA 11080 Place the guards
*/
#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<iomanip>
#include<cstdint>
#include<numeric>
#include<stack>
#include<queue>
#include<list>
#include<deque>
#include<bitset>
#include<functional>
using namespace std;
bool isBicolorable(int node, vector<int> &visited,
vector<vector<int>> &adjlist, int color,
int &numnodes,
int &numones) {
numnodes++; // count number of nodes in the component
if (color == 1) numones++; // count number of nodes colored 1.
visited[node] = color;
int neighcolor = (color == 1 ? 2 : 1);
bool ans = true;
for (int n : adjlist[node]) {
if (visited[n]) {
if (neighcolor == visited[n])
continue;
else
return false;
} else {
if (not (ans = ans and isBicolorable(n, visited, adjlist, neighcolor, numnodes, numones)))
break;
}
}
return ans;
}
int main() {
int T;
cin >> T;
while(T--) {
int v,e;
cin >> v >> e;
vector<vector<int>> adjlist(v);
for(int i = 0; i < e; i++) {
int f, t;
cin >> f >> t;
adjlist[f].push_back(t);
adjlist[t].push_back(f);
}
bool ans = true;
vector<int> visited(v, 0);
int nguards = 0;
for (int i = 0; ans and i < v; i++) {
if ( not visited[i]) { // This true once per component
int numnodes = 0, numones = 0;
ans = ans and isBicolorable(i, visited, adjlist, 1, numnodes, numones);
// Add minimum number of guards needed for this component
if (numnodes > 1 and numones > numnodes/2)
nguards += (numnodes - numones);
else
nguards += numones;
}
}
if (ans) {
cout << nguards << endl;
} else {
cout << -1 << endl;
}
}
}