forked from algorithmzuo/algorithmbasic2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode05_UnionFind.java
More file actions
73 lines (64 loc) · 1.73 KB
/
Code05_UnionFind.java
File metadata and controls
73 lines (64 loc) · 1.73 KB
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
73
package class14;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
// 课上讲的并查集实现
// 请务必看补充的Code06_UnionFind
// 那是数组实现的并查集,并且有测试链接
// 可以直接通过
// 这个文件的并查集是用map实现的
// 但是笔试或者平时用的并查集一律用数组实现
// 所以Code06_UnionFind更具实战意义
// 一定要看!
public class Code05_UnionFind {
// 课上讲的时候
// 包了一层
// 其实不用包一层哦
public static class UnionFind<V> {
public HashMap<V, V> father;
public HashMap<V, Integer> size;
public UnionFind(List<V> values) {
father = new HashMap<>();
size = new HashMap<>();
for (V cur : values) {
father.put(cur, cur);
size.put(cur, 1);
}
}
// 给你一个节点,请你往上到不能再往上,把代表返回
public V findFather(V cur) {
Stack<V> path = new Stack<>();
while (cur != father.get(cur)) {
path.push(cur);
cur = father.get(cur);
}
while (!path.isEmpty()) {
father.put(path.pop(), cur);
}
return cur;
}
public boolean isSameSet(V a, V b) {
return findFather(a) == findFather(b);
}
public void union(V a, V b) {
V aFather = findFather(a);
V bFather = findFather(b);
if (aFather != bFather) {
int aSize = size.get(aFather);
int bSize = size.get(bFather);
if (aSize >= bSize) {
father.put(bFather, aFather);
size.put(aFather, aSize + bSize);
size.remove(bFather);
} else {
father.put(aFather, bFather);
size.put(bFather, aSize + bSize);
size.remove(aFather);
}
}
}
public int sets() {
return size.size();
}
}
}