Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A tree has vertices with values in brackets: O[1], Y[4], S[5], A[2], U[2], L[6]. The edges are O–Y, Y–S, Y–A, O–U, O–L. 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. 16
  2. B. 10
  3. C. 6
  4. D. 20

Correct answer: A. 16

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic