We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
classUnionFind:"""OptimizedUnion-Find(DisjointSet)datastructurewithpathcompressionandunionbyrankoptimizations."""def__init__(self):self.parent={}self.rank={}self.size={}defmake_set(self,x:int)->None:ifxnotinself.parent:self.parent[x]=xself.rank[x]=0self.size[x]=1deffind(self,x:int)->int:"""Find with path compression"""ifx!=self.parent[x]:self.parent[x]=self.find(self.parent[x])returnself.parent[x]defunion(self,x:int,y:int)->int:"""Union by rank with size tracking"""px,py=self.find(x),self.find(y)ifpx==py:returnself.size[px]ifself.rank[px]<self.rank[py]:px,py=py,pxself.parent[py]=pxself.size[px]+=self.size[py]ifself.rank[px]==self.rank[py]:self.rank[px]+=1returnself.size[px]defmaxCircle(queries):uf=UnionFind()max_size=0result=[]forx,yinqueries:# Create sets if they don't existuf.make_set(x)uf.make_set(y)# Perform union and get size of resulting setcurrent_size=uf.union(x,y)max_size=max(max_size,current_size)result.append(max_size)returnresult
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Friend Circle Queries
You are viewing a single comment's thread. Return to all comments →
python solution: