Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional
A disjoint-set forest on 0 to 10 keeps a parent array (initially parent[i] = i) and uses union by size with path compression. find(v) walks up to the root and re-points every node on that walk directly at the root. union(a, b) calls find(a) and find(b); if the roots are equal it does nothing, otherwise the root of the smaller set is attached below the root of the larger set (on equal sizes the root of a stays the root). Apply, in order: find(9), union(10, 8), union(2, 8), find(0), find(6), union(8, 1), find(6), union(5, 6), union(9, 8), union(8, 1), union(0, 6), union(1, 10), union(3, 6), union(0, 10), find(5), union(2, 3). After all operations, what is parent[3]? (A root stores itself.)
- A. 5
- B. 8
- C. 10
- D. 6
Correct answer: C. 10
Path compression rewrites parent pointers during find, so the stored parent of a node is not always its original parent. Forgetting the compression step, or breaking the tie the other way, leaves a different parent value.
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
- A disjoint-set forest on 0 to 7 keeps a parent array (initially parent[i] = i) and uses union by size with path compression. find(v) walks…
- A disjoint-set forest on 0 to 8 keeps a parent array (initially parent[i] = i) and uses union by size with path compression. find(v) walks…
- A disjoint-set forest on 0 to 8 keeps a parent array (initially parent[i] = i) and uses union by size with path compression. find(v) walks…
- A disjoint-set forest on 0 to 10 keeps a parent array (initially parent[i] = i) and uses union by size with path compression. find(v)…
- A disjoint-set forest on 0 to 8 keeps a parent array (initially parent[i] = i) and uses union by size with path compression. find(v) walks…
- Elements 0 to 11 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…