Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS
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 these weights: node 14: 4; node 15: 19; node 21: 17; node 25: 17; node 38: 1; node 49: 20; node 53: 12; node 70: 5; node 75: 16; node 78: 17; node 81: 11; node 94: 1. 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. 84
- B. 99
- C. 140
- D. 53
Correct answer: B. 99
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: 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…
- 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…