-
Notifications
You must be signed in to change notification settings - Fork 31
/
完全可持久化并查集-带size.go
209 lines (182 loc) · 4.6 KB
/
完全可持久化并查集-带size.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"bufio"
"fmt"
"os"
"runtime/debug"
)
func init() {
debug.SetGCPercent(-1)
}
func yosupo() {
// https://judge.yosupo.jp/submission/130167
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var n, q int
fmt.Fscan(in, &n, &q)
uf := NewPersistentUnionfind()
versions := make([]*AryNode, 0, q+10)
versions = append(versions, uf.Alloc())
for i := 0; i < q; i++ {
var op, version, u, v int
fmt.Fscan(in, &op, &version, &u, &v)
version++
root := versions[version]
if op == 0 {
newRoot, _ := uf.Union(root, u, v)
root = newRoot
} else {
if uf.IsConnected(root, u, v) {
fmt.Fprintln(out, 1)
} else {
fmt.Fprintln(out, 0)
}
}
versions = append(versions, root)
}
}
func UnionSets() {
// # https://atcoder.jp/contests/code-thanks-festival-2017-open/tasks/code_thanks_festival_2017_h
// # 给定n个集合,初始时第i个集合只有一个元素i (i=1,2,...,n)
// # 之后进行m次合并操作,每次合并ai和bi所在的集合
// # 如果ai和bi在同一个集合,则无事发生
// # 给定q个询问,问ai和bi是在第几次操作后第一次连通的,如果不连通则输出-1
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var n, m int
fmt.Fscan(in, &n, &m)
uf := NewPersistentUnionfind()
version := make([]*AryNode, 0, m+1)
version = append(version, uf.Alloc())
for i := 0; i < m; i++ {
var a, b int
fmt.Fscan(in, &a, &b)
a, b = a-1, b-1
newRoot, _ := uf.Union(version[i], a, b)
version = append(version, newRoot)
}
var q int
fmt.Fscan(in, &q)
for i := 0; i < q; i++ {
var a, b int
fmt.Fscan(in, &a, &b)
a, b = a-1, b-1
if !uf.IsConnected(version[m], a, b) {
fmt.Fprintln(out, -1)
continue
}
left, right := 0, m
for left <= right {
mid := (left + right) / 2
if uf.IsConnected(version[mid], a, b) {
right = mid - 1
} else {
left = mid + 1
}
}
fmt.Fprintln(out, left)
}
}
func main() {
// yosupo()
UnionSets()
}
type E = int
type AryNode struct {
data E
children [16]*AryNode // !
}
// 完全可持久化并查集,不使用路径压缩.
type PersistentUnionfind struct {
parents *PersistentArray
}
func NewPersistentUnionfind() *PersistentUnionfind {
return &PersistentUnionfind{parents: NewPersistentArray(-1)}
}
func (p *PersistentUnionfind) Alloc() *AryNode {
return p.parents.Alloc()
}
func (p *PersistentUnionfind) Union(root *AryNode, x, y int) (newRoot *AryNode, ok bool) {
x, y = p.Find(root, x), p.Find(root, y)
if x == y {
return root, false
}
p1, p2 := p.parents.Get(root, x), p.parents.Get(root, y)
if -p1 < -p2 {
x, y = y, x
}
newSize := p1 + p2
root = p.parents.Set(root, x, newSize)
root = p.parents.Set(root, y, x)
newRoot, ok = root, true
return
}
func (p *PersistentUnionfind) Find(root *AryNode, x int) int {
for {
p := p.parents.Get(root, x)
if p < 0 {
break
}
x = p
}
return x
}
func (p *PersistentUnionfind) IsConnected(root *AryNode, x, y int) bool {
return p.Find(root, x) == p.Find(root, y)
}
func (p *PersistentUnionfind) GetSize(root *AryNode, x int) int {
return -p.parents.Get(root, p.Find(root, x))
}
type PersistentArray struct {
null E // 当index越界/不存在时返回的值
}
func NewPersistentArray(null E) *PersistentArray {
return &PersistentArray{null: null}
}
func (o *PersistentArray) Alloc() *AryNode {
return &AryNode{data: o.null}
}
func (o *PersistentArray) Build(nums []E) *AryNode {
root := o.Alloc()
for i := 0; i < len(nums); i++ {
root = o.setWithoutCopy(root, i, nums[i])
}
return root
}
func (o *PersistentArray) Get(root *AryNode, index int) E {
if root == nil {
return o.null
}
if index == 0 {
return root.data
}
return o.Get(root.children[index&15], (index-1)>>4)
}
func (o *PersistentArray) Set(root *AryNode, index int, data E) *AryNode {
newNode := o.Alloc()
if root != nil { // copyNode
newNode.data = root.data
for i := 0; i < 16; i++ {
newNode.children[i] = root.children[i]
}
}
if index == 0 {
newNode.data = data
return newNode
}
newNode.children[index&15] = o.Set(newNode.children[index&15], (index-1)>>4, data)
return newNode
}
func (o *PersistentArray) setWithoutCopy(root *AryNode, index int, data E) *AryNode {
if root == nil {
root = o.Alloc()
}
if index == 0 {
root.data = data
return root
}
root.children[index&15] = o.setWithoutCopy(root.children[index&15], (index-1)>>4, data)
return root
}