class UnionFind: def __init__(self,n): self.n=n self.parent_size=[-1]*n def leader(self,a): if self.parent_size[a]<0: return a self.parent_size[a]=self.leader(self.parent_size[a]) return self.parent_size[a] def merge(self,a,b): x,y=self.leader(a),self.leader(b) if x == y: return if abs(self.parent_size[x])