Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS
A rooted tree has these parent→child edges: 74→96, 74→9, 29→98, 55→35, 95→72, 95→88, 29→73, 29→55, 29→23, 98→61, 61→74, 88→29, 98→66. The nodes carry these weights: node 9: 16; node 23: 18; node 29: 3; node 35: 6; node 55: 10; node 61: 19; node 66: 11; node 72: 4; node 73: 18; node 74: 20; node 88: 13; node 95: 6; node 96: 20; node 98: 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. 98
- B. 179
- C. 129
- D. 81
Correct answer: C. 129
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: 65→11, 4→64, 65→60, 44→63, 44→15, 44→5, 65→67, 60→44, 30→4, 67→30, 5→48. The nodes carry these…
- A rooted tree has these parent→child edges: 60→75, 64→41, 84→66, 39→60, 61→88, 84→39, 39→2, 61→64, 75→76, 75→72, 2→61. The nodes carry…
- A rooted tree has these parent→child edges: 11→8, 37→78, 8→41, 37→21, 11→85, 78→1, 28→38, 1→4, 56→28, 85→56, 21→11, 1→70. The nodes carry…
- 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…
- 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…