+ """union-find with union-by-rank and path compression"""
+ def __init__(self,cap=100):
+ """creates a disjoint forest with the given capacity"""
+ self.__parent = [ i for i in range(cap) ]
+ self.__rank = [ 0 for i in range(cap) ]
+ """return the string representation of the disjoint forest"""
+ return str(self.__parent)
+ """return the length of the disjoint forest"""
+ return len(self.__parent)
+ """return the representative of x in the disjoint forest"""
+ self.__parent[x] = ans = self.find(ans)
+ """union of the trees of x and y"""
+ rx,ry = self.find(x),self.find(y)
+ kx,ky = self.__rank[rx],self.__rank[ry]
+ """return the number of trees in the dijoint forest"""