BFS-Shortest Path

Q-
Consider an undirected graph consisting of ‘n’ nodes where each node is labeled from 1 to n and the edge between any two nodes is always of length 6 . We define node ‘s’ to be the starting position for a BFS.

Given ‘q’ queries in the form of a graph and some starting node, ‘s’ , perform each query by calculating the shortest distance from starting node ‘s’ to all the other nodes in the graph. Then print a single line of n-1 space-separated integers listing node s’s shortest distance to each of the n-1 other nodes (ordered sequentially by node number); if ‘s’ is disconnected from a node, print -1 as the distance to that node.

Input Format:
The first line contains an integer,q , denoting the number of queries. The subsequent lines describe each query in the following format:

Each line contains N and M, the number of nodes and edges respectively.

The next m lines contains u and v means that there is edge between u and v .

The last line contains ‘s’, the starting node.

Constraints:
1 <= q <= 10 2 <= n <= 1000 1 <= m <= (n*(n-1))/2 1 <= u,s,v <= n

Output Format
You have to print n-1 integer denoting the shortest distance from s to all other nodes excluding s itself.

code-https://ide.codingblocks.com/s/139223

I am not able to develop logic to print the output if you could pls analyse my code or perhaps share yours.

Hi @S18LPC-OL0050
Just implement bfs starting from the source given.
if(dist=== -1)
update its distance

//printing answer
for(int i=0;i<v;i++)
if( i!=src)
print(dist[i])

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.