无权重并查集模板
解读:还有其他语言的也在解读链接中
- https://leetcode-cn.com/problems/number-of-provinces/solution/python-duo-tu-xiang-jie-bing-cha-ji-by-m-vjdr/
- https://leetcode-cn.com/problems/evaluate-division/solution/pythonbing-cha-ji-fu-mo-ban-by-milomusia-kfsu/
根节点的父节点可以用其本身表示,也可以用None表示,这里用的None表示
「路径压缩」的效果是:在查询一个结点 a 的根结点同时,把结点 a 到根结点的沿途所有结点的父亲结点都指向根结点。
class UnionFind:
def __init__(self):
"""
记录每个节点的父节点
"""
self.father = {}
def find(self,x):
"""
查找根节点
路径压缩
"""
root = x
while self.father[root] != None:
root = self.father[root]
# 路径压缩
while x != root:
original_father = self.father[x]
self.father[x] = root
x = original_father
return root
def merge(self,x,y,val):
"""
合并两个节点
"""
root_x,root_y = self.find(x),self.find(y)
if root_x != root_y:
self.father[root_x] = root_y
def is_connected(self,x,y):
"""
判断两节点是否相连
"""
return self.find(x) == self.find(y)
def add(self,x):
"""
添加新节点
"""
if x not in self.father:
self.father[x] = None
有权重并查集模板
class UnionFind:
def __init__(self):
"""
记录每个节点的父节点
记录每个节点到根节点的权重
"""
self.father = {}
self.value = {}
def find(self,x):
"""
查找根节点
路径压缩
更新权重
"""
root = x
# 节点更新权重的时候要放大的倍数
base = 1
while self.father[root] != None:
root = self.father[root]
base *= self.value[root]
while x != root:
original_father = self.father[x]
##### 离根节点越远,放大的倍数越高
self.value[x] *= base
base /= self.value[original_father]
#####
self.father[x] = root
x = original_father
return root
def merge(self,x,y,val):
"""
合并两个节点
"""
root_x,root_y = self.find(x),self.find(y)
if root_x != root_y:
self.father[root_x] = root_y
##### 四边形法则更新根节点的权重
self.value[root_x] = self.value[y] * val / self.value[x]
def is_connected(self,x,y):
"""
两节点是否相连
"""
return x in self.value and y in self.value and self.find(x) == self.find(y)
def add(self,x):
"""
添加新节点,初始化权重为1.0
"""
if x not in self.father:
self.father[x] = None
self.value[x] = 1.0
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
uf = UnionFind()
for (a,b),val in zip(equations,values):
uf.add(a)
uf.add(b)
uf.merge(a,b,val)
res = [-1.0] * len(queries)
for i,(a,b) in enumerate(queries):
if uf.is_connected(a,b):
res[i] = uf.value[a] / uf.value[b]
return res
例题
无权重
力扣990. 等式方程的可满足性
题解:https://blog.csdn.net/anan15151529/article/details/118416516
有权重
力扣399. 除法求值
题解:https://blog.csdn.net/anan15151529/article/details/118422831