Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
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). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?
- A. 24
- B. 23
- C. 4
- D. 25
Correct answer: A. 24
Two DFS passes (or a DP over subtrees) give the weighted diameter 24. The farthest distance from R is only 15, and counting edges instead of weights gives 4.
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 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…
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→B (7), S→T (6), A→B (5), A→T (3), E→B (2), B→C…