Data Structures & AlgorithmsHardCodeforces Div2 D · ICPC regional
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 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(5), union(8, 5), find(3), find(0), union(0, 5), union(6, 7), union(7, 1), find(8), union(5, 0), find(0), union(4, 1), union(1, 0), find(4), find(7), union(8, 0). After all operations, what is parent[0]? (A root stores itself.)
- A. 6
- B. 8
- C. 0
- D. 1
Correct answer: A. 6
Simulating union by size with path compression, 0 ends up pointing straight at the root 6 after the later finds compress its path. Without compression parent[0] stays 8; 0 would mean it is still a root, and 1 is just another node of the same set.
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 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 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…