/*并查集裸题*/
#include <cstdio>
#include <cstring>
#define MAXN 1001
int root[MAXN], hash[MAXN];
int get_root(int x)
{
if( root[x] == x ) {
return x;
}
return root[x] = get_root(root[x]);
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int vertex, arc, u, v, r_x, r_y, ans;
while( scanf("%d", &vertex) && vertex ) {
scanf("%d", &arc);
for(int i = 1; i <= vertex; i++) {
root[i] = i;
}
for(int i = 0; i < arc; i ++) {
scanf("%d %d", &u, &v);
r_x = get_root(u); r_y = get_root(v);
if( r_x != r_y ) {
root[r_x] = r_y;
}
}
ans = 0; memset(hash, 0, sizeof(hash));
for(int i = 1; i <= vertex; i ++) {
root[i] = get_root(i);
hash[root[i]] ++;
}
for(int i = 1; i <= vertex; i ++) {
if( hash[i] ) {
ans ++;
}
}
printf("%d\n", ans-1);
}
return 0;
}
hdu_1232
最新推荐文章于 2019-06-29 14:23:42 发布