Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 D
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 its own ancestor. What is the lowest common ancestor of the nodes 43 and 9, i.e. the deepest node that is an ancestor of both?
- A. 45
- B. 36
- C. 9
- D. 43
Correct answer: A. 45
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: 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…
- A rooted tree has these parent→child edges: 44→12, 90→19, 81→87, 44→37, 90→74, 19→44, 87→80, 80→25, 29→64, 12→81, 87→24, 19→29. A node…
- A rooted tree has these parent→child edges: 50→66, 72→15, 44→58, 68→72, 69→44, 59→68, 59→69, 72→30, 44→50. A node counts as its own…
- 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…
- 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…