Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

A rooted tree has these parent→child edges: 20→84, 28→1, 72→74, 59→69, 99→20, 28→99, 14→47, 74→28, 30→59, 12→64, 72→12, 74→30, 69→14. Each node starts with the value: 1=7, 12=13, 14=11, 20=15, 28=13, 30=4, 47=13, 59=3, 64=19, 69=1, 72=11, 74=15, 84=7, 99=2. These updates are applied in order: set node 28 to 2; set node 99 to 6; set node 28 to 3; set node 59 to 18; set node 12 to 10. Afterwards, what is the sum of the values in the subtree of node 99 (node 99 and all its descendants)?

  1. A. 24
  2. B. 21
  3. C. 38
  4. D. 28

Correct answer: D. 28

Apply the updates first, then sum the current values over the whole subtree of 99. Summing the old values, only the children, or the parent's subtree are the traps.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic