Skip to content

Instantly share code, notes, and snippets.

@dada8397
Created February 17, 2017 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dada8397/e9e206c2c120d1723810e01698613067 to your computer and use it in GitHub Desktop.
Save dada8397/e9e206c2c120d1723810e01698613067 to your computer and use it in GitHub Desktop.
UVa 10583 - Ubiquitous Religions
#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