Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

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

  1. A. 4
  2. B. 0
  3. C. 9
  4. D. 6

Correct answer: D. 6

Simulating with union by size and path compression puts all ten nodes under root 6, and the find calls re-point 0 directly at it. Without compression parent[0] stays 4 (its first union); 0 would mean it is still a root, and 9 is another node in the set, not 0's parent.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic