I made a silly mistake, but I am unable to find it. I am not getting correct answer for the second test case. Please help.

p.s. Mistake is in variable k I think

I made a silly mistake, but I am unable to find it. I am not getting correct answer for the second test case. Please help.

p.s. Mistake is in variable k I think

you are pushing the same variable twice on the queue list.

For example, in the second test case:

initially qq = {0};

after loop 1, qq = {1,2};

after loop 2, qq = {2,2}, which is incorrect;

add a variable done[] to avoid this.

This passes the given test cases

I think dfs is easier and less error prone.

In BFS, you are supposed to iterate over the adjacent vertices, but in your code you were popping them(line 46). I cannot prove it but it might be deleting necessary connections.

Thanks, this now passes the given test cases, but not the problem. Can you tell me where else I have made mistake?