This is the solution to this UVa problem.
Note: In this problem you need to be very careful with the update of the rank list
Note: In this problem you need to be very careful with the update of the rank list
Code
class uf:
def __init__(self,numnodes):
self.r = [1] * (numnodes)
self.p = []
for i in range(n):
self.p.append(i)
def j(self,a,b):
if a == b:
return
a=self.root(a)
b=self.root(b)
if a == b:
return
if self.r[a] > self.r[b]:
self.r[a]+=self.r[b]
self.p[b]= self.p[a]
else :
self.r[b]+=self.r[a]
self.p[a] = self.p[b]
def root(self,a):
while True:
if self.p[a]==a:
break
else:
self.p[a] =self.p[self.p[a]]
a = self.p[a]
return a
def con(self,a,b):
if self.root(a) == self.root(b):
return True
else:
return False
while True:
n,m=input().split(' ')
n=int(n)
m=int(m)
if n==0 and m==0:
break
muf=uf(n)
while m!=0:
m-=1
g=[int(i) for i in input().split(' ')[1:]]
for i,j in zip(g[:-1],g[1:]):
muf.j(i,j)
print(muf.r[muf.root(0)])
Comments
Post a Comment