SiriBlog

siriyang的个人博客


  • 首页

  • 排行榜

  • 标签115

  • 分类37

  • 归档320

  • 关于

  • 搜索

算法常用模板:并查集

发表于 2020-07-19 更新于 2021-10-29 分类于 计算机 , 算法题 , 技术 , Java , C/C++ 阅读次数: Valine:
本文字数: 4k 阅读时长 ≈ 4 分钟

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <map>

using namespace std;

class UnionFindSet {

public:
//(key,value)表示,key的父节点,是value,(Data_A,Data_B)代表,Data_A的父节点是Data_B
map<int, int> fatherMap;
map<int, int> sizeMap;

UnionFindSet(vector<int> nodes) {
makeSets(nodes);
}

void makeSets(vector<int> nodes) {
fatherMap.clear();
sizeMap.clear();
for (int i=0;i< nodes.size();i++) {
fatherMap[nodes[i]]=nodes[i];
sizeMap[nodes[i]]=1;
}
}

int findHead(int node) {
int father = fatherMap[node];
if (father != node)
father = findHead(father);
fatherMap[node]= father;
return father;
}

bool isSameSet(int a, int b) {
return findHead(a) == findHead(b);
}

void Union(int a, int b) {
int aHead = findHead(a);
int bHead = findHead(b);
if (aHead != bHead) {
int aSetSize = sizeMap[aHead];
int bSetSize = sizeMap[bHead];
if (aSetSize <= bSetSize) {
fatherMap[aHead]=bHead;
sizeMap[bHead]=aSetSize + bSetSize;
} else {
fatherMap[bHead]= aHead;
sizeMap[aHead]= aSetSize + bSetSize;
}
}
}
};

int main(){
int n=10;
vector<int> ini;
for(int i=0;i<n;i++)
ini.push_back(i);

UnionFindSet fs(ini);

fs.Union(1,2);
cout<<fs.isSameSet(1,2)<<endl;

return 0;
}

Java实现:

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
66
67
68
69
70
71
72
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class UnionSet {
public static void main(String[] args) {
Data[] datas = new Data[5];
List<Data> list = new LinkedList<Data>();
for (int i = 0; i < 5; i++) {
datas[i] = new Data();
list.add(datas[i]);
}
UnionFindSet unionFindSet = new UnionFindSet(list);
unionFindSet.union(datas[1],datas[2]);
System.out.println(unionFindSet.isSameSet(datas[1],datas[2]));
}

public static class Data {

}

public static class UnionFindSet {
//(key,value)表示,key的父节点,是value,(Data_A,Data_B)代表,Data_A的父节点是Data_B
public HashMap<Data, Data> fatherMap;
public HashMap<Data, Integer> sizeMap;

public UnionFindSet(List<Data> nodes) {
fatherMap = new HashMap<Data, Data>();
sizeMap = new HashMap<Data, Integer>();
makeSets(nodes);
}

private void makeSets(List<Data> nodes) {
fatherMap.clear();
sizeMap.clear();
for (Data node : nodes) {
fatherMap.put(node, node);
sizeMap.put(node, 1);
}
}

private Data findHead(Data node) {
Data father = fatherMap.get(node);
if (father != node)
father = findHead(father);
fatherMap.put(node, father);
return father;
}

public boolean isSameSet(Data a, Data b) {
return findHead(a) == findHead(b);
}

public void union(Data a, Data b) {
if (a == null || b == null)
return;
Data aHead = findHead(a);
Data bHead = findHead(b);
if (aHead != bHead) {
int aSetSize = sizeMap.get(aHead);
int bSetSize = sizeMap.get(bHead);
if (aSetSize <= bSetSize) {
fatherMap.put(aHead, bHead);
sizeMap.put(bHead, aSetSize + bSetSize);
} else {
fatherMap.put(bHead, aHead);
sizeMap.put(aHead, aSetSize + bSetSize);
}
}
}
}
}
-------- 本文结束 感谢阅读 --------
相关文章
  • 寻找无向图最小着色方案
  • 《C语言程序设计(第五版)谭浩强》课后习题答案源码
  • C语言基础编程练习题
  • Bulls and Cows的Java求解过程遇到的问题
  • OpenCV3使用中遇到的一些问题
觉得文章写的不错的话,请我喝瓶怡宝吧!😀
SiriYang 微信支付

微信支付

SiriYang 支付宝

支付宝

  • 本文标题: 算法常用模板:并查集
  • 本文作者: SiriYang
  • 创建时间: 2020年07月19日 - 22时07分
  • 修改时间: 2021年10月29日 - 18时10分
  • 本文链接: https://blog.siriyang.cn/posts/20200719222037id.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
C/C++ 算法题 Java
NLP综合实践(一)
2020华为云大数据挑战赛-正式赛(2)
SiriYang

SiriYang

努力搬砖攒钱买镜头的摄影迷
320 日志
33 分类
88 标签
RSS
GitHub E-Mail
Creative Commons
Links
  • 友情链接
  • 作品商铺

蜀ICP备19008337号 © 2019 – 2025 SiriYang | 1.7m | 25:41
0%