Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
留下足迹#include<stdio.h> int p[30001]; int num[30001]; int find_set(int x) { if(x!=p[x]) { p[x]=find_set(p[x]); } return p[x]; } void union_set(int a,int b) { int x,y; x=find_set(a); y=find_set(b); if(x==y) return ; if(num[x]>num[y]) { p[y]=x; num[x]+=num[y]; } else { p[x]=y; num[y]+=num[x]; } } int main (void) { int n,m; int t; int x,y; int i; while(scanf("%d %d",&n,&m),n||m) { for(i=0;i<n;i++) { p[i]=i; num[i]=1; } for(i=1;i<=m;i++) { scanf("%d",&t); t--; scanf("%d",&x); while(t--) { scanf("%d",&y); union_set(x,y); } } x=find_set(0); printf("%d\n",num[x]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator