Count Non Adjacent Subsets in a Rooted Tree Problem
Description
LeetCode Problem 3939.
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(node0is the root).- For each
1 <= i < n,parent[i]is the parent of nodei(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:
- The sum of the values of the selected nodes is divisible by
k. - No two selected nodes are adjacent in the tree (no node and its direct parent are both included in the subset).
Return the number of valid subsets modulo 10^9 + 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] == -1- For all
1 <= i < n:0 <= parent[i] < i
1 <= nums[i] <= 10^91 <= k <= 100parentdescribes a valid rooted tree.
Solution
Approach: Tree Knapsack by Remainder
For each node u we count the subsets inside its subtree by the remainder of their sum modulo k, in two groups:
skip[r]: subsets that do not useu.take[r]: subsets that useu.
If u is not chosen, each child can be in either state, so skip is the convolution (over remainders, modulo k) of skip + take of all children. If u is chosen, every child must be unchosen, so we convolve the children's skip arrays and then shift the result by nums[u] % k.
Because parent[i] < i, we can process the nodes from n - 1 down to 0 and merge each finished node into its parent, with no recursion. At the root, the number of valid subsets is skip[0] + take[0] - 1, where the - 1 removes the empty subset. All counts are taken modulo 109 + 7.
Complexity: O(n k2) time and O(n k) space.
class Solution:
def countValidSubsets(self, parent: List[int], nums: List[int], k: int) -> int:
mod = 10**9 + 7
n = len(parent)
# skip[u][r]: ways in u's subtree with u unchosen and sum % k == r
# take[u][r]: the same with u chosen
# a = product (convolution) of (skip + take) over the children, b = of skip
a = [[1] + [0] * (k - 1) for _ in range(n)]
b = [[1] + [0] * (k - 1) for _ in range(n)]
def mul(x: List[int], y: List[int]) -> List[int]:
res = [0] * k
for i, xi in enumerate(x):
if xi:
for j, yj in enumerate(y):
if yj:
res[(i + j) % k] = (res[(i + j) % k] + xi * yj) % mod
return res
for u in range(n - 1, -1, -1):
skip = a[u]
v = nums[u] % k
take = b[u][-v:] + b[u][:-v] if v else b[u][:]
if u == 0:
return (skip[0] + take[0] - 1) % mod
p = parent[u]
a[p] = mul(a[p], [(x + y) % mod for x, y in zip(skip, take)])
b[p] = mul(b[p], skip)
return 0