Maximum Sum of Independent Nodes in a Tree
Trees & GraphsAuto-graded · 15 test casesLast updated
Tests tree DP by finding the maximum sum of node values when no two selected nodes may be adjacent (parent-child) in a rooted tree.
Maximum Sum of Independent Nodes in a Tree
You are given a rooted tree with n nodes numbered 0 through n - 1. Node 0 is always the root.
The tree is described by a list parent of length n where parent[i] is the parent of node i. For the root, parent[0] == -1. Each node i also has an integer value values[i].
Your task is to select a subset of nodes such that:
- No two selected nodes are directly connected by an edge (i.e., no selected node is the parent or child of another selected node).
- The sum of values of the selected nodes is maximised.
Return the maximum possible sum.
How it works
- Build the tree from the
parentarray. - Use tree DP: for every node decide whether to include it (gaining its value but forbidding its children) or exclude it (allowing each child to make its own best choice).
- Recurse from the root.
Constraints
1 ≤ n ≤ 2000len(parent) == len(values) == nparent[0] == -1; for alli ≥ 1,0 ≤ parent[i] < nandparent[i] != i- The input is guaranteed to describe a valid rooted tree (no cycles, every non-root node has exactly one parent).
-1000 ≤ values[i] ≤ 1000- Nodes may have negative values; you are never forced to select any node (an empty selection with sum
0is always valid).
Examples
Example 1
Input: max_independent_node_sum([-1, 0, 0, 1, 1], [10, 5, 3, 4, 2])
Output: 16
Example 2
Input: max_independent_node_sum([-1, 0, 1, 2], [3, 7, 4, 5])
Output: 12
Example 3
Input: max_independent_node_sum([-1], [42])
Output: 42
Hints
Hint 1
Define dp(node) returning a pair (include_sum, exclude_sum): the best sum when this node is included vs. excluded.
Hint 2
When a node is included its value is added and all children must be excluded. When it is excluded each child independently picks the better of its own include/exclude.
Hint 3
Because all values can be negative, make sure to allow the empty selection — the answer is max(include_sum, exclude_sum) at the root, and each subproblem naturally handles this since negative contributions are never forced.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a classic tree DP (dynamic programming on trees) problem.
Key insight
For every node we have exactly two choices:
- Include it — add its value, but every direct child must be excluded.
- Exclude it — each direct child is free to choose whichever option gives the larger sum.
Define dp(node) to return a pair (inc, exc):
inc= best sum achievable in the subtree rooted atnodewhennodeis selected.exc= best sum achievable in the subtree rooted atnodewhennodeis not selected.
Recurrence
inc(node) = values[node] + sum(exc(child) for child in children(node))
exc(node) = sum(max(inc(child), exc(child)) for child in children(node))
Both values are clamped to 0 (we are never forced to pick nodes with negative contributions).
The answer is max(inc(root), exc(root)).
Correctness
The recurrence exhaustively covers all valid independent sets of the subtree. Because selections only interact through parent-child edges, and we track both cases per node, no valid configuration is missed or double-counted.
Complexity
- Time: O(n) — each node is visited exactly once.
- Space: O(n) — for the children adjacency list and the recursion stack (depth at most n in a chain).
Reference solution
from typing import List
def max_independent_node_sum(parent: List[int], values: List[int]) -> int:
n = len(parent)
# Build adjacency list (children only)
children = [[] for _ in range(n)]
for i in range(1, n):
children[parent[i]].append(i)
# dp(node) -> (best_sum_if_included, best_sum_if_excluded)
def dp(node: int):
inc = values[node] # include this node
exc = 0 # exclude this node
for child in children[node]:
child_inc, child_exc = dp(child)
# If we include `node`, children must be excluded
inc += child_exc
# If we exclude `node`, children choose the better option
exc += max(child_inc, child_exc)
# Never forced to take negative sums — clamp at 0
inc = max(inc, 0)
exc = max(exc, 0)
return inc, exc
root_inc, root_exc = dp(0)
return max(root_inc, root_exc)