Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

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 weights: node 4: 9; node 5: 3; node 11: 8; node 15: 1; node 30: 12; node 44: 12; node 48: 3; node 60: 10; node 63: 19; node 64: 7; node 65: 15; node 67: 6. 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?

  1. A. 57
  2. B. 105
  3. C. 49
  4. D. 60

Correct answer: D. 60

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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic