Data Structures & AlgorithmsHardGATE CS · ICPC regional

A tree has vertices A, D, E, J, L, Q, T, W, X and weighted edges (weights in brackets): L–T (2), T–D (6), D–W (9), T–Q (6), Q–J (7), L–X (6), T–E (9), W–A (8). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?

  1. A. 34
  2. B. 5
  3. C. 25
  4. D. 36

Correct answer: D. 36

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic