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.
not sure why my python3 solution fails nearly half the test cases. Any ideas? I think it may have to do with deleting distances[start]
defbfs(graph,start):queue=[]visited=set()distances={}queue.append(start)fornodeingraph:distances[node]=-1distances[start]=0whilequeue:cur=queue.pop()ifcurinvisited:continuevisited.add(cur)forneighboringraph[cur]:ifneighbornotinvisited:queue.append(neighbor)ifdistances[neighbor]==-1:distances[neighbor]=distances[cur]+6deldistances[start]returndistancesn_queries=int(input())for_inrange(n_queries):graph={}n,m=list(map(int,input().split()))#init graph by pointsfor_inrange(n):graph[_+1]=[]for_inrange(m):edge=list(map(int,input().split()))graph[edge[0]].append(edge[1])graph[edge[1]].append(edge[0])start=int(input())distances=bfs(graph,start)print(*distances.values(),sep=' ')
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
BFS: Shortest Reach
You are viewing a single comment's thread. Return to all comments →
not sure why my python3 solution fails nearly half the test cases. Any ideas? I think it may have to do with deleting distances[start]