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?

  1. A. 13
  2. B. 15
  3. C. 7
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic