标签:
c.
/* 并查集模板 */ #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define MAXN 1024 int fa[MAXN]; int set_find(int d){ if(fa[d]<0)return d; return fa[d]=set_find(fa[d]); } void set_join(int x,int y){ x=set_find(x); y=set_find(y); if(x!=y)fa[x]=y; } int main(){ int n,m;//点数,边数 int x,y,i,sum; printf("输入点数、边数:"); while(~scanf("%d%d",&n,&m)){ memset(fa,-1,sizeof(fa)); printf("输入边:\n"); while(m--){ scanf("%d%d",&x,&y); set_join(x,y); } sum=0; for(i=1;i<=n;++i) if(fa[i]==-1)++sum; printf("集合个数:%d\n",sum); printf("输入点数、边数:"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/bofengyu/p/5365181.html