1)按秩的大小来插。即

``````if( fx != fy){
//这里根据秩的大小来插入
if(rank[fx] > rank[fy]){
rank[fx] += rank[fy];
father[fy] = fx;
}else{
rank[fy] += rank[fx];
father[fx] = fy;
}
}``````

2)需要求出秩的最大值

``````int max = -1;
for( i = 1 ; i<= 10000001 ; ++i){
if(rank[i] > max){
max = rank[i];
}
}``````

``````/*
* 1856_2.cpp
*
*  Created on: 2013年8月23日
*/

#include <iostream>

using namespace std;

/**
* father[i] :用来保存当前节点的父亲节点
* rank[i] :用来保存当前节点的秩(即当前节点包含了多少节点)
*
*/
int father[10000001];
int rank[10000001];

int find(int a){
if( a == father[a]){
return a;
}

father[a] = find(father[a]);
return father[a];
}

void join(int x , int y){
int fx = find(x);
int fy = find(y);

if( fx != fy){
//这里根据秩的大小来插入
if(rank[fx] > rank[fy]){
rank[fx] += rank[fy];
father[fy] = fx;
}else{
rank[fy] += rank[fx];
father[fx] = fy;
}
}
}

void make_set(){
int i;
for( i = 1 ; i<= 10000001 ; ++i){
father[i] = i;
rank[i] = 1;
}
}

int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(rank,0,sizeof(rank));
memset(father,0,sizeof(father));
make_set();
int i;
for( i = 1 ; i <= n ; ++i ){
int a,b;
scanf("%d%d",&a,&b);
join(a,b);
}

//以下代码用来求秩的最大值
int max = -1;
for( i = 1 ; i<= 10000001 ; ++i){
if(rank[i] > max){
max = rank[i];
}
}
printf("%d\n",max);
}
}``````