Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

A rooted tree has these parent→child edges: 24→67, 99→17, 39→27, 39→53, 17→22, 66→55, 39→99, 27→34, 34→66, 55→24, 39→37, 66→12. The nodes carry these weights: node 12: 3; node 17: 20; node 22: 18; node 24: 12; node 27: 11; node 34: 20; node 37: 11; node 39: 20; node 53: 6; node 55: 2; node 66: 10; node 67: 11; node 99: 17. 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. 75
  2. B. 88
  3. C. 161
  4. D. 76

Correct answer: B. 88

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