How do you define a "group"? You pick one person in the group to be the Leader (or Representative).
If Alice and Bob have the same leader, they are in the same group. To merge two groups, make one leader report to the other. To check if two people are in the same group, check if they have the same leader.
This intuition leads to a tree structure: everyone points to someone "above" them, and the root is the leader. Finding the leader means walking up the tree.