Undirected graph connectivity (bfs)

Question :

In an undirected graph G, two vertices u and v are connected if G contains a path from u to v. An undirected graph is said to be connected if every pair of vertices in the graph are connected. Given an undirected graph, determine whether the graph is connected or not.


The first line consists of ‘t’, the number of test cases. The first line of each test case consists of ‘v’ and ‘e’, the number of vertices and the number of edges respectively. Next ‘e’ lines consists of ‘a’ & ‘b’ meaning that there is an edge between vertices ‘a’ and ‘b’.


Print “YES”(without quotes) if the graph is connected and “NO”(without quotes) otherwise

My code:

My doubt:

this works for graphs with cycles, without cycles and graphs that are not connected. please help by telling any test case that would not give the correct output or what is the error in the above code.

Thank you

There isn’t any code.