water~~~~
简单的并查集,查出有几个集合即可
#include<stdio.h> #define max(a,b) a>b?a:b #define min(a,b) a<b?a:b int father[10000001],vis[10000001],root[10000001]; void init(){ for(int i=1;i<=10000000;father[i]=i,vis[i++]=0); } int findfather(int x){ if(father[x]!=x)father[x]=findfather(father[x]); return father[x]; } void merge(int a,int b){ int x,y; x=findfather(a); y=findfather(b); if(x!=y)father[x]=y; } int main(){ int i,j,k,n,mini,maxi,a,b,x,y,cnt,maxsum; while(~scanf("%d",&n)){ if(n==0){ printf("1/n");continue; } init(); mini=99999999; maxi=-1; for(i=1;i<=n;i++){ scanf("%d%d",&a,&b); x=min(a,b); y=max(a,b); if(x<mini) mini=x; if(y>maxi) maxi=y; vis[a]=vis[b]=1; merge(a,b); } for(j=0,i=mini;i<=maxi;i++){ if(vis[i]==1 && findfather(i)==i) root[j++]=i; } maxsum=0; for(i=0;i<j;i++){ cnt=0; for(k=mini;k<=maxi;k++)if(father[k]==root[i])cnt++; if(cnt>maxsum)maxsum=cnt; } printf("%d/n",maxsum); } return 0; }