Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional
A tree has vertices with values in brackets: J[0], N[0], I[8], Q[3], K[-2]. The edges are J–N, N–I, N–Q, I–K. 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. 10
- D. 11
Correct answer: D. 11
A DP that combines the two best downward chains at each vertex finds 11. Paths restricted to a single ancestor-to-descendant line give at most 8, and paths that start at the root give 8.
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
- Cities H, J, L, T, W are joined by roads with these travel costs: H–T 6, H–W 9, H–L 19, H–J 19, T–W 19, T–L 20, T–J 15, W–L 13, W–J 14…
- Consider this function: python def g(n): if n < 3: return n if n % 2 == 0: return g(n - 3) + g(n - 4) return g(n - 1) - 1 What value does…
- Cities E, S, T, U, V are joined by roads with these travel costs: T–E 16, T–V 1, T–S 8, T–U 3, E–V 2, E–S 4, E–U 14, V–S 2, V–U 4, S–U 12…
- Consider this recursive function: python def f(n): if n <= 0: return f(n - 2) if n % 3 == 0: f(n - 2) How many times is f called in total…
- An undirected graph has the vertices D, P, Q, Y and the edges Y–P, Y–D, P–D, Q–D. A walk may repeat vertices and edges. How many walks of…
- A bipartite graph has left side P1, P2, P3 and right side Q1, Q2, Q3, Q4, with edges P2–Q1, P2–Q3, P1–Q4, P1–Q3, P2–Q4, P3–Q2. What is the…