Data Structures & AlgorithmsVery hardCodeforces Div2 C · ICPC regional

A tree has these parent→child edges (treat them as undirected): 26→66, 6→39, 8→69, 34→26, 26→6, 34→58, 34→71, 34→68, 8→59, 6→40, 8→81, 26→8. 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. 20
  2. B. 25
  3. C. 38
  4. D. 12

Correct answer: A. 20

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