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 |
第一次就ac了爽死了!#include <stdio.h> #include <string.h> #define len 30010 int father[len],num[len]; void make_set(int n) { int i; for(i=0;i<n;i++) { father[i]=i; num[i]=1; } } int dfs(int x) { return x==father[x]?x:dfs(father[x]); } void union_set(int a,int b) { int x=dfs(a); int y=dfs(b); if(x!=y) { father[x]=y; num[y]+=num[x]; } } int sum() { int f=father[0]; while(f!=father[f]) f=father[f]; return num[f]; } int main() { int m,n,t,a,b; while(scanf("%d %d",&n,&m)!=EOF) { if(m==0&&n==0) break; make_set(n); while(m--) { scanf("%d",&t); if(t>=1) { scanf("%d",&a); t--; } while(t--) { scanf("%d",&b); union_set(a,b); } } printf("%d\n",sum()); } return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator