Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A tree has vertices with values in brackets: R[-3], G[6], I[7], U[-4], H[3], W[1], P[9], O[-3]. The edges are R–G, G–I, I–U, R–H, R–W, R–P, R–O. 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. 10
  2. B. 13
  3. C. 26
  4. D. 19

Correct answer: D. 19

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic