Data Structures & AlgorithmsModerateCodeforces Div2 D · ICPC regional

A disjoint-set forest on 0 to 7 keeps a parent array (initially parent[i] = i) and uses union by size with path compression. find(v) walks up to the root and re-points every node on that walk directly at the root. union(a, b) calls find(a) and find(b); if the roots are equal it does nothing, otherwise the root of the smaller set is attached below the root of the larger set (on equal sizes the root of a stays the root). Apply, in order: union(7, 1), union(7, 1), union(2, 4), union(0, 2), union(3, 3), find(4), union(6, 3), union(1, 6), find(7), union(3, 0), union(0, 2), union(7, 2), union(3, 5), find(0), union(3, 6). After all operations, what is parent[0]? (A root stores itself.)

  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 7

Correct answer: D. 7

Path compression rewrites parent pointers during find, so the stored parent of a node is not always its original parent. Forgetting the compression step, or breaking the tie the other way, leaves a different parent value.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic