Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

Elements 0 to 9 start as singletons. union(a, b) finds the roots of a and b and, if they differ, links one root under the other so the merged set's size is the sum of both sizes. After the calls union(8, 7), union(3, 0), union(2, 6), union(8, 7), union(8, 4), union(7, 2), union(2, 2), union(0, 7), union(8, 1), union(1, 0) (in this order), how many elements are in the set that contains 9?

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

Correct answer: D. 1

The size of a merged set is the sum of the two merged sizes; the answer is the size of x's final set, not the number of calls that involved x or the number of components.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic