Data Structures & AlgorithmsModerateCodeforces Div2 D · GATE CS

A rooted tree has these parent→child edges: 54→15, 64→46, 61→11, 15→76, 64→28, 37→61, 28→54, 28→37, 37→68. The nodes carry these weights: node 11: 16; node 15: 9; node 28: 13; node 37: 1; node 46: 11; node 54: 3; node 61: 11; node 64: 18; node 68: 6; node 76: 2. 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. 49
  2. B. 55
  3. C. 90
  4. D. 40

Correct answer: B. 55

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