Data Structures & AlgorithmsModerateICPC regional · Codeforces Div2 C
Elements 0 to 6 start as 7 separate singleton sets. The calls union(5, 4), union(3, 4), union(5, 6), union(6, 5), union(2, 2), union(0, 2) 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. 4
- B. 3
- C. 1
- D. 2
Correct answer: B. 3
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 9 start as 10 separate singleton sets. The calls union(6, 9), union(0, 6), union(3, 5), union(8, 4), union(6, 1), union(6…
- 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…
- Elements 0 to 5 start as singletons. For each call union(a, b), in order: let ra and rb be the roots of a and b. If ra = rb nothing…
- Elements 0 to 5 start as singletons. For each call union(a, b), in order: let ra and rb be the roots of a and b. If ra = rb nothing…
- Elements 0 to 9 start as singletons. For each call union(a, b), in order: let ra and rb be the roots of a and b. If ra = rb nothing…
- Elements 0 to 6 start as singletons. For each call union(a, b), in order: let ra and rb be the roots of a and b. If ra = rb nothing…