C++實(shí)現(xiàn)并查集
本文實(shí)例為大家分享了C++實(shí)現(xiàn)并查集的具體代碼,供大家參考,具體內(nèi)容如下
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
class UnionFind{
private:
vector<int> parent;
int count;
//優(yōu)化,記錄p和q所在組的深度,在合并時(shí)將深度小的結(jié)點(diǎn)的根指向深度大的結(jié)點(diǎn)的根
vector<int> rank;
public:
UnionFind(int count){
parent.resize(count);
rank.resize(count);
this->count = count;
for(int i = 0; i < count; ++i){
parent[i] = i;
rank[i] = 1;
}
}
~UnionFind(){
parent.clear();
rank.clear();
}
//路徑壓縮
int find(int p){
assert(p >= 0 && p < count);
if(p != parent[p])
parent[p] = find(parent[p]);
return parent[p];
}
bool isConnected(int p, int q){
return find(p) == find(q);
}
void unionElement(int p, int q){
int pRoot = find(p), qRoot = find(q);
if(pRoot == qRoot)
return;
if(rank[pRoot] < rank[qRoot])
parent[pRoot] = qRoot;
else if(rank[qRoot] < rank[pRoot])
parent[qRoot] = pRoot;
else{
//兩者的rank相等
parent[pRoot] = qRoot;
rank[qRoot] += 1;
}
}
};
小編再補(bǔ)充一段代碼,之前收藏的一段代碼:
#include <iostream>
using namespace std;
class UF {
//cnt is the number of disjoint sets.
//id is an array that records distinct identity of each set,when two sets are merged ,their id will be same.
//sz is an array that records the child number of each set including the set self.
int *id, cnt, *sz;
public:
// Create an empty union find data structure with N isolated sets.
UF(int N) {
cnt = N;
id = new int[N];
sz = new int[N];
for (int i = 0; i<N; i++) {
id[i] = i;
sz[i] = 1;
}
}
~UF() {
delete[] id;
delete[] sz;
}
// Return the id of component corresponding to object p.
int find(int p) {
if (p != id[p]){
id[p] = find(id[p]);
}
return id[p];
}
// Replace sets containing x and y with their union.
void merge(int x, int y) {
int i = find(x);
int j = find(y);
if (i == j) return;
// make smaller root point to larger one
if (sz[i] < sz[j]) {
id[i] = j;
sz[j] += sz[i];
}
else {
id[j] = i;
sz[i] += sz[j];
}
cnt--;
}
// Are objects x and y in the same set?
bool connected(int x, int y) {
return find(x) == find(y);
}
// Return the number of disjoint sets.
int count() {
return cnt;
}
};
void main(){
UF test(5);
test.merge(2, 3);
test.merge(3, 4);
cout << test.find(4);
cout << test.count();
}
同時(shí)謝謝這位作者的分享
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言數(shù)組任意位置插入一個(gè)元素方法
這篇文章主要給大家分享C語(yǔ)言數(shù)組任意位置插入一個(gè)元素方法,2021-11-11
C++ Boost Lockfree超詳細(xì)講解使用方法
Boost是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱。Boost庫(kù)是一個(gè)可移植、提供源代碼的C++庫(kù),作為標(biāo)準(zhǔn)庫(kù)的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱2022-11-11
Matlab實(shí)現(xiàn)簡(jiǎn)易紀(jì)念碑谷游戲的示例代碼
《紀(jì)念碑谷》是USTWO公司開發(fā)制作的解謎類手機(jī)游戲,在游戲中,通過(guò)探索隱藏小路、發(fā)現(xiàn)視力錯(cuò)覺(jué)以及躲避神秘的烏鴉人來(lái)幫助沉默公主艾達(dá)走出紀(jì)念碑迷陣。本文將用Matlab編寫簡(jiǎn)易版的紀(jì)念碑谷游戲,感興趣的可以了解一下2022-03-03
C語(yǔ)言實(shí)現(xiàn)父進(jìn)程主動(dòng)終止子進(jìn)程的方法總結(jié)
一般的情況,子進(jìn)程自己運(yùn)行完后,執(zhí)行exit 或者return 后,父進(jìn)程wait. waitpid收回子進(jìn)程,但子進(jìn)程是一個(gè)循環(huán)等待狀態(tài)不主動(dòng)退出,父進(jìn)程可以采用文中介紹的幾種方法,需要的朋友可以參考下2023-10-10
C++中指針的數(shù)據(jù)類型和運(yùn)算相關(guān)知識(shí)小結(jié)
這篇文章主要介紹了C++中指針的數(shù)據(jù)類型和運(yùn)算相關(guān)知識(shí)小結(jié),是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
非常漂亮的新年祝福!C語(yǔ)言實(shí)現(xiàn)漂亮的煙花效果
非常漂亮的新年祝福!這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)漂亮的煙花效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02

