Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 B
A tree has vertices with values in brackets: B[8], C[0], L[-3], G[-1], N[3], V[5]. The edges are B–C, B–L, C–G, G–N, B–V. 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. 15
- B. 8
- C. 13
- D. 16
Correct answer: A. 15
A DP that combines the two best downward chains at each vertex finds 15. Paths restricted to a single ancestor-to-descendant line give at most 13, and paths that start at the root give 13.
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
- On an 6×6 chessboard, rows and columns are numbered 0 to 5, and squares are written (row, column). A knight moves in the usual L-shape…
- The directed acyclic graph has the vertices C, E, J, K, L, M, Q, Z and the arcs Q→C, Q→M, C→J, L→J, L→K, Q→K, E→J, M→C, M→L, M→E, Q→E…
- Starting from the integer 4, one operation either adds 1, subtracts 1, or doubles the current value. Values may be any non-negative…
- A directed graph has the vertices A, M, O, V and the arcs O→M, O→A, M→O, M→A, A→O, A→V, V→O, V→A. Count the directed Hamiltonian paths…
- An undirected graph has the vertices B, D, E, F, I, M, X and the edges M–E, M–X, M–D, M–B, M–F, E–X, E–D, E–I, X–D, X–B, D–F, B–I, F–I…
- A directed weighted graph has the arcs u→v (weight): Y→K (14), Y→Q (6), K→L (4), K→T (5), L→Q (8), D→Y (3), D→Q (11), D→T (3), Q→Y (12)…