Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import heapq as hq
- class Solution:
- #Function to find sum of weights of edges of the Minimum Spanning Tree.
- def spanningTree(self,n, adj):
- h=[(0,0)]
- ans=0
- vis=set()
- while len(vis)<n:
- w,v=hq.heappop(h)
- if v in vis:continue
- vis.add(v)
- ans+=w
- for u,w in adj[v]:
- if u not in vis:
- hq.heappush(h,(w,u))
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement