Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS
A rooted tree has these parent→child edges: 1→84, 20→50, 59→51, 59→1, 84→63, 1→56, 1→29, 29→34, 8→20, 1→8, 50→82. The nodes carry these weights: node 1: 20; node 8: 1; node 20: 17; node 29: 8; node 34: 16; node 50: 19; node 51: 15; node 56: 15; node 59: 20; node 63: 11; node 82: 8; node 84: 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. 91
- B. 81
- C. 165
- D. 78
Correct answer: A. 91
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: 38→81, 53→94, 70→75, 21→25, 21→14, 78→15, 70→78, 78→21, 21→38, 70→49, 21→53. The nodes carry…
- A rooted tree has these parent→child edges: 33→54, 18→61, 42→18, 83→79, 33→89, 83→98, 61→33, 98→42, 18→17. The nodes carry these weights…
- A rooted tree has these parent→child edges: 6→24, 56→7, 18→59, 98→92, 81→56, 39→81, 81→6, 98→18, 18→12, 98→39, 56→90. The nodes carry…
- A rooted tree has these parent→child edges: 16→34, 64→72, 46→64, 1→77, 1→47, 47→38, 1→46, 34→92, 24→16, 46→24. The nodes carry these…
- A rooted tree has these parent→child edges: 8→25, 34→95, 4→80, 34→4, 39→56, 4→93, 95→37, 93→99, 37→21, 21→8, 34→39. The nodes carry these…
- 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…