Data Structures & AlgorithmsModerateCodeforces Div 2 C

Tree on vertices 0 to 8 with weighted edges (u–v (w)): 1–8 (4), 2–6 (5), 4–8 (5), 3–6 (5), 2–8 (4), 2–7 (6), 0–8 (6), 1–5 (4). What is the diameter of the tree, i.e. the largest sum of edge weights over any path between two vertices?

  1. A. 20
  2. B. 5
  3. C. 39
  4. D. 22

Correct answer: D. 22

The heaviest path has weight 22, confirmed by all-pairs distances. The eccentricity of vertex 0 (20) measures paths from a single vertex, and the hop-count diameter (5) ignores the weights.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic