Data Structures & AlgorithmsVery hardCodeforces Div2 C · ICPC regional

A tree has these parent→child edges (treat them as undirected): 77→90, 77→84, 90→8, 46→33, 92→10, 46→77, 10→46, 92→81, 81→15, 77→39. For a node x, let D(x) be the sum of the distances, in edges, from x to every node of the tree. What is the minimum value of D(x) over all nodes x?

  1. A. 29
  2. B. 45
  3. C. 22
  4. D. 21

Correct answer: D. 21

The minimum of D(x) is attained at the tree's median, which can be found by rerooting one edge at a time. The root's total, the maximum total, or the node of highest degree are not the minimum.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic