Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional

Union–find on elements 0 to 7, all starting as singletons, with no path compression. union(x, y): let rx, ry be the roots of x and y; if rx = ry do nothing; if size(rx) < size(ry) make rx a child of ry, otherwise make ry a child of rx (sizes add). Operations in order: union(6,5), union(2,7), union(3,2), union(5,2), union(6,5), union(7,4), union(4,0). How many parent links does element 5 follow to reach its root (depth, root = 0)?

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

Correct answer: A. 2

Following parent links from 5 gives depth 2. Breaking the size tie in the other direction gives 1, and always linking the first root gives 3, so the tie rule matters.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic