-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaceTheGuards_UVA11080OJ.cpp
109 lines (98 loc) · 2.84 KB
/
placeTheGuards_UVA11080OJ.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <queue>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int testCase; cin >> testCase;
for (int c = 1; c <= testCase; ++c)
{
int v, e, totalApart = 0;
cin >> v >> e;
vector<vector<bool>> a(v, vector<bool>(v, false));
vector<int> color(v, -1);
for (int k = 1; k <= e; ++k)
{
int f, t; cin >> f >> t;
a[f][t] = true;
a[t][f] = true;
}
if (e == 0)
{
cout << v << '\n';
continue;
}
int total0 = 0, totalAparts = 0;
bool isBicolorable = true;
for (int k = 0; k <= v - 1; ++k)
{
if (color[k] == -1)
{
int tempTotal0 = 0, maxDeepness = 1;
queue<int> q;
q.push(k);
color[k] = 0;
++tempTotal0;
while (isBicolorable == true && !(q.empty()))
{
int temp = q.size();
int s = q.front();
q.pop();
for (int i = 0; i <= v - 1; ++i)
{
if (a[s][i] == true)
{
if (color[i] == -1)
{
q.push(i);
++maxDeepness;
if (color[s] == 0)
{
color[i] = 1;
}
else
{
color[i] = 0;
++tempTotal0;
}
}
else if (color[s] == color[i])
{
isBicolorable = false;
break;
}
}
}
}
if (maxDeepness == 1)
{
++totalAparts;
}
else
{
total0 += min(maxDeepness - tempTotal0, tempTotal0);
}
}
}
if (isBicolorable == true)
{
cout << totalAparts + min(v - total0, total0) << '\n';
}
else
{
cout << "-1\n";
}
}
cout.flush();
return 0;
}