Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2
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(3,4), union(7,1), union(1,2), union(3,7), union(1,5), union(7,6). How many parent links does element 4 follow to reach its root (depth, root = 0)?
- A. 0
- B. 2
- C. 3
- D. 1
Correct answer: B. 2
Following parent links from 4 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
- 2 × 3 sliding puzzle with tiles 1 to 5 and one blank (shown as ). Current board: 4 1 3 / 2 5 . Goal board: 1 2 3 / 4 5 . A move slides a…
- Directed graph on vertices A, B, C, D, E, F with edges D→E, A→B, C→D, F→E, F→C, E→D, A→D, C→A. How many strongly connected components does…
- Undirected simple graph on vertices A, B, C, D, E, F with edges A–C (1), B–D (1), B–E (1), B–F (1), C–D (1), C–E (1), C–F (1), D–E (1)…
- Directed acyclic graph on vertices A, B, C, D, E, F, G (edges go from earlier to later letters) with weights A→B (3), A→D (4), A→E (4)…
- Points A(1,7), B(2,4), C(4,9), D(8,8), E(2,7) in the plane. The cost of joining two points is the Manhattan distance |x1−x2| + |y1−y2|…
- An undirected graph has the vertices G, Q, R, U, Y, Z and the edges Q–Z, Y–Q, G–Z, R–Z, G–R, Y–R. How many simple cycles does it contain…