class UnionFind:
def __init__(self):
"""
记录每个节点的父节点
"""
self.father = {
}
self.size = [1]
def find(self, x):
"""
查找根节点
路径压缩
"""
root
python 字典实现完整并查集的模板(包含路径压缩与基于size的优化)
最新推荐文章于 2024-04-09 10:30:00 发布
![](https://img-home.csdnimg.cn/images/20240611030827.png)