Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional
A tree has vertices with values in brackets: X[-1], L[8], G[-1], S[7], F[8], K[6]. The edges are X–L, L–G, X–S, X–F, S–K. A path is any sequence of adjacent vertices with no repeated vertex (a single vertex is a path). What is the maximum possible sum of values along one path?
- A. 13
- B. 8
- C. 29
- D. 20
Correct answer: D. 20
A DP that combines the two best downward chains at each vertex finds 20. Paths restricted to a single ancestor-to-descendant line give at most 13, and paths that start at the root give 12.
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
- An undirected graph has the vertices A, G, H, L, P and the edges L–G, H–P, A–H, H–G, H–L, A–P, P–G. How many simple cycles does it contain…
- A grid has 4 rows and 7 columns (top to bottom; '.' open, '#' blocked): .#.##.. ..#..#. .....#. ....... You may only move right or down…
- A directed weighted graph has the arcs u→v (weight): F→K (4), F→U (1), F→H (13), K→F (12), U→F (13), U→K (4), U→L (8), L→F (2), L→K (2)…
- A connected undirected graph has the vertices C, K, L, P, U, X, Y and the edges L–X, X–C, X–Y, C–K, C–P, C–U, K–Y, K–P, Y–U, P–U. Parallel…
- A directed acyclic graph has the vertices C, J, P, U, X and the arcs P→C, P→X, U→C, J→C, C→X. Two players alternately move a single token…
- A grid has 3 rows and 4 columns with these values (top to bottom): 1832 3673 2187 A path moves between side-adjacent cells (up, down, left…