Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

A rooted tree has these parent→child edges: 61→41, 41→38, 58→16, 87→61, 28→68, 61→13, 38→25, 38→19, 87→58, 28→52, 41→28, 87→27, 9→87. Each node starts with the value: 9=11, 13=3, 16=17, 19=9, 25=1, 27=2, 28=16, 38=20, 41=7, 52=17, 58=8, 61=2, 68=4, 87=5. These updates are applied in order: set node 25 to 7; set node 16 to 9; set node 38 to 8; set node 61 to 9. Afterwards, what is the sum of the values in the subtree of node 58 (node 58 and all its descendants)?

  1. A. 25
  2. B. 104
  3. C. 9
  4. D. 17

Correct answer: D. 17

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