前言:说实话,我当时学并查集的时候只学会了背模板、、然后今天遇到了可撤销的并查集还是在被模板,什么按秩合并、小挂大我也没时间研究了,所以这是一个板子嘿嘿嘿
我就弄明白了一点就是可撤销的并查集不可以路径压缩。。
size = [1] * (n + 1)
quiry = []
history = []
def findroot(x):
while p[x] != x:
x = p[x]
return x
def merge(x, y):
rootx = findroot(x)
rooty = findroot(y)
if rootx == rooty:
history.append(None)
return False
if size[rootx] < size[rooty]:
rootx, rooty = rooty, rootx
history.append((rooty, rootx, size[rootx]))
p[rooty] = rootx
size[rootx] += size[rooty]
return True
def undo():
if not history:return
op = history.pop()
if op == None:return
y, x, ori_size = op
p[y] = y
size[x] = ori_size

1071

被折叠的 条评论
为什么被折叠?



