Getting a Wrong answer in Pairing problem

I am getting the wrong answer. Can you please help in figuring out where did I make a mistake. Here is the code:

here in the union function
image

i and j should be root( p) and root( q)

id vector contains the immediate parent not the root of set

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.