Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
A tree has vertices B, H, L, V, W, X, Y and weighted edges (weights in brackets): L–H (7), L–B (2), L–V (3), L–Y (7), L–X (3), X–W (1). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?
- A. 13
- B. 15
- C. 7
- D. 14
Correct answer: D. 14
Two DFS passes (or a DP over subtrees) give the weighted diameter 14. The farthest distance from L is only 7, and counting edges instead of weights gives 3.
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 C, G, I, L, M, R, X and weighted edges (weights in brackets): R–X (6), R–I (2), X–L (9), R–M (7), R–C (1), C–G (8)…
- Cities E, P, U, V, Y are joined by roads with these travel costs: V–E 6, V–P 20, V–U 9, V–Y 20, E–P 2, E–U 20, E–Y 11, P–U 15, P–Y 11, U–Y…
- Consider this function: python def g(n): if n < 3: return n if n % 2 == 0: return g(n - 3) + g(n - 2) return g(n - 3) - 1 What value does…
- Grid with 4 rows and 4 columns (top to bottom). A cell marked G is a gate; '.' is a normal cell: ..GG ..G. .G.. ..G. Each step, which is a…
- An undirected graph has the vertices C, D, F, K, W, Y and the edges D–W, D–F, D–Y, D–K, W–Y, C–Y, C–K, F–Y, Y–K. What is its chromatic…
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→D (9), S→B (6), D→B (9), D→T (4), E→D (3), E→T…