Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 B

A tree has vertices with values in brackets: N[-2], J[9], M[1], R[0], H[9], G[6], S[-2]. The edges are N–J, J–M, N–R, M–H, N–G, J–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. 23
  2. B. 19
  3. C. 9
  4. D. 17

Correct answer: A. 23

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic