Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS

A rooted tree has these parent→child edges: 78→3, 62→46, 78→1, 81→78, 81→91, 93→76, 3→62, 81→23, 46→93. The nodes carry these weights: node 1: 8; node 3: 7; node 23: 16; node 46: 5; node 62: 15; node 76: 14; node 78: 13; node 81: 20; node 91: 1; node 93: 9. 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. 108
  3. C. 54
  4. D. 59

Correct answer: D. 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