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?

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic