-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10583 Ubiquitous Religions.cpp
64 lines (61 loc) · 1.09 KB
/
10583 Ubiquitous Religions.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
#include <bits/stdc++.h>
using namespace std;
int par[50001] , ran[50001];
void built (int n)
{
for(int i = 1; i <= n; i++)
{
ran[i] = 0;
par[i] = i;
}
}
int make_friend (int n)
{
if(par[n] == n)
return n;
else
return par[n] = make_friend(par[n]);
}
void Union(int a , int b)
{
if(ran[a] > ran[b])
{
par[b] = a;
}
else
{
par[a] = b;
if(ran[a] == ran[b])
ran[b]++;
}
}
int main()
{
int n , m, o = 0;
while(scanf("%d %d", &n , &m))
{
o++;
if(m == 0 && n == 0)
break;
built(n);
while(m --)
{
int a, b;
cin >> a >> b;
int pa , pb;
pa = make_friend(a);
pb = make_friend(b);
if(pa != pb)
Union(pa, pb);
}
int cnt = 0;
set <int> st;
for(int i = 1; i <= n; i++)
{
int x = make_friend(i);
st.insert(x);
}
printf("Case %d: %d\n",o, st.size());
st.clear();
}
}