标签:des style io color ar os sp java for
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
4 2HintA and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers./*两个朋友合并和的时候,需要将他们每个人的朋友数也一起合并。 */ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define M 10000005 int pre[M]; int sum[M]; int k; int find(int N) { return pre[N]==N?N:pre[N]=find(pre[N]); } void join(int x,int y) { int fx=find(x),fy=find(y); if(fx==fy) return; if(fx!=fy) { if(sum[fx]<sum[fy]) { pre[fx]=fy; sum[fy]+=sum[fx]; k=max(k,sum[fy]); } else { pre[fy]=fx; sum[fx]+=sum[fy]; k=max(k,sum[fx]); } } } int main() { int i,n,a,b; while(scanf("%d",&n)!=EOF) { for(i=1;i<M;i++)//sum[M]初始化均为1; { pre[i]=i; sum[i]=1; } if(n==0) printf("1\n"); else { k=0; while(n--){ scanf("%d %d",&a,&b); join(a,b);} printf("%d\n",k); } } return 0; }
标签:des style io color ar os sp java for
原文地址:http://blog.csdn.net/hdd871532887/article/details/41154365