Maximum Score After Applying Operations on a Tree Problem
Description
LeetCode Problem 2925.
There is an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given a 2D integer array edges of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.
You are also given a 0-indexed integer array values of length n, where values[i] is the value associated with the i^th node.
You start with a score of 0. In one operation, you choose a node i, add values[i] to your score, and then set values[i] to 0. All three steps happen together as a single operation.
A tree is healthy if, for every leaf node, the sum of the current values on the path from the root to that leaf is not equal to 0.
Return the maximum score you can obtain by performing this operation on the tree any number of times, so that the tree remains healthy at the end.
Example 1:

Input: edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
Output: 11
Explanation: We apply the operation to nodes 1, 2, 3, 4, and 5, so the values become [5,0,0,0,0,0]. The leaves are nodes 1, 3, and 5.
- The sum of values on the path from 0 to 1 is equal to 5.
- The sum of values on the path from 0 to 3 is equal to 5.
- The sum of values on the path from 0 to 5 is equal to 5.
Every leaf has a non-zero path sum, so the tree is healthy. The score is the sum of the original values of the chosen nodes: 2 + 5 + 2 + 1 + 1 = 11.
It can be shown that 11 is the maximum score obtainable by performing any number of operations on the tree.
Example 2:

Input: edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
Output: 40
Explanation: We apply the operation to nodes 0, 2, 3, and 4, so the values become [0,10,0,0,0,3,5]. The leaves are nodes 3, 4, 5, and 6.
- The sum of values on the path from 0 to 3 is equal to 10.
- The sum of values on the path from 0 to 4 is equal to 10.
- The sum of values on the path from 0 to 5 is equal to 3.
- The sum of values on the path from 0 to 6 is equal to 5.
Every leaf has a non-zero path sum, so the tree is healthy. The score is the sum of the original values of the chosen nodes: 20 + 9 + 7 + 4 = 40.
It can be shown that 40 is the maximum score obtainable by performing any number of operations on the tree.
Constraints:
2 <= n <= 2 * 10^4edges.length == n - 1edges[i].length == 20 <= a_i, b_i < nvalues.length == n1 <= values[i] <= 10^9- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: Tree DP
Equivalently, we choose which nodes to keep (their values are added to the score) so that every path from the root to a leaf has at least one node that is not kept. We want the largest kept sum.
For a node i, dfs(i, fa) returns two numbers for its subtree:
a: the sum of all values in the subtree (everything kept),b: the best score when every root-to-leaf path inside the subtree has an unkept node.
For a leaf the result is (values[i], 0): it has to be the unkept node. Otherwise, add up the children's results as A and B. Then a = values[i] + A, and b is the larger of keeping i (values[i] + B) and not keeping i (then the whole subtree below can be kept: A).
The answer is the second value of dfs(0, -1).
Complexity: O(n) time and O(n) space, where n is the number of nodes.
class Solution:
def maximumScoreAfterOperations(
self, edges: List[List[int]], values: List[int]
) -> int:
def dfs(i: int, fa: int = -1) -> (int, int):
a = b = 0
leaf = True
for j in g[i]:
if j != fa:
leaf = False
aa, bb = dfs(j, i)
a += aa
b += bb
if leaf:
return values[i], 0
return values[i] + a, max(values[i] + b, a)
g = [[] for _ in range(len(values))]
for a, b in edges:
g[a].append(b)
g[b].append(a)
return dfs(0)[1]