More is better
#include<stdio.h> #include<stdlib.h> #include<string.h> #define maxn 10000010 int father[maxn],d[maxn]; int find(int x) { return x==father[x]?x:father[x]=find(father[x]); } void merge(int x,int y) { x=find(x),y=find(y); if(x!=y) father[x]=y; } int main( ) { int N,i,j,a,b,k,t=0; while(scanf("%d",&N)!=EOF) { if(N==0) {printf("1\n");continue;} //伤心啊,没考虑到这个特殊情况,让我错了好久,wa的好辛苦啊 memset(d,0,sizeof(d)); memset(father,0,sizeof(father)); for(i=1;i<=maxn;i++) father[i]=i; t=0; for(i=1;i<=N;i++) { scanf("%d%d",&a,&b); if(t<a) t=a; if(t<b) t=b; if(find(a)!=find(b)) merge(a,b); } for(i=1;i<=t;i++) { k=find(i); d[k]++; printf("%d %d\n",k,d[k]); } a=d[1]; for(i=2;i<=t;i++) if(d[i]>a) a=d[i]; printf("%d\n",a); } return 0; } // 2 4 1 4 3 1 5 3 6 7 7 8 100 3 20 100 100 2000 2000 100000
posted on 2011-04-30 23:51 more think, more gains 阅读(176) 评论(0) 编辑 收藏 举报