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)?
- A. 25
- B. 104
- C. 9
- 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.
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 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…
- A tree has these parent→child edges (treat them as undirected): 4→59, 92→30, 92→54, 54→4, 26→99, 30→13, 26→92. For a node x, let D(x) be…
- A tree has these parent→child edges (treat them as undirected): 89→51, 30→28, 81→93, 89→4, 28→69, 68→30, 69→29, 51→81, 4→38, 38→68. For a…
- A tree has these parent→child edges (treat them as undirected): 66→92, 18→66, 66→16, 51→93, 66→79, 66→81, 18→51. For a node x, let D(x) be…
- A tree has these parent→child edges (treat them as undirected): 51→18, 33→51, 51→15, 71→19, 71→26, 7→6, 7→71, 33→7. For a node x, let D(x)…