Data Structures & AlgorithmsModerateICPC regional · Codeforces Div2 C
Elements 0 to 7 start as 8 separate singleton sets. The calls union(1, 4), union(7, 1), union(5, 3), union(0, 4), union(4, 1), union(2, 1), union(6, 4) 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?
- A. 1
- B. 3
- C. 6
- D. 2
Correct answer: D. 2
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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Elements 0 to 10 start as 11 separate singleton sets. The calls union(6, 0), union(8, 8), union(5, 3), union(2, 1), union(3, 3), union(7…
- Elements 0 to 10 start as 11 separate singleton sets. The calls union(5, 0), union(3, 9), union(9, 3), union(0, 1), union(0, 8), union(6…
- Elements 0 to 9 start as 10 separate singleton sets. The calls union(5, 7), union(2, 3), union(7, 1), union(9, 5), union(7, 4), union(8…
- Elements 0 to 10 start as 11 separate singleton sets. The calls union(10, 6), union(10, 2), union(8, 3), union(5, 9), union(0, 7)…
- Elements 0 to 5 start as 6 separate singleton sets. The calls union(5, 4), union(3, 2), union(3, 3), union(4, 3), union(5, 2), union(0…
- Elements 0 to 7 start as 8 separate singleton sets. The calls union(1, 0), union(3, 6), union(7, 1), union(6, 3), union(3, 0), union(6…