Union find.
Union by rank.
Path compression and test