unoin_find

  • 2022-12-14
  • 浏览 (586)

unoin_find.py 源码

# 并查集的代码模板

class UnionFind:

    def __init__(self, n: int):
        self.count = n
        self.parent = [i for i in range(n)]
    
    def find(self, p: int):
        temp = p
        while p != self.parent[p]:
            p = self.parent[p]
        while temp != self.parent[p]:
            temp, self.parent[temp] = self.parent[temp], p
        return p

    def union(self, p, q):
        pSet, qSet = self.find(p), self.find(q)
        if self.parent[pSet] != qSet:
            self.parent[pSet] = qSet
            self.count -= 1

你可能感兴趣的文章

algo-learn

contains_duplicate

container_with_most_water

0  赞