Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 36417 | Accepted: 17681 |
Description
Input
Output
Sample Input
100 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2 200 2 1 5 5 1 2 3 4 5 1 0 0 0
Sample Output
4 1 1
Source
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int pre[30010],num[30010]; int findn(int x) { if(x!=pre[x]) pre[x] = findn(pre[x]); return pre[x];//注意这里是pre[x] } void join(int x,int y) { int fx=findn(x),fy=findn(y); if(fx!=fy) { if(num[fx]>=num[fy]) { pre[fy] = fx; num[fx] = num[fx] + num[fy];//跟新num } else { pre[fx] = fy; num[fy] = num[fy] + num[fx]; } } } int main() { int n,m; while(~scanf("%d %d",&n,&m)) { if(n==0&&m==0) break; for(int i=0;i<n;i++) pre[i]=i,num[i]=1;//先初始化 while(m--) { int a,b; scanf("%d %d",&a,&b); for(int i=1;i<a;i++) { int c; scanf("%d",&c); join(b,c);//以每组第一个建立一个小组集合 } } printf("%d\n",num[pre[0]]); } return 0; }
原文:http://www.cnblogs.com/l609929321/p/6556909.html