Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS
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 these weights: node 2: 3; node 39: 16; node 41: 8; node 60: 3; node 61: 10; node 64: 17; node 66: 10; node 72: 16; node 75: 19; node 76: 11; node 84: 6; node 88: 19. 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. 89
- B. 81
- C. 138
- D. 75
Correct answer: A. 89
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→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…
- A rooted tree has these parent→child edges: 94→92, 29→17, 62→74, 92→93, 9→48, 30→85, 30→9, 9→29, 9→62, 9→24, 94→30, 94→76, 29→65. The…
- A rooted tree has these parent→child edges: 26→22, 79→89, 26→54, 26→96, 14→81, 45→14, 22→45, 79→16, 74→41, 16→74, 96→79, 89→47. The nodes…
- A tree has these parent→child edges: 77→20, 81→66, 35→21, 22→77, 21→70, 32→88, 35→32, 20→81, 22→35. A vertex cover is a set of vertices…
- A tree has these parent→child edges: 10→85, 22→81, 85→22, 10→50, 50→42, 73→2, 81→73, 48→10. A vertex cover is a set of vertices such that…
- A tree has these parent→child edges: 82→91, 74→48, 88→34, 74→56, 60→25, 51→60, 56→88, 91→74, 51→82. A vertex cover is a set of vertices…