Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS
A rooted tree has these parent→child edges: 73→27, 57→8, 84→26, 57→70, 92→84, 70→73, 59→48, 73→92, 8→59, 48→81. The nodes carry these weights: node 8: 3; node 26: 8; node 27: 10; node 48: 18; node 57: 4; node 59: 20; node 70: 17; node 73: 6; node 81: 2; node 84: 12; node 92: 10. 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. 67
- B. 61
- C. 110
- D. 44
Correct answer: A. 67
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: 24→67, 99→17, 39→27, 39→53, 17→22, 66→55, 39→99, 27→34, 34→66, 55→24, 39→37, 66→12. The nodes…
- 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…
- 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…