Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

A rooted tree has these parent→child edges: 78→10, 58→4, 90→37, 90→58, 91→85, 97→30, 30→31, 31→87, 87→55, 87→78, 97→90, 30→91, 97→98. Each node starts with the value: 4=1, 10=12, 30=5, 31=2, 37=12, 55=9, 58=2, 78=9, 85=2, 87=1, 90=11, 91=13, 97=19, 98=8. These updates are applied in order: set node 37 to 2; set node 91 to 5; set node 78 to 16; set node 78 to 15. Afterwards, what is the sum of the values in the subtree of node 91 (node 91 and all its descendants)?

  1. A. 15
  2. B. 7
  3. C. 51
  4. D. 2

Correct answer: B. 7

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