Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS
A rooted tree has these parent→child edges: 78→3, 62→46, 78→1, 81→78, 81→91, 93→76, 3→62, 81→23, 46→93. The nodes carry these weights: node 1: 8; node 3: 7; node 23: 16; node 46: 5; node 62: 15; node 76: 14; node 78: 13; node 81: 20; node 91: 1; node 93: 9. 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. 57
- B. 108
- C. 54
- D. 59
Correct answer: D. 59
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: 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…
- 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…