Data Structures & AlgorithmsHardGATE CS · ICPC regional
A tree has vertices with values in brackets: A[-2], U[8], Y[5], G[0], Z[-3], V[6], T[1], J[-2]. The edges are A–U, U–Y, Y–G, G–Z, A–V, Y–T, Y–J. 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. 8
- B. 12
- C. 14
- D. 18
Correct answer: D. 18
A DP that combines the two best downward chains at each vertex finds 18. Paths restricted to a single ancestor-to-descendant line give at most 14, and paths that start at the root give 12.
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 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…
- 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…