Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS
A rooted tree has these parent→child edges: 94→92, 29→17, 62→74, 92→93, 9→48, 30→85, 30→9, 9→29, 9→62, 9→24, 94→30, 94→76, 29→65. The nodes carry these weights: node 9: 20; node 17: 16; node 24: 12; node 29: 9; node 30: 17; node 48: 18; node 62: 10; node 65: 13; node 74: 17; node 76: 10; node 85: 11; node 92: 20; node 93: 20; node 94: 15. A set of nodes is independent if no two chosen nodes are joined by an edge. What is the largest total weight of an independent set?
- A. 107
- B. 208
- C. 123
- D. 112
Correct answer: C. 123
Tree DP: for each node, either take it (children must be skipped) or skip it (children are free). Greedily taking the heaviest node, taking everything, or taking alternate depths are the trap answers.
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: 26→22, 79→89, 26→54, 26→96, 14→81, 45→14, 22→45, 79→16, 74→41, 16→74, 96→79, 89→47. The nodes…
- A tree has these parent→child edges: 77→20, 81→66, 35→21, 22→77, 21→70, 32→88, 35→32, 20→81, 22→35. A vertex cover is a set of vertices…
- A tree has these parent→child edges: 10→85, 22→81, 85→22, 10→50, 50→42, 73→2, 81→73, 48→10. A vertex cover is a set of vertices such that…
- A tree has these parent→child edges: 82→91, 74→48, 88→34, 74→56, 60→25, 51→60, 56→88, 91→74, 51→82. A vertex cover is a set of vertices…
- A tree has these parent→child edges: 17→24, 59→71, 50→98, 72→61, 39→59, 61→50, 59→17, 1→72, 39→1, 1→97. A vertex cover is a set of…
- A tree has these parent→child edges: 52→59, 36→42, 48→36, 48→45, 48→52, 55→48, 14→85, 36→22, 8→55, 45→14, 22→61. A vertex cover is a set…