并查集(数组)笔记


没有进行路径压缩

#include
#include
using namespace std;
typedef int Elemtype;
typedef struct
{
	ElemType data;
	int parent;
}Settype;
Settype s[10];
int Find(Settype s[],Elemtype x)
{
	int i;
	for(i=0;i=Maxsize) return -1;
	for( ; s[i].parent>=0;i=s[i].parent);
	return i;
}
void Union(Settype s[],Elemtype x1,Elemtype x2)
{
	int root1,root2;
	root1=Find(s,x1);
	root2=Find(s,x2);
	if(root1!=root2) s[root2].parent=s[root1];
}