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.
fromcollectionsimportdequedeftreeFlow(n,tree):# Write your code heregraph=[[]for_inrange(500010)]fora,b,wintree:graph[a].append((b,w))graph[b].append((a,w))defbfs(start,dis):ans=0qu=deque()qu.append((-1,start,0))whilequ:bef,cur,dis=qu.popleft()fornex_node,weightingraph[cur]:ifnex_node==bef:continuex=dis+weightqu.append((cur,nex_node,x))ans+=xreturnansanswer=min(bfs(1,0),bfs(n,0))returnanswer
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree Flow
You are viewing a single comment's thread. Return to all comments →
pyhon