Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional
Union–find on elements 0 to 7, all starting as singletons, with no path compression. union(x, y): let rx, ry be the roots of x and y; if rx = ry do nothing; if size(rx) < size(ry) make rx a child of ry, otherwise make ry a child of rx (sizes add). Operations in order: union(6,5), union(2,7), union(3,2), union(5,2), union(6,5), union(7,4), union(4,0). How many parent links does element 5 follow to reach its root (depth, root = 0)?
- A. 2
- B. 0
- C. 1
- D. 3
Correct answer: A. 2
Following parent links from 5 gives depth 2. Breaking the size tie in the other direction gives 1, and always linking the first root gives 3, so the tie rule matters.
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
- Directed weighted graph on vertices A, B, C, D, E with edges (u→v, w): A→D (3); E→D (4); A→E (3); C→A (8); A→C (5); D→C (6); E→C (9); B→E…
- Distinct keys [26, 13, 5, 17, 19, 4] are inserted, in the order given by the sequence [19, 13, 4, 5, 17, 26], into an empty binary search…
- Undirected simple graph on vertices A, B, C, D, E, F with edges A–C (1), A–D (1), A–F (1), B–D (1), B–E (1), D–E (1), E–F (1) (weights…
- Connected undirected weighted graph on vertices A, B, C, D, E with edges A–B (1), A–D (4), A–E (4), B–C (4), B–D (1), B–E (3), D–E (2)…
- Tree with vertices 0 to 9, root 0, edges 0–1, 0–2, 2–3, 1–4, 3–5, 4–6, 5–7, 4–8, 8–9 (parent–child). Vertex weights (vertex:weight) 0:18…
- Keys [19, 23, 15, 12, 48, 41] are inserted, in this order, into an empty AVL tree using the standard rebalancing (left, right, left–right…