Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

A rooted tree has these parent→child edges: 93→74, 59→15, 74→59, 65→30, 16→45, 65→25, 73→33, 59→16, 74→73, 93→63, 65→28, 93→65. Each node starts with the value: 15=8, 16=12, 25=11, 28=11, 30=20, 33=19, 45=14, 59=10, 63=12, 65=5, 73=18, 74=6, 93=13. These updates are applied in order: set node 74 to 13; set node 33 to 14; set node 30 to 18; set node 59 to 8; set node 45 to 4. Afterwards, what is the sum of the values in the subtree of node 16 (node 16 and all its descendants)?

  1. A. 26
  2. B. 16
  3. C. 32
  4. D. 4

Correct answer: B. 16

Apply the updates first, then sum the current values over the whole subtree of 16. 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