Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

A tree has vertices F, G, J, K, L, M, N, U, X and weighted edges (weights in brackets): N–F (4), N–U (3), U–M (7), N–X (7), U–J (7), U–K (2), K–L (6), X–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. 5
  2. B. 27
  3. C. 15
  4. D. 26

Correct answer: D. 26

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic