Https://ide.codingblocks.com/s/253404

https://ide.codingblocks.com/s/253404 Wrong answer

@Shlok,
Input:
2 1
0 1
Correct Output:
0
Your Output:
2

Can you tell me how to approach this problem or a sample code so that i can make changes in mine

@Shlok,

Find connected components.
Any vertex from one set of C.C will definitely not have a path to other vertex of another C.C

Consider the test case:
Input:
4 1
0 2

Expected Output:
5

5 ways of choosing a pair:
[0,1] [0,3] [1,2] [1,3] [2,3]

In one of the graphs webinar of Prateek Bhaiya, he has discussed about a problem named “Journey to the Moon” which is similar to this problem. Refer that video and then try to solve this.

Hope this helps :slightly_smiling_face:

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.