Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS
A rooted tree has these parent→child edges: 39→7, 52→6, 94→52, 7→45, 40→33, 33→79, 5→39, 94→5, 51→40, 52→51, 33→67. The nodes carry these weights: node 5: 4; node 6: 13; node 7: 13; node 33: 15; node 39: 6; node 40: 13; node 45: 12; node 51: 3; node 52: 8; node 67: 18; node 79: 5; node 94: 13. 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. 75
- B. 123
- C. 80
- D. 62
Correct answer: C. 80
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: 11→33, 42→77, 42→70, 46→15, 70→46, 70→76, 42→16, 42→89, 31→11, 42→31, 76→67. The nodes carry…
- 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…
- 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…