并查集(数组)笔记

没有进行路径压缩

#include <iostream>
#include <vector>
using namespace std;
typedef int Elemtype;
#define Maxsize 100

typedef struct {
	Elemtype data;
	int parent;
} Settype;
Settype s[10];

int Find(Settype s[], Elemtype x) {
	int i;
	for (i = 0; i < Maxsize && s[i].data != x; i++) ;
	if (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 = root1;
}

posted @ 2022-02-16 11:35  帝宝单推人!  阅读(33)  评论(0)    收藏  举报