-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuf.go
46 lines (41 loc) · 827 Bytes
/
uf.go
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
package algorithm
// UF 并查集结构
type UF struct {
collection []int
}
// NewUF 新建并查集
func NewUF(cnt int) *UF {
uf := &UF{
collection: make([]int, cnt),
}
for i := 0; i < cnt; i++ {
uf.collection[i] = i
}
return uf
}
// Connection i,j联通
func (uf *UF) Connection(i, j int) {
rootI, deepI := uf.root(i)
rootJ, deepJ := uf.root(j)
if deepI > deepJ {
uf.collection[j] = rootI
} else {
uf.collection[i] = rootJ
}
}
// IsConnection 判断i,j是否联通
func (uf *UF) IsConnection(i, j int) bool {
rootI, _ := uf.root(i)
rootJ, _ := uf.root(j)
return rootI == rootJ
}
func (uf *UF) root(i int) (int, int) {
root, deep := i, 1
for root != uf.collection[root] {
temp := root
root = uf.collection[root]
uf.collection[temp] = uf.collection[root]
deep++
}
return root, deep
}