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?
- A. 23
- B. 19
- C. 9
- 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.
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
- A directed acyclic graph has the vertices A, H, N, Q, X, Z and the edges N→Z, H→Z, A→H, Q→H, Q→Z, Q→X. A topological order lists every…
- An undirected graph has the vertices A, B, D, P, R, Y and the edges P–D, P–B, P–R, D–A, D–Y, B–Y, A–R, R–Y. In how many ways can each…
- Each cell of a grid with 3 rows and 5 columns has an arrow: R = right, L = left, U = up, D = down. RURUR LRDLU RDDRD From a cell you may…
- On an 8×8 chessboard, rows and columns are numbered 0 to 7, and squares are written (row, column). A knight moves in the usual L-shape…
- Bellman–Ford is run from source X on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: F→I…
- A tree has the vertices C, E, G, K, N, S, T, V, Z and the edges T–K, K–E, K–V, K–S, S–N, N–C, C–G, G–Z. A centroid is a vertex whose…