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.
how do i optimise this further only 1 test case passes with this one`
def valueOfFriendsship(n, friendships):
p=[i for i in range(1,n+1)]
f=[0 for i in range(n)]
def findparent(n):
if p[n-1]==n:
return n
else:
return findparent(p[n-1])
def union(a,b):
x=findparent(a)
y=findparent(b)
if x!=y:
if f[a-1]>f[b-1]:
p[b-1]=x
a=f[a-1]+1
for i in range(len(p)):
if p[i]==x:
f[i]=a
else:
p[a-1]=y
a=f[b-1]+1
for i in range(len(p)):
if p[i]==y:
f[i]=a
s=0
for i in friendships:
union(i[0],i[1])
s+=sum(f)
return s
The Value of Friendship
You are viewing a single comment's thread. Return to all comments →
how do i optimise this further only 1 test case passes with this one` def valueOfFriendsship(n, friendships): p=[i for i in range(1,n+1)] f=[0 for i in range(n)]
`