Given a tree with N vertices, each having a weight, determine the optimal partitioning of the tree into subtrees such that the sum of weights of vertices in each subtree does not exceed a given limit. The goal is to minimize the number of subtrees. The tree is represented as an adjacency list where each vertex is associated with a list of its children and a weight.
Explanation: Optimal partitioning: subtree {0, 1, 3, 4} with weight sum 10 + 5 + 3 + 2 = 20 is not feasible, so partition into {0, 2} with weight sum 10 + 5 = 15 and {1, 3, 4} with weight sum 5 + 3 + 2 = 10.
Explanation: Optimal partitioning: since the sum of weights of any two vertices exceeds the limit, each vertex must be in its own subtree, resulting in 4 subtrees, but since vertex 0 can include vertex 2 or 3, we get 3 subtrees.
Dynamic programming over tree structures
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak