/* 并查集*/
typedef struct 4{
elementtype data;
int parent;
}settype;
/*查找某个元素所在的集合(用根节点表示)*/
int find (settype s[],elementtype x)
{//在数组S中查找值为X的元素所属的集合
// maxsize是全局变量,为数组S的最大长度
int i;
for(i=0;i<maxsize&&s[i].data!=x;i++);
if(i>=maxsize) return -1;//未找到X,返回-1
for(;s[i].parent>=0;i=s[i].parent);
return i;//找到X所属集合,返回树根节点在数组S中的下标
}
/*集合的并运算*/
/*分别找道X1和X2两个元素所在集合树的根节点
如果它们不同根,则将其中一个根节点的父节点指针设置成
另一个根节点的数组下标*/
void union(settype s[],elementtype x1,elementtype x2)
{
int root1,root2;
root1=find(s.x1);
root2=find(s,x2);
if(root1!=root2)
{
s[root2].parent=root1;
}
}