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?

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic