A city's clubs keep merging, and anyone may ask whether two people are now in the same club. A
union-find structure keeps each club as a tree of parent pointers: find(x) follows the
pointers from up to the root, and union(a, b) links one root under the other. A find costs
one step per pointer it follows, so tall trees are slow.
The two linking rules in parts A and B, for union(a, b) with roots of and of :
- Naive: becomes a child of .
- By size: the root of the smaller tree becomes a child of the other's root; on a tie, goes under .
A tree's height is the most parent steps from any element to its root. In parts A and B
there are 16 elements, to , and each union is written as two numbers, a b, one per line.
Part C's cost
Your structure keeps everything in two lists the grader gives you, parent and rank, of
cells each. Every read and every write of a cell costs 1. The grader runs four sequences of up to
24,096 operations on , built to hurt structures that skip one of the standard ideas. Each
sequence's total must stay within . Every find is checked: it
must return an element of the right club, and the same one for the whole club until the next
union. Every 97 operations the grader also compares two elements' clubs; those extra finds are
not charged to you.
Background: Union-Find.