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)?
- A. 15
- B. 7
- C. 51
- 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.
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: 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…
- 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…