链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
代码
#include<stdio.h> #include<stdlib.h> #include<algorithm> #include<iostream> using namespace std; int p[1010]; int getfather(int n) { while(n!=p[n]) n=p[n]; return n; } void Union(int x,int y) { int rootx=getfather(x); int rooty=getfather(y); if(rootx!=rooty) p[rooty]=rootx; } void inti(int n) { for(int i=1;i<=n;i++) p[i]=i; } int main() { int T,m,n,i; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); inti(n); int a,b; for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); Union(a,b); } int c=0; for(i=1;i<=n;i++) if(p[i]==i) c++; printf("%d\n",c); getchar(); // 这一步比较关键,我开始一直没注意到,无线wa,坑爹啊。 } return 0; }
这一题也是并查集的问题。
最基本的三个函数:getfather,Union,inti;
然后在主程序中判断,要是p[i]=i,那么他就是根节点,
根节点的个数就是集合的个数。