Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D
A rooted tree has these parent→child edges: 59→20, 32→33, 33→10, 96→77, 3→92, 80→59, 33→3, 33→99, 80→39, 10→55, 59→11, 11→96, 10→80. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 11 and 39, i.e. the deepest node that is an ancestor of both?
- A. 32
- B. 80
- C. 59
- D. 39
Correct answer: B. 80
The LCA is the deepest common ancestor: lift the deeper node to the same depth, then climb both together (binary lifting does this in O(log n)). Answering the root, the parent of one node, or a node itself is the trap.
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 rooted tree has these parent→child edges: 56→63, 72→79, 48→75, 13→5, 81→91, 31→72, 58→40, 31→56, 31→81, 58→31, 56→89, 40→13, 58→48…
- A rooted tree has these parent→child edges: 7→75, 11→34, 11→99, 89→80, 52→11, 19→91, 60→19, 35→60, 60→89, 80→25, 91→52, 7→43, 35→7. A node…
- A rooted tree has these parent→child edges: 2→49, 95→55, 60→5, 60→31, 2→45, 98→83, 55→33, 55→87, 31→70, 60→2, 45→95, 45→98, 2→65. A node…
- A rooted tree has these parent→child edges: 4→14, 35→69, 35→83, 70→56, 4→37, 69→26, 4→68, 35→70, 56→99, 70→24, 44→4, 78→44, 2→35, 78→2. A…
- A rooted tree has these parent→child edges: 13→44, 75→96, 33→30, 96→98, 23→92, 98→42, 16→76, 41→59, 76→33, 47→84, 96→16, 59→23, 76→12…
- A rooted tree has these parent→child edges: 9→83, 83→70, 95→92, 87→7, 83→87, 50→90, 78→62, 83→80, 7→86, 50→78, 92→50, 78→31, 9→95, 7→43…