Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D
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, 76→47, 30→41, 92→13, 12→55. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 84 and 55, i.e. the deepest node that is an ancestor of both?
- A. 75
- B. 47
- C. 76
- D. 84
Correct answer: C. 76
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: 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…
- A rooted tree has these parent→child edges: 42→60, 13→40, 10→42, 19→10, 8→19, 8→75, 42→77, 77→13. The parent of a node is its 1st…
- A rooted tree has these parent→child edges: 22→20, 20→50, 22→55, 55→48, 75→24, 48→75, 75→17, 24→10. The parent of a node is its 1st…
- A rooted tree has these parent→child edges: 33→59, 27→38, 59→27, 25→85, 78→33, 85→76, 27→25. The parent of a node is its 1st ancestor…
- A rooted tree has these parent→child edges: 10→9, 81→72, 97→16, 85→10, 81→45, 73→40, 6→37, 9→6, 97→81, 45→73, 73→85, 37→30. The parent of…
- A rooted tree has these parent→child edges: 57→55, 95→57, 85→20, 62→95, 35→24, 20→62, 85→31, 57→9, 62→35. The parent of a node is its 1st…