Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 C

Elements 0 to 9 start as 10 separate singleton sets. The calls union(4, 4), union(3, 9), union(3, 5), union(9, 6), union(7, 2), union(8, 4), union(9, 8) are made in this order. union(a, b) merges the sets containing a and b, and does nothing when they already share a set. How many disjoint sets remain at the end?

  1. A. 3
  2. B. 6
  3. C. 5
  4. D. 4

Correct answer: D. 4

Each call that joins two different sets lowers the number of sets by exactly one; every other call, including a repeated pair or union(a, a), changes nothing. So the answer is n minus the number of merging calls, not n minus the number of calls.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic