Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

A rooted tree has these parent→child edges: 74→96, 74→9, 29→98, 55→35, 95→72, 95→88, 29→73, 29→55, 29→23, 98→61, 61→74, 88→29, 98→66. The nodes carry these weights: node 9: 16; node 23: 18; node 29: 3; node 35: 6; node 55: 10; node 61: 19; node 66: 11; node 72: 4; node 73: 18; node 74: 20; node 88: 13; node 95: 6; node 96: 20; node 98: 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?

  1. A. 98
  2. B. 179
  3. C. 129
  4. D. 81

Correct answer: C. 129

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