forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoppers.cpp
66 lines (53 loc) · 1.23 KB
/
hoppers.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
#include <bits/stdc++.h>
using namespace std;
int find(vector<int>& d, int a) {
if(d[a] < 0) return a;
return d[a] = find(d, d[a]);
}
void join(vector<int>& d, int a, int b) {
a = find(d, a);
b = find(d, b);
if(a == b) return;
d[a] += d[b];
d[b] = a;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
vector<int> d(n, -1);
vector<pair<int,int>> edges;
for(int i = 0; i < m; i++) {
int n1, n2;
cin >> n1 >> n2;
n1--; n2--;
adj[n1].push_back(n2);
adj[n2].push_back(n1);
edges.push_back({n1,n2});
}
for(int i = 0; i < n; i++) {
for(int j = 1; j < adj[i].size(); j++) {
join(d, adj[i][j-1], adj[i][j]);
}
}
bool odd = false;
for(int i = 0; i < n; i++) {
if(d[i] < 0) {
for(auto j : adj[i]) {
if(find(d,i) == find(d,j)) {
odd = true;
}
}
}
}
for(auto i : edges) {
join(d,i.first,i.second);
}
int components = 0;
for(int i = 0; i < n; i++) {
if(d[i] < 0) components++;
}
components--;
if(!odd) components++;
cout << components << endl;
}