When the input says there is an undirected edge between and , you must record it in both adjacency lists. I add to adj[u] and to adj[v].
adj[u].push_back(v)
adj[v].push_back(u)
If you only add one direction, a traversal from the other endpoint will miss the connection and you will report the wrong answer. Adding both entries takes time and extra space per edge, so you should always do it.