Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS

A rooted tree has these parent→child edges: 33→54, 18→61, 42→18, 83→79, 33→89, 83→98, 61→33, 98→42, 18→17. The nodes carry these weights: node 17: 11; node 18: 7; node 33: 17; node 42: 9; node 54: 8; node 61: 7; node 79: 15; node 83: 7; node 89: 9; node 98: 8. 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. 52
  2. B. 98
  3. C. 59
  4. D. 51

Correct answer: C. 59

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