Data Structures & AlgorithmsHardGATE CS · ICPC regional
A tree has vertices with values in brackets: P[-4], S[4], V[7], U[1], G[-4], D[1]. The edges are P–S, S–V, V–U, S–G, S–D. 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. 7
- B. 8
- C. 12
- D. 13
Correct answer: D. 13
A DP that combines the two best downward chains at each vertex finds 13. Paths restricted to a single ancestor-to-descendant line give at most 12, and paths that start at the root give 8.
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 four-wheel combination lock shows four digits 0–9 on wheels that wrap around (9 goes to 0 and 0 goes to 9). One move turns one wheel by…
- A grid has 3 rows and 4 columns with these values (top to bottom): 6389 5169 6136 A path moves between side-adjacent cells (up, down, left…
- A directed acyclic graph has the vertices F, H, M, P, U, W and the arcs H→M, H→W, U→F, F→M, F→P, F→W, M→P, M→W. Two players alternately…
- An undirected graph has the vertices A, D, U, W, Y and the edges U–Y, A–D, A–Y, D–Y, W–Y, U–D, A–W, A–U. How many simple cycles does it…
- Floyd–Warshall runs on vertices 1 to 5 with these arcs u→v (weight): 1→2 (6), 1→3 (5), 1→5 (6), 2→3 (5), 2→4 (11), 3→1 (11), 3→2 (3), 3→4…
- An undirected graph on 6 vertices has the edges E–Z, E–K, E–W, Z–K, Z–F, Z–H, Z–W, K–F, K–W. How many simple paths (no repeated vertex) go…