Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 D
A rooted tree has these parent→child edges: 98→57, 49→29, 72→94, 76→98, 72→87, 76→41, 76→49, 94→71, 72→76, 57→11. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 29 and 41, i.e. the deepest node that is an ancestor of both?
- A. 76
- B. 72
- C. 49
- D. 41
Correct answer: A. 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: 96→13, 13→90, 13→32, 72→46, 58→36, 13→30, 14→96, 72→93, 90→58, 96→50, 20→72, 58→20, 50→61. A…
- A rooted tree has these parent→child edges: 63→16, 38→40, 21→76, 76→77, 38→63, 55→21, 38→55, 21→60, 76→99. A node counts as its own…
- A rooted tree has these parent→child edges: 91→58, 37→12, 71→68, 68→35, 37→30, 16→37, 35→97, 71→16, 71→44, 35→91, 12→79, 79→54. A node…
- A rooted tree has these parent→child edges: 18→9, 78→90, 45→18, 48→91, 90→36, 78→48, 36→43, 45→78, 43→42, 64→38, 36→64. A node counts as…
- A rooted tree has these parent→child edges: 83→91, 16→22, 47→92, 86→47, 13→6, 22→25, 79→86, 92→65, 86→83, 13→73, 91→13, 83→16. A node…
- A rooted tree has these parent→child edges: 3→7, 5→79, 89→3, 7→88, 12→87, 5→52, 52→8, 92→80, 80→12, 79→92, 5→20, 89→5, 80→76. A node…