Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
A tree has vertices with values in brackets: R[-3], G[6], I[7], U[-4], H[3], W[1], P[9], O[-3]. The edges are R–G, G–I, I–U, R–H, R–W, R–P, R–O. 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. 10
- B. 13
- C. 26
- D. 19
Correct answer: D. 19
A DP that combines the two best downward chains at each vertex finds 19. Paths restricted to a single ancestor-to-descendant line give at most 13, 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
- An undirected graph has the vertices A, G, Q, Z and the edges Z–A, Z–G, Q–A, Q–G. In how many ways can each vertex be given one of 3…
- A directed acyclic graph has the vertices E, N, O, S, U, V, Z and the edges Z→S, N→O, U→V, U→N, U→O, E→V, E→S. A topological order lists…
- An undirected graph on 6 vertices has the edges I–S, I–F, I–W, I–Q, I–K, S–F, S–K, F–K. How many simple paths (no repeated vertex) go from…
- An undirected connected graph on 6 vertices has the edges (weights in brackets, all distinct): X–Z (14), X–S (12), V–Z (30), S–Z (4), V–S…
- Consider this function: python def g(n): if n < 2: return n if n % 2 == 0: return g(n - 3) + g(n - 2) return g(n - 3) - 1 What value does…
- A connected undirected graph has the vertices F, H, I, M, O, V and the edges F–M, F–I, F–O, M–V, M–H, H–I. How many bridges does it have…