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.
vector<int>bfs(intn,intm,vector<vector<int>>edges,ints){// Rearranging edges. my_edges[i] - a list of nodes connected with i-th nodevector<vector<int>>my_edges(n);for(autoedge:edges){my_edges[edge[0]-1].push_back(edge[1]);my_edges[edge[1]-1].push_back(edge[0]);}// Traversing through the graphqueue<int>my_queue;my_queue.push(s);vector<int>distances(n,-1);distances[s-1]=0;intcurrent_dist=0;while(!my_queue.empty()){intcurrent_node=my_queue.front();for(intnew_node:my_edges[current_node-1]){if(distances[new_node-1]==-1){my_queue.push(new_node);distances[new_node-1]=distances[current_node-1]+6;}}my_queue.pop();}// Removing the start nodevector<int>ans;ans.reserve(n-1);for(inti=0;i<n;i++){if(i!=s-1)ans.push_back(distances[i]);}returnans;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Breadth First Search: Shortest Reach
You are viewing a single comment's thread. Return to all comments →