Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 B

A tree has vertices with values in brackets: B[8], C[0], L[-3], G[-1], N[3], V[5]. The edges are B–C, B–L, C–G, G–N, B–V. 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?

  1. A. 15
  2. B. 8
  3. C. 13
  4. D. 16

Correct answer: A. 15

A DP that combines the two best downward chains at each vertex finds 15. Paths restricted to a single ancestor-to-descendant line give at most 13, and paths that start at the root give 13.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic