I am getting wrong ans


here is my code ide , pls check it , pls do comment the wrong part in it , actually i have got my mistake , while taking the edges (0,1) the parent of 1 doesnot chnges to 0, pls tell what i need to correct to correct it .

There were several errors in your code. Please see the corrections https://ide.codingblocks.com/s/575026.
I prefer to use vector for adjacency list.
While calculating answer I calculate the total number of elements belonging to each component with the help of a map.
Your unite function should work with the roots of both x and y. So I initialise them by their roots in the start of the function,

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.