#define MAX_SIZE 1000
class Set
{
public:
int parent;
int rank;
Set()
{
parent = -1;
rank = 0;
}
};
Set set[MAX_SIZE];
int FindSet(int x)
{
int i,tmp;
for (i=x;set[i].parent>0;i=set[i].parent);
while (i!=x)//压缩路径
{
tmp = set[x].parent;
set[x].parent = i;
x = tmp;
}
return i;
}
int Union( int a,int b )
{
int rootA,rootB;
rootA = FindSet(a);
rootB = FindSet(b);
if (rootA == rootB)
return 0;//表示不用合并
if( set[a].parent < set[b].parent )//将小的合并到大的上面
{
set[rootA].parent += set[rootB].parent;
set[rootB].parent = rootA;
}
else
{
set[rootB].parent += set[rootA].parent;
set[rootA].parent = rootB;
}
}
class Set
{
public:
int parent;
int rank;
Set()
{
parent = -1;
rank = 0;
}
};
Set set[MAX_SIZE];
int FindSet(int x)
{
int i,tmp;
for (i=x;set[i].parent>0;i=set[i].parent);
while (i!=x)//压缩路径
{
tmp = set[x].parent;
set[x].parent = i;
x = tmp;
}
return i;
}
int Union( int a,int b )
{
int rootA,rootB;
rootA = FindSet(a);
rootB = FindSet(b);
if (rootA == rootB)
return 0;//表示不用合并
if( set[a].parent < set[b].parent )//将小的合并到大的上面
{
set[rootA].parent += set[rootB].parent;
set[rootB].parent = rootA;
}
else
{
set[rootB].parent += set[rootA].parent;
set[rootA].parent = rootB;
}
}