You are given a rooted tree with n nodes labeled from 0 to n - 1, represented by an integer array parent of length n, where:
parent[0] = -1 (node 0 is the root).1 <= i < n, parent[i] is the parent of node i (0 <= parent[i] < i).You are also given an integer array nums of length n, where nums[i] is the value of node i, and an integer k.
A non-empty subset of nodes is called valid if:
k.Return the number of valid subsets modulo 109 + 7.
Example 1:
Input: parent = [-1,0,1], nums = [1,2,3], k = 3
Output: 1
Explanation:
The only valid subset is {2}. It contains node 2 with value 3, which is divisible by 3.
Example 2:
Input: parent = [-1,0,0,0], nums = [2,1,2,1], k = 3
Output: 2
Explanation:
The valid subsets are:
{1, 2}: Nodes 1 and 2 are both children of node 0 and not directly connected to each other. Their values sum to 1 + 2 = 3, which is divisible by 3.{2, 3}: Nodes 2 and 3 are also non-adjacent. Their values sum to 2 + 1 = 3, which is divisible by 3.No other subset satisfies both conditions. Therefore, the answer is 2.
Constraints:
n == parent.length == nums.length1 <= n <= 1000parent[0] == -11 <= i < n:
0 <= parent[i] < i1 <= nums[i] <= 1091 <= k <= 100parent describes a valid rooted tree.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
This problem asks for the number of valid non-adjacent subsets in a rooted tree whose values sum to a multiple of k. Because every node's parent has an index smaller than the node itself, the tree can be processed in reverse topological order from node n - 1 down to 1. For each node, we maintain two remainder distributions modulo k: one where the node is excluded and one where it is included.
Here's how the algorithm would work step-by-step:
def count_non_adjacent_subsets(parent: list[int], nums: list[int], k: int) -> int:
modulo = 10**9 + 7
total_nodes = len(parent)
# Initialize dynamic programming tables for excluding and including each node
not_chosen = [[0] * k for _ in range(total_nodes)]
chosen = [[0] * k for _ in range(total_nodes)]
for node_index in range(total_nodes):
not_chosen[node_index][0] = 1
chosen[node_index][nums[node_index] % k] = 1
# Process nodes from leaves up to the root
for child_index in range(total_nodes - 1, 0, -1):
parent_index = parent[child_index]
child_combined = [
(not_chosen[child_index][remainder] + chosen[child_index][remainder]) % modulo
for remainder in range(k)
]
next_not_chosen = [0] * k
next_chosen = [0] * k
for rem_parent in range(k):
ways_not = not_chosen[parent_index][rem_parent]
ways_inc = chosen[parent_index][rem_parent]
for rem_child in range(k):
target_rem = (rem_parent + rem_child) % k
next_not_chosen[target_rem] = (next_not_chosen[target_rem] + ways_not * child_combined[rem_child]) % modulo
next_chosen[target_rem] = (next_chosen[target_rem] + ways_inc * not_chosen[child_index][rem_child]) % modulo
not_chosen[parent_index] = next_not_chosen
chosen[parent_index] = next_chosen
# Combine root configurations and subtract the empty subset
total_valid = (not_chosen[0][0] + chosen[0][0] - 1 + modulo) % modulo
return total_valid
The problem asks for the number of non-empty, non-adjacent subsets of nodes in a rooted tree whose values sum to a multiple of k. Because parent indices are strictly smaller than child indices, we can process nodes in reverse index order, which corresponds to a bottom-up post-order traversal. For each node, we compute dynamic programming tables representing subset sums modulo k when the node is included versus when it is not included.
Here's how the algorithm would work step-by-step:
def count_non_adjacent_subsets(parent, nums, k):
modulus = 10**9 + 7
node_count = len(parent)
dp_not_selected = [[0] * k for _ in range(node_count)]
dp_selected = [[0] * k for _ in range(node_count)]
children = [[] for _ in range(node_count)]
# Group each child under its corresponding parent node.
for child_index in range(1, node_count):
children[parent[child_index]].append(child_index)
# Process tree nodes in reverse topological order from leaves to root.
for node_index in range(node_count - 1, -1, -1):
ways_if_not_picked = [0] * k
ways_if_picked = [0] * k
ways_if_not_picked[0] = 1
ways_if_picked[0] = 1
for child in children[node_index]:
next_not_picked = [0] * k
next_picked = [0] * k
child_any = [(dp_not_selected[child][remainder] + dp_selected[child][remainder]) % modulus for remainder in range(k)]
child_none = dp_not_selected[child]
for remainder_a in range(k):
val_any = child_any[remainder_a]
val_none = child_none[remainder_a]
for remainder_b in range(k):
target_remainder = (remainder_a + remainder_b) % k
next_not_picked[target_remainder] = (next_not_picked[target_remainder] + ways_if_not_picked[remainder_b] * val_any) % modulus
next_picked[target_remainder] = (next_picked[target_remainder] + ways_if_picked[remainder_b] * val_none) % modulus
ways_if_not_picked = next_not_picked
ways_if_picked = next_picked
dp_not_selected[node_index] = ways_if_not_picked
node_remainder = nums[node_index] % k
# Shift the remainder distribution by the current node's value when selected.
for remainder in range(k):
dp_selected[node_index][(remainder + node_remainder) % k] = ways_if_picked[remainder]
total_valid = (dp_not_selected[0][0] + dp_selected[0][0] - 1 + modulus) % modulus
return total_valid
| Case | How to Handle |
|---|---|
| Null or empty root node input | Return a count of 1 to account for the empty subset as required by the problem statement. |
| Single node tree with no children | Return 2 because both the empty subset and the single-node subset are valid non-adjacent subsets. |
| Linearly skewed tree resembling a linked list | Use iterative post-order traversal or tail recursion to prevent stack overflow from deep recursion. |
| Star graph tree with a single root and many direct children | Iterate through all children efficiently in linear time to combine their inclusion and exclusion states. |
| Extremely large tree resulting in subset counts exceeding standard integer limits | Apply modulo arithmetic using 10 to the 9th power plus 7 at every addition and multiplication step. |
| Negative or zero integer values stored inside the tree nodes | Treat node values purely as weights for presence or absence, as subset adjacency depends solely on graph topology. |
| Disconnected graphs or cyclic structures passed instead of a valid tree | Assume a valid rooted tree input or maintain a visited set during traversal to prevent infinite loops. |