Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

A rooted tree has these parent→child edges: 26→22, 79→89, 26→54, 26→96, 14→81, 45→14, 22→45, 79→16, 74→41, 16→74, 96→79, 89→47. The nodes carry these weights: node 14: 8; node 16: 13; node 22: 4; node 26: 18; node 41: 8; node 45: 1; node 47: 17; node 54: 9; node 74: 7; node 79: 9; node 81: 18; node 89: 4; node 96: 16. 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. 132
  3. C. 70
  4. D. 85

Correct answer: D. 85

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