WRONG OUTPUT ON SUBMISSION

can you tell me the problem in my code, on running sample test case i figured out that i had to clear distance vector every time i run bfs function. but even that doesn’t help

Save your code on ide.codingblocks.com and share its link.

Dry run and check your code with the given sample test case.
Sample Input
2
4 2
1 2
1 3
1
3 1
2 3
2
Sample Output
6 6 -1
-1 6

Your Output:
6 6 -1
6 6

You can refer this https://ide.codingblocks.com/s/253469

i still don’t understand where the error is in my code. can you point it out please?

For each test case, you have to reinitialize or clear you graph/adjacency list.
Also please check if you have implemented the adjacency list correctly. Because i can see that you are building your graph with a 1D vector. But there may be cases that a node has several children. So vector < int > will not work. You must use vector < list< int > >

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.