Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
A tree has vertices with values in brackets: U[-2], B[7], H[5], A[7], N[-2], P[5], C[4], S[-1]. The edges are U–B, U–H, H–A, A–N, B–P, A–C, H–S. 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. 14
- B. 16
- C. 7
- D. 26
Correct answer: D. 26
A DP that combines the two best downward chains at each vertex finds 26. Paths restricted to a single ancestor-to-descendant line give at most 16, and paths that start at the root give 14.
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, J, Q, T, Y and the edges T–Q, T–A, Y–Q, Y–A, Q–A. What is its chromatic number, the least number…
- A tree has the vertices B, C, D, E, M, P, S, U, V, W and the edges U–B, U–C, C–W, B–V, U–M, M–D, D–P, B–S, W–E. A set D dominates the tree…
- A tree has vertices D, H, J, M, Q, R, U, W and weighted edges (weights in brackets): D–H (9), H–Q (5), H–W (5), Q–M (1), D–U (8), U–J (2)…
- An undirected connected graph on the vertices F, G, Q, S, U has the edges U–G, S–Q, F–Q, U–F, G–S, U–S, S–F, G–Q, U–Q. How many spanning…
- A tree has the vertices A, G, H, I, L, O, P, T, U, Z and the edges G–T, G–U, U–H, H–O, O–Z, Z–P, P–A, A–I, P–L. A set D dominates the tree…
- A grid has 8 rows and 6 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): ...#.. ..##.# ...#.. #..#.. ..###. .#.#…