poj1161
简单的。。。一遍。。。
#include<stdio.h> #define N 30000+5 int n,m,ans;//n个人,m个团体 int father[N]; int sum[N]; int find_f(int x){ if(father[x]==x) return x; else { father[x]=find_f(father[x]); } return father[x]; } void u(int x,int y){ int fx,fy; fx=find_f(x); fy=find_f(y); if(fx==fy) return ; if(sum[fx]>=sum[fy]){ father[fy]=fx; sum[fx]+=sum[fy]; } else{ father[fx]=fy; sum[fy]+=sum[fx]; } return ; } int main(){ int i,j,k,t; int groupnum,first,next; while(scanf("%d%d",&n,&m),n+m){ for(i=0;i<n;i++){ father[i]=i; sum[i]=1; } ans=0; while(m--){ scanf("%d",&groupnum); scanf("%d",&first); for(i=0;i<groupnum-1;i++){ scanf("%d",&next); u(first,next); } } /*for(i=0;i<n;i++){ if(find_f(i)==find_f(0)) ans++; } printf("%d\n",ans);*/ printf("%d\n",sum[find_f(0)]);//注释里的方法也可以 } return 0; }
AC
keep moving...