Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional
A tree has vertices D, I, K, N, O, R, T, Y and weighted edges (weights in brackets): Y–T (4), Y–I (2), Y–O (8), Y–K (3), O–N (5), O–R (7), N–D (9). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?
- A. 26
- B. 24
- C. 4
- D. 22
Correct answer: A. 26
Two DFS passes (or a DP over subtrees) give the weighted diameter 26. The farthest distance from Y is only 22, 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
- An undirected graph has the vertices C, O, Q, W, X, Z and the edges X–Q, Q–C, Q–O, W–C, X–C, O–Z, Q–Z, C–O, X–Z. What is the minimum…
- An undirected graph has the vertices C, G, T, W, X and the edges C–T, C–G, W–T, W–G, W–X, T–G, G–X. A walk may repeat vertices and edges…
- A grid has 6 rows and 6 columns (top to bottom; '.' open, '#' blocked): ..#... .....# .##... ...... ..#..# ...... You may only move right…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 20. Two sequences of steps…
- A grid has 4 rows and 4 columns (top to bottom; '.' open, '#' blocked): ..## .... .... .... You may only move right or down, and you…
- An undirected connected graph on the vertices D, H, I, K, M has the edges H–K, I–K, D–H, D–M, D–K, D–I, H–M, M–K, H–I. How many spanning…