-
Notifications
You must be signed in to change notification settings - Fork 1
/
uva_11080.cpp
88 lines (78 loc) · 1.66 KB
/
uva_11080.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
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = a, _b = b; i < _b; ++i)
#define FOR(i, a, b) for(int i = a, _b = b; i <= b; ++i)
#define FORD(i, a, b) for(int i = a, _b = b; i >= b; --i)
#define endl "\n"
#define newline cout << "\n"
#define puts(_xxx_) cout << _xxx_ << "\n"
#define db1(x) cout << #x << " = " << x << "\n"
#define db2(a, i) cout << #a << "[" << i << "] = " << a[i] << "\n"
using namespace std;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef long long llong;
const int MX = 202;
int V, E;
vector<int> adj[MX];
int color[MX];
bool possible;
int bfs (int start) {
queue<int> que;
que.push(start);
color[start] = 0;
int cnt[2] = {1, 0};
vector<int> visited(V, false);
while(!que.empty() && possible) {
int u = que.front(); que.pop();
if(visited[u]) continue;
visited[u] = true;
for(int v: adj[u]) {
if(color[v] == color[u]) {
possible = false;
break;
}
if(color[v] == -1) {
color[v] = 1 - color[u];
cnt[color[v]]++;
que.push(v);
}
}
}
return max(1, min(cnt[0], cnt[1]));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifndef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T;
cin >> T;
while(T--) {
cin >> V >> E;
REP(v, 0, V) {
adj[v].clear();
color[v] = -1;
}
int u, v;
REP(i, 0, E) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
possible = true;
int cnt = 0;
REP(v, 0, V) {
if(color[v] == -1) {
cnt += bfs(v);
}
if(!possible) {
cnt = -1;
break;
}
}
puts(cnt);
}
return 0;
}