Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS
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 these weights: node 11: 15; node 15: 20; node 16: 14; node 31: 8; node 33: 15; node 42: 18; node 46: 11; node 67: 14; node 70: 14; node 76: 5; node 77: 10; node 89: 5. 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. 149
- C. 49
- D. 100
Correct answer: D. 100
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: 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…
- 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…