Cycle Detection in Undirected Graph

Question - https://practice.geeksforgeeks.org/problems/detect-cycle-in-an-undirected-graph/1/
My Code - https://ide.codingblocks.com/s/99402

Okay, so this problem has test cases in which the graph contains self loops and more than one components.
But my code shows runtime error on my system’s terminal, but in the IDE it shows compiled successfully and then shows incorrect and incomple answers.

Please tell me where the error’s occuring and also if my approach to check for self loop is correct or not…
Thanks in advance!