Data Structures & AlgorithmsHardCodeforces Div2 D · ICPC regional

A disjoint-set forest on 0 to 11 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(9, 5), union(4, 10), find(9), find(6), union(9, 2), find(6), union(4, 7), union(1, 3), union(11, 10), find(5), union(11, 8), union(5, 10), union(0, 11), find(4), find(2), union(10, 3). After all operations, what is parent[2]? (A root stores itself.)

  1. A. 10
  2. B. 4
  3. C. 5
  4. D. 9

Correct answer: B. 4

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