If your code is too slow, check that you are merging small into large. Print set sizes before merging to verify the direction is correct. If answers are wrong, check that you are adding the current node's contribution after merging children, not before.
Order matters. If you get memory errors or segfaults, make sure you are not keeping references to moved or swapped sets. After you move a set, the old variable is invalid and accessing it causes undefined behavior.