Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 B

A tree has vertices C, I, M, R, S, U, X and weighted edges (weights in brackets): M–C (9), C–U (2), C–I (8), C–X (1), M–R (8), U–S (7). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?

  1. A. 18
  2. B. 27
  3. C. 4
  4. D. 26

Correct answer: D. 26

Two DFS passes (or a DP over subtrees) give the weighted diameter 26. The farthest distance from M is only 18, and counting edges instead of weights gives 4.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic