QuickFind的并查集

class UnionFind:
    def __init__(self,size: int):
        self.root = [i for i in range(size)]
    def find(self,x: int):
        return self.root[x]
    def union(self,x: int,y: int):
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX != rootY:
            for i in range(len(self.root)):
                if self.root[i] == rootY:
                    self.root[i] = rootX
    def connected(self,x: int,y: int):
        return self.find(x) == self.find(y)

QuickUnion的并查集

class UnionFind:
    def __init__(self,size: int):
        self.root = [i for i in range(size)]
    def find(self,x: int):
        while x!= self.root[x]:
            x = self.root[x]
        return x
    def union(self,x: int,y: int):
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX != rootY:
            self.root[rootY] = rootX 
    def connected(self,x: int,y: int):
        return self.find(x) == self.find(y)

按秩合并

class UnionFind:
    def __init__(self,size: int):
        self.root = [i for i in range(size)]
        self.rank = [1]*size
    def find(self,x: int):
        while x!= self.root[x]:
            x = self.root[x]
        return x
    def union(self,x: int,y: int):
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX != rootY:
            if self.rank[rootX] > self.rank[rootY]:
                self.root[rootY] = rootX
            elif self.rank[rootX] < self.rank[rootY]:
                self.root[rootX] = rootY
            else:
                self.root[rootY] = rootX
                self.rank[rootX]+=1
    def connected(self,x: int,y: int):
        return self.find(x) == self.find(y)

路径压缩

class UnionFind:
    def __init__(self,size: int):
        self.root = [i for i in range(size)]
    def find(self,x: int):
        if x == self.root[x]:
            return x
        self.root[x] = self.find(self.root[x])
        return self.root[x]
    def union(self,x: int,y: int):
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX != rootY:
            self.root[rootY] = rootX 
    def connected(self,x: int,y: int):
        return self.find(x) == self.find(y)
上一篇 下一篇