Pairing Problem

Can you please send me the implementation code for doing this problem by DFS/BFS approach.

hello @gptradhika1208
the idea is simple run a dfs from each univisited node .
let say on dfs u visited c nodes. then count of required pairs will be c * (n-c) .

repeat this process for all univisted node and add c*(n-c) to ur answer.

at the end print answer/2 (divide by 2 becuase each pair will be counted twice)

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.