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)?
- A. 24
- B. 21
- C. 38
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A rooted tree has these parent→child edges: 65→8, 60→61, 61→19, 61→57, 57→63, 72→65, 57→72, 19→83, 60→92, 8→82, 57→67, 60→13. Each node…
- 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…
- A rooted tree has these parent→child edges: 81→45, 81→61, 33→46, 78→51, 94→70, 46→81, 46→94, 94→58, 81→78, 58→72, 51→52. Each node starts…
- 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…
- A tree has these parent→child edges (treat them as undirected): 5→23, 5→97, 5→70, 97→82, 5→22, 99→5. For a node x, let D(x) be the sum of…
- A tree has these parent→child edges (treat them as undirected): 20→27, 20→83, 92→20, 83→90, 27→97. For a node x, let D(x) be the sum of…