hdoj1856

More is better


/***************************************************************\
*Author: 小呼
*Created Time: Mon 12 Jul 2010 09:21:56 AM CST
*File Name: a.cpp
*Description://并查集,增加信心的题目
\***************************************************************/

//*========================*Head File*========================*\\

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*----------------------*Global Variable*----------------------*/
int
frd[
10000001];
int
A,B,n;

//*=======================*Main Program*=======================*//
using namespace std;

int
find(int x){
if
(frd[x]>
0)
frd[x]=find(frd[x]);
return
frd[x]>
0?frd[x]:x;
}

void
union_set(int x,int y){
x=find(x);y=find(y);
if
(find(x)<find(y)){
frd[x]+=frd[y];
frd[y]=x;
}

else
{
frd[y]+=frd[x];
frd[x]=y;
}
}


int
main(){

//freopen("input","r",stdin);
while(scanf("%d",&n)!=EOF){
int
res=-
1;
memset(frd,-
1,sizeof(frd));
for
(int i=
1;i<=n;++i){
scanf(
"%d%d",&A,&B);
A=find(A);
B=find(B);
if
(A!=B){
int
temp=frd[A]+frd[B];
res=res<temp?res:temp;
union_set(A,B);
}
}

printf(
"%d\n",-1*res);
}

return
0;
}

你可能感兴趣的:(OJ)