C/C++ Arena

Step 6 of 6

Challenge: union-find

Union-find (disjoint set union) tracks which items are in the same group while groups keep merging: network connectivity, clustering, Kruskal's minimum spanning tree, detecting cycles as edges arrive.

Each set is a tree; find(x) walks to the root, which identifies the set. Two tricks make it nearly O(1) per operation:

Your turn: write class UnionFind:

Previous: Dijkstra's shortest paths