Created
February 17, 2017 07:47
-
-
Save dada8397/e9e206c2c120d1723810e01698613067 to your computer and use it in GitHub Desktop.
UVa 10583 - Ubiquitous Religions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
using namespace std; | |
int p[50000], N; | |
int Find(int x) { | |
if(x == p[x]) return x; | |
return p[x] = Find(p[x]); | |
} | |
void Union(int x, int y) { | |
x = Find(x); | |
y = Find(y); | |
if(x != y) { | |
p[x] = y; | |
--N; | |
} | |
} | |
int main(void) { | |
int n, m, Case = 0; | |
while(scanf("%d %d", &n, &m) && (n || m)) { | |
N = n; | |
int x, y; | |
for(int i=1; i<=n; ++i) | |
p[i] = i; | |
for(int i=0; i<m; ++i) { | |
scanf("%d %d", &x, &y); | |
Union(x, y); | |
} | |
printf("Case %d: %d\n", ++Case, N); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment