Data Structures & AlgorithmsHardGATE CS · ICPC regional

A tree has vertices E, H, I, K, L, M, V, Z and weighted edges (weights in brackets): I–E (5), I–L (9), I–H (9), E–K (6), K–Z (7), E–M (1), I–V (5). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?

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

Correct answer: B. 27

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic