Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A tree has vertices with weights in brackets: O[11], H[15], M[14], Z[13], Q[2], V[7], L[15], T[3]. The edges are O–H, O–M, M–Z, M–Q, Q–V, O–L, M–T. A set of vertices is independent if no edge joins two of its members. What is the maximum total weight of an independent set?

  1. A. 51
  2. B. 53
  3. C. 52
  4. D. 29

Correct answer: B. 53

A tree DP (take a vertex and forfeit its children, or keep the children) gives 53. Greedily taking the heaviest vertex that conflicts with nothing gives only 51.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic