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?

  1. A. 7
  2. B. 8
  3. C. 12
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic