Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional

A tree has vertices D, I, K, N, O, R, T, Y and weighted edges (weights in brackets): Y–T (4), Y–I (2), Y–O (8), Y–K (3), O–N (5), O–R (7), N–D (9). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?

  1. A. 26
  2. B. 24
  3. C. 4
  4. D. 22

Correct answer: A. 26

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic