news 2026/4/27 6:31:53

C++实现并查集

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++实现并查集

本文实例为大家分享了C++实现并查集的具体代码,供大家参考,具体内容如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

#include <iostream>

#include <vector>

#include <cassert>

usingnamespacestd;

classUnionFind{

private:

vector<int> parent;

intcount;

//优化,记录p和q所在组的深度,在合并时将深度小的结点的根指向深度大的结点的根

vector<int> rank;

public:

UnionFind(intcount){

parent.resize(count);

rank.resize(count);

this->count = count;

for(inti = 0; i < count; ++i){

parent[i] = i;

rank[i] = 1;

}

}

~UnionFind(){

parent.clear();

rank.clear();

}

//路径压缩

intfind(intp){

assert(p >= 0 && p < count);

if(p != parent[p])

parent[p] = find(parent[p]);

returnparent[p];

}

boolisConnected(intp,intq){

returnfind(p) == find(q);

}

voidunionElement(intp,intq){

intpRoot = find(p), qRoot = find(q);

if(pRoot == qRoot)

return;

if(rank[pRoot] < rank[qRoot])

parent[pRoot] = qRoot;

elseif(rank[qRoot] < rank[pRoot])

parent[qRoot] = pRoot;

else{

//两者的rank相等

parent[pRoot] = qRoot;

rank[qRoot] += 1;

}

}

};

小编再补充一段代码,之前收藏的一段代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

#include <iostream>

usingnamespacestd;

classUF {

//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(intN) {

cnt = N;

id =newint[N];

sz =newint[N];

for(inti = 0; i<N; i++) {

id[i] = i;

sz[i] = 1;

}

}

~UF() {

delete[] id;

delete[] sz;

}

// Return the id of component corresponding to object p.

intfind(intp) {

if(p != id[p]){

id[p] = find(id[p]);

}

returnid[p];

}

// Replace sets containing x and y with their union.

voidmerge(intx,inty) {

inti = find(x);

intj = 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?

boolconnected(intx,inty) {

returnfind(x) == find(y);

}

// Return the number of disjoint sets.

intcount() {

returncnt;

}

};

voidmain(){

UF test(5);

test.merge(2, 3);

test.merge(3, 4);

cout << test.find(4);

cout << test.count();

}

同时谢谢这位作者的分享

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/27 6:31:38

Phi-3.5-mini-instruct辅助STM32CubeMX配置:根据需求生成初始化代码

Phi-3.5-mini-instruct辅助STM32CubeMX配置&#xff1a;根据需求生成初始化代码 1. 嵌入式开发的新助手 最近在STM32开发社区里&#xff0c;有个话题越来越热&#xff1a;怎么让AI帮我们更快完成外设配置。传统方式下&#xff0c;开发者需要在STM32CubeMX里手动点选各种参数&…

作者头像 李华
网站建设 2026/4/27 6:29:28

AI如何教会自己从错误中学习,无需任何外部老师的指导

这项由普林斯顿大学、多伦多大学和卡内基梅隆大学联合开展的研究&#xff0c;于2026年4月以预印本形式发布&#xff0c;论文编号为arXiv:2604.12002v1。研究提出了一种名为"自蒸馏零"&#xff08;SD-ZERO&#xff09;的训练方法&#xff0c;核心问题是&#xff1a;一…

作者头像 李华
网站建设 2026/4/27 6:29:27

如何理解低代码平台:可视化开发趋势的终极指南

如何理解低代码平台&#xff1a;可视化开发趋势的终极指南 【免费下载链接】interview &#x1f4da; C/C 技术面试基础知识总结&#xff0c;包括语言、程序库、数据结构、算法、系统、网络、链接装载库等知识及面试经验、招聘、内推等信息。This repository is a summary of t…

作者头像 李华
网站建设 2026/4/27 6:27:17

plumber:消息系统的瑞士军刀CLI工具完整指南

plumber&#xff1a;消息系统的瑞士军刀CLI工具完整指南 【免费下载链接】plumber A swiss army knife CLI tool for interacting with Kafka, RabbitMQ and other messaging systems. 项目地址: https://gitcode.com/gh_mirrors/pl/plumber plumber 是一款功能强大的命…

作者头像 李华
网站建设 2026/4/27 6:25:26

23 ComfyUI 实战:AnimateDiff + OpenPose Walking 姿态驱动视频生成

ComfyUI 实战&#xff1a;AnimateDiff OpenPose Walking 姿态驱动视频生成 摘要 在姿态驱动视频生成任务中&#xff0c;动作控制是否准确&#xff0c;决定了整条生成链路是否具有实际价值。相比人物外观、场景细节和画面风格&#xff0c;动作是否被正确执行更适合作为首要验…

作者头像 李华
网站建设 2026/4/27 6:22:35

BetterJoy:让Switch手柄成为你的跨平台游戏控制器终极方案

BetterJoy&#xff1a;让Switch手柄成为你的跨平台游戏控制器终极方案 【免费下载链接】BetterJoy Allows the Nintendo Switch Pro Controller, Joycons and SNES controller to be used with CEMU, Citra, Dolphin, Yuzu and as generic XInput 项目地址: https://gitcode.…

作者头像 李华