-
Notifications
You must be signed in to change notification settings - Fork 31
/
UnionFindWithDist.py
334 lines (271 loc) · 10.1 KB
/
UnionFindWithDist.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
带权并查集(维护到每个组根节点距离的并查集)
- 注意距离是`有向`的
例如维护和距离的并查集时,a->b 的距离是正数,b->a 的距离是负数
- 如果组内两点距离存在矛盾(沿着不同边走距离不同),那么在组内会出现正环
"""
# Class:
# UnionFindArrayWithDist1(n) : 数组实现的并查集,距离为加法.
# UnionFindArrayWithDist2(n) : 数组实现的并查集,距离为乘法.
# UnionFindMapWithDist1() : 字典实现的并查集,距离为加法.
# UnionFindMapWithDist2() : 字典实现的并查集,距离为乘法.
# API:
# Union(x,y,dist) : p(x) = p(y) + dist. 如果组内两点距离存在矛盾(沿着不同边走距离不同),返回false.
# Find(x) : 返回x所在组的根节点.
# Dist(x,y) : 返回x到y的距离.
# DistToRoot(x) : 返回x到所在组根节点的距离.
from collections import defaultdict
from typing import Callable, DefaultDict, List, Optional
class UnionFindArrayWithDist1:
"""维护到根节点距离的并查集.距离为加法."""
__slots__ = ("part", "_data", "_potential")
def __init__(self, n: int):
self.part = n
self._data = [-1] * n
self._potential = [0] * n
def union(
self, x: int, y: int, dist: int, cb: Optional[Callable[[int, int], None]] = None
) -> bool:
"""
p(x) = p(y) + dist.
如果组内两点距离存在矛盾(沿着不同边走距离不同),返回false.
"""
dist += self.distToRoot(y) - self.distToRoot(x)
x, y = self.find(x), self.find(y)
if x == y:
return dist == 0
if self._data[x] < self._data[y]:
x, y = y, x
dist = -dist
self._data[y] += self._data[x]
self._data[x] = y
self._potential[x] = dist
self.part -= 1
if cb is not None:
cb(y, x)
return True
def find(self, x: int) -> int:
if self._data[x] < 0:
return x
r = self.find(self._data[x])
self._potential[x] += self._potential[self._data[x]]
self._data[x] = r
return r
def dist(self, x: int, y: int) -> int:
"""返回x到y的距离`f(x) - f(y)`."""
return self.distToRoot(x) - self.distToRoot(y)
def distToRoot(self, x: int) -> int:
"""返回x到所在组根节点的距离`f(x) - f(find(x))`."""
self.find(x)
return self._potential[x]
def isConnected(self, x: int, y: int) -> bool:
return self.find(x) == self.find(y)
def getSize(self, x: int) -> int:
return -self._data[self.find(x)]
def getGroups(self) -> DefaultDict[int, List[int]]:
res = defaultdict(list)
for i in range(len(self._data)):
res[self.find(i)].append(i)
return res
class UnionFindArrayWithDist2:
"""维护到根节点距离的并查集.距离为乘法."""
__slots__ = ("part", "_data", "_potential")
def __init__(self, n: int):
self.part = n
self._data = [-1] * n
self._potential = [1.0] * n
def union(
self, x: int, y: int, dist: float, cb: Optional[Callable[[int, int], None]] = None
) -> bool:
"""
p(x) = p(y) * dist.
如果组内两点距离存在矛盾(沿着不同边走距离不同),返回false.
"""
dist *= self.distToRoot(y) / self.distToRoot(x)
x, y = self.find(x), self.find(y)
if x == y:
return dist == 1
if self._data[x] < self._data[y]:
x, y = y, x
dist = 1 / dist
self._data[y] += self._data[x]
self._data[x] = y
self._potential[x] = dist
self.part -= 1
if cb is not None:
cb(y, x)
return True
def find(self, x: int) -> int:
if self._data[x] < 0:
return x
r = self.find(self._data[x])
self._potential[x] *= self._potential[self._data[x]]
self._data[x] = r
return r
def dist(self, x: int, y: int) -> float:
"""返回x到y的距离`f(x)/f(y)`."""
return self.distToRoot(x) / self.distToRoot(y)
def distToRoot(self, x: int) -> float:
"""返回x到所在组根节点的距离`f(x)/f(find(x))`."""
self.find(x)
return self._potential[x]
def isConnected(self, x: int, y: int) -> bool:
return self.find(x) == self.find(y)
def getSize(self, x: int) -> int:
return -self._data[self.find(x)]
def getGroups(self) -> DefaultDict[int, List[int]]:
res = defaultdict(list)
for i in range(len(self._data)):
res[self.find(i)].append(i)
return res
def id(o: object) -> int:
if o not in _pool:
_pool[o] = len(_pool)
return _pool[o]
_pool = dict()
class UnionFindMapWithDist1:
"""维护到根节点距离的并查集.距离为加法."""
__slots__ = ("part", "_data", "_potential")
def __init__(self):
self.part = 0
self._data = dict()
self._potential = dict()
def union(
self, x: int, y: int, dist: int, cb: Optional[Callable[[int, int], None]] = None
) -> bool:
"""
p(x) = p(y) + dist.
!如果组内两点距离存在矛盾(沿着不同边走距离不同),返回false.
"""
dist += self.distToRoot(y) - self.distToRoot(x)
x, y = self.find(x), self.find(y)
if x == y:
return dist == 0
if self._data[x] < self._data[y]:
x, y = y, x
dist = -dist
self._data[y] += self._data[x]
self._data[x] = y
self._potential[x] = dist
self.part -= 1
if cb is not None:
cb(y, x)
return True
def find(self, x: int) -> int:
if x not in self._data:
self.add(x)
return x
if self._data[x] < 0:
return x
r = self.find(self._data[x])
self._potential[x] += self._potential[self._data[x]]
self._data[x] = r
return r
def dist(self, x: int, y: int) -> int:
"""返回x到y的距离`f(x) - f(y)`."""
return self.distToRoot(x) - self.distToRoot(y)
def distToRoot(self, x: int) -> int:
"""返回x到所在组根节点的距离`f(x) - f(find(x))`."""
self.find(x)
return self._potential[x]
def isConnected(self, x: int, y: int) -> bool:
return self.find(x) == self.find(y)
def getSize(self, x: int) -> int:
return -self._data[self.find(x)]
def getGroups(self) -> DefaultDict[int, List[int]]:
res = defaultdict(list)
for k in self._data:
res[self.find(k)].append(k)
return res
def add(self, x: int) -> "UnionFindMapWithDist1":
if x not in self._data:
self._data[x] = -1
self._potential[x] = 0
self.part += 1
return self
def __contains__(self, x: int) -> bool:
return x in self._data
def __repr__(self) -> str:
return "\n".join(f"{root}: {member}" for root, member in self.getGroups().items())
class UnionFindMapWithDist2:
"""维护到根节点距离的并查集.距离为乘法."""
__slots__ = ("part", "_data", "_potential")
def __init__(self):
self.part = 0
self._data = dict()
self._potential = dict()
def union(
self, x: int, y: int, dist: float, cb: Optional[Callable[[int, int], None]] = None
) -> bool:
"""
p(x) = p(y) * dist.
!如果组内两点距离存在矛盾(沿着不同边走距离不同),返回false.
"""
dist *= self.distToRoot(y) / self.distToRoot(x)
x, y = self.find(x), self.find(y)
if x == y:
return dist == 1
if self._data[x] < self._data[y]:
x, y = y, x
dist = 1 / dist
self._data[y] += self._data[x]
self._data[x] = y
self._potential[x] = dist
self.part -= 1
if cb is not None:
cb(y, x)
return True
def find(self, x: int) -> int:
if x not in self._data:
self.add(x)
return x
if self._data[x] < 0:
return x
r = self.find(self._data[x])
self._potential[x] *= self._potential[self._data[x]]
self._data[x] = r
return r
def dist(self, x: int, y: int) -> float:
"""返回x到y的距离`f(x)/f(y)`."""
return self.distToRoot(x) / self.distToRoot(y)
def distToRoot(self, x: int) -> float:
"""返回x到所在组根节点的距离`f(x)/f(find(x))`."""
self.find(x)
return self._potential[x]
def isConnected(self, x: int, y: int) -> bool:
return self.find(x) == self.find(y)
def getSize(self, x: int) -> int:
return -self._data[self.find(x)]
def getGroups(self) -> DefaultDict[int, List[int]]:
res = defaultdict(list)
for k in self._data:
res[self.find(k)].append(k)
return res
def add(self, x: int) -> "UnionFindMapWithDist2":
if x not in self._data:
self._data[x] = -1
self._potential[x] = 1.0
self.part += 1
return self
def __contains__(self, x: int) -> bool:
return x in self._data
def __repr__(self) -> str:
return "\n".join(f"{root}: {member}" for root, member in self.getGroups().items())
# https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_B&lang=ja
if __name__ == "__main__":
import sys
sys.setrecursionlimit(int(1e6))
input = lambda: sys.stdin.readline().rstrip("\r\n")
n, q = map(int, input().split())
uf = UnionFindMapWithDist1()
for _ in range(q):
op, *rest = map(int, input().split())
if op == 0:
x, y, w = rest
uf.union(y, x, w)
else:
x, y = rest
if not uf.isConnected(x, y):
print("?")
else:
print(uf.dist(y, x))