View Code
http://acm.hdu.edu.cn/showproblem.php?pid=1856
#include<iostream>
using namespace std;
#define M 10000001
int father[M];
int _max;
int sum[M];
int FindFa( int a )
{
if( father[a] == a )
{
return a;
}
return father[a] = FindFa( father[a] ); }
void UnionFa( int a , int b ) //将a,b合并
{
father[b] = a; //把a设为b的父结点
sum[a] += sum[b]; //当有元素加入a中时,使其个数加sum[b]个
if( sum[a] > _max )
{ _max = sum[a]; }
}
int main()
{
int a , b , n;
int i;
while( scanf( "%d" , &n ) != EOF )
{
_max = 1;
for( i = 0 ; i < M ; ++i )
{ father[i] = i; sum[i] = 1; }
for( i = 0 ; i < n ; ++i )
{
scanf( "%d %d" , &a , &b );
a = FindFa( a );
b = FindFa( b );
if( a != b )
{ UnionFa( a , b ); }
}
printf( "%d\n" , _max );
}
return 0;
}