Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS

A rooted tree has these parent→child edges: 1→84, 20→50, 59→51, 59→1, 84→63, 1→56, 1→29, 29→34, 8→20, 1→8, 50→82. The nodes carry these weights: node 1: 20; node 8: 1; node 20: 17; node 29: 8; node 34: 16; node 50: 19; node 51: 15; node 56: 15; node 59: 20; node 63: 11; node 82: 8; node 84: 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. 91
  2. B. 81
  3. C. 165
  4. D. 78

Correct answer: A. 91

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