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?
- A. 16
- B. 10
- C. 6
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- The directed acyclic graph has the vertices D, O, R, T, U, Y and the arcs Y→O, T→O, O→R, Y→T, Y→D, Y→R, T→R, T→U. How many distinct…
- A connected undirected graph has the vertices B, F, M, O, Z and weighted edges (weights in brackets): O–M (16), F–Z (14), B–M (7), O–B…
- An undirected graph has the vertices C, J, L, Q, W and the edges W–L, J–C, L–C, J–Q, Q–L, W–C, W–J, W–Q. How many simple cycles does it…
- A tree on the vertices B, E, G, S, W has the arcs W→E, S→E, B→W, W→G (each undirected edge is directed as shown). You may reverse the…
- The directed acyclic graph has the vertices C, F, G, K, N, T, W and the arcs C→F, W→C, G→N, G→K, N→F, K→F, T→W, W→F, T→F, G→T, K→C, T→K…
- A directed acyclic graph has the vertices B, I, M, Q, R and the arcs B→Q, B→R, I→Q, I→R, I→M, Q→M. Two players alternately move a single…