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?
- A. 3
- B. 8
- C. 0
- 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.
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 7 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…
- Elements 0 to 6 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…
- A Fenwick tree t[1..12] (1-indexed, all zero at the start) is updated by add(i, v): set j = i, then while j ≤ n do { t[j] += v; j += j &…
- A Fenwick tree t[1..6] (1-indexed, all zero at the start) is updated by add(i, v): set j = i, then while j ≤ n do { t[j] += v; j += j & −j…
- A Fenwick tree t[1..6] (1-indexed, all zero at the start) is updated by add(i, v): set j = i, then while j ≤ n do { t[j] += v; j += j & −j…
- A Fenwick tree t[1..6] (1-indexed, all zero at the start) is updated by add(i, v): set j = i, then while j ≤ n do { t[j] += v; j += j & −j…