-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10583.cpp
More file actions
46 lines (44 loc) · 1.08 KB
/
Copy path10583.cpp
File metadata and controls
46 lines (44 loc) · 1.08 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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (UVa) 10583
#include <bits/stdc++.h>
#define GET(x) scanf("%d", &x)
#define GED(x) scanf("%lf", &x)
typedef long long ll;
using namespace std;
typedef pair<int, int> ii;
struct UnionFind {
vector<int> p, rank;
UnionFind(int n) : p(n), rank(n, 0) { iota(p.begin(), p.end(), 0); }
int find(int i) { return p[i] == i ? i : (p[i] = find(p[i])); }
bool same(int i, int j) { return find(i) == find(j); }
void join(int i, int j) {
if (!same(i, j)) {
int x = find(i), y = find(j);
if (rank[x] < rank[y]) {
p[x] = y;
} else {
p[y] = x;
if (rank[x] == rank[y]) ++rank[x];
}
}
}
int countp() {
unordered_set<int> ps;
for (int i = 0; i < p.size(); ++i) ps.insert(find(i));
return (int)ps.size();
}
};
int main() {
int n, m;
for (int ca = 1; GET(n) && GET(m) && (n || m); ++ca) {
int a, b;
UnionFind uf(n);
for (int i = 0; i < m; ++i) {
GET(a);
GET(b);
uf.join(a - 1, b - 1);
}
printf("Case %d: %d\n", ca, uf.countp());
}
return 0;
}