Data Structures & AlgorithmsHardCodeforces Div2 D · ICPC regional
A disjoint-set forest on 0 to 9 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(0), union(0, 5), union(9, 3), union(4, 2), union(4, 7), union(6, 2), union(3, 1), union(4, 4), union(1, 2), union(2, 1). After all operations, what is parent[1]? (A root stores itself.)
- A. 3
- B. 4
- C. 9
- D. 2
Correct answer: B. 4
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 6 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 9 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 6 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 11 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 6 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…