C/C++实现并查集

 #include "pch.h"
#include 
using namespace std;

/*
*并查集
*/

int getRoot(int* parent, int len, int loc) {
	if (parent && len && loc > -1) {
		while (parent[loc] != loc) {
			parent[loc] = parent[parent[loc]];//路径压缩 对于并查集提高效率非常有效
			loc = parent[loc];
		}
		return loc;
	}
	throw new exception("out of range");
	return -1;
}

void quickUion(int* rank,int* parent, int len, int p, int q) {
	if (rank && parent && len && p > -1 && q > -1) {
		if (rank[p] > rank[q]) {
			parent[q] = getRoot(parent, len, p);
			rank[q] = rank[p];
		}
		else if (rank[p] < rank[q]) {
			parent[p] = getRoot(parent, len, q);
			rank[p] = rank[q];
		}
		else {
			parent[p] = getRoot(parent, len, q);
			rank[p]++;
			rank[q]++;
		}
	}
}

bool quickFind(int* rank, int* parent, int len, int p, int q) {
	if (rank && parent && len && p > -1 && q > -1) {
		return getRoot(parent, len, q) == getRoot(parent, len, p) ? true : false;
	}
	return false;
}

int main()
{
	int parent[26];
	int rank[26];
	for (int i = 0; i < 26; i++) {
		parent[i] = i;
		rank[i] = 1;
	}
	quickUion(rank, parent, 26, 1, 2);
	quickUion(rank, parent, 26, 3, 5);
	quickUion(rank, parent, 26, 1, 9);
	quickUion(rank, parent, 26, 1, 10);
	quickUion(rank, parent, 26, 10, 20);
	cout << quickFind(rank, parent, 26, 1, 22);

	return 0;
}

你可能感兴趣的:(算法题)