Maximum Good Subtree Score Problem
Description
LeetCode Problem 3575.
You are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1. Each node i has an integer value vals[i], and its parent is given by par[i].
A subset of nodes within the subtree of a node is called good if every digit from 0 to 9 appears at most once in the decimal representation of the values of the selected nodes.
The score of a good subset is the sum of the values of its nodes.
Define an array maxScore of length n, where maxScore[u] represents the maximum possible sum of values of a good subset of nodes that belong to the subtree rooted at node u, including u itself and all its descendants.
Return the sum of all values in maxScore.
Since the answer may be large, return it modulo 10^9 + 7.
Example 1:
Input: vals = [2,3], par = [-1,0]
Output: 8
Explanation:

- The subtree rooted at node 0 includes nodes {0, 1}. The subset {2, 3} is good as the digits 2 and 3 appear only once. The score of this subset is 2 + 3 = 5.
- The subtree rooted at node 1 includes only node {1}. The subset {3} is good. The score of this subset is 3.
- The maxScore array is [5, 3], and the sum of all values in maxScore is 5 + 3 = 8. Thus, the answer is 8.
Example 2:
Input: vals = [1,5,2], par = [-1,0,0]
Output: 15
Explanation:

- The subtree rooted at node 0 includes nodes {0, 1, 2}. The subset {1, 5, 2} is good as the digits 1, 5 and 2 appear only once. The score of this subset is 1 + 5 + 2 = 8.
- The subtree rooted at node 1 includes only node {1}. The subset {5} is good. The score of this subset is 5.
- The subtree rooted at node 2 includes only node {2}. The subset {2} is good. The score of this subset is 2.
- The maxScore array is [8, 5, 2], and the sum of all values in maxScore is 8 + 5 + 2 = 15. Thus, the answer is 15.
Example 3:
Input: vals = [34,1,2], par = [-1,0,1]
Output: 42
Explanation:

- The subtree rooted at node 0 includes nodes {0, 1, 2}. The subset {34, 1, 2} is good as the digits 3, 4, 1 and 2 appear only once. The score of this subset is 34 + 1 + 2 = 37.
- The subtree rooted at node 1 includes node {1, 2}. The subset {1, 2} is good as the digits 1 and 2 appear only once. The score of this subset is 1 + 2 = 3.
- The subtree rooted at node 2 includes only node {2}. The subset {2} is good. The score of this subset is 2.
- The maxScore array is [37, 3, 2], and the sum of all values in maxScore is 37 + 3 + 2 = 42. Thus, the answer is 42.
Example 4:
Input: vals = [3,22,5], par = [-1,0,1]
Output: 18
Explanation:
- The subtree rooted at node 0 includes nodes {0, 1, 2}. The subset {3, 22, 5} is not good, as digit 2 appears twice. Therefore, the subset {3, 5} is valid. The score of this subset is 3 + 5 = 8.
- The subtree rooted at node 1 includes nodes {1, 2}. The subset {22, 5} is not good, as digit 2 appears twice. Therefore, the subset {5} is valid. The score of this subset is 5.
- The subtree rooted at node 2 includes {2}. The subset {5} is good. The score of this subset is 5.
- The maxScore array is [8, 5, 5], and the sum of all values in maxScore is 8 + 5 + 5 = 18. Thus, the answer is 18.
Constraints:
1 <= n == vals.length <= 5001 <= vals[i] <= 10^9par.length == npar[0] == -10 <= par[i] < nforiin[1, n - 1]- The input is generated such that the parent array
parrepresents a valid tree.
Solution
Approach: Bitmask DP on the Tree
A good subset uses each decimal digit at most once, so its digits form a 10-bit mask. For every node u keep dp[mask], the best score of a good subset inside the subtree of u whose digits are exactly mask (or -1 if no such subset exists).
Process the nodes from the leaves up. Start dp with the empty set (mask = 0, score 0) and, if the digits of vals[u] are all different, the single-node set {u}. Then merge each child's table: for every reachable a in the current table, enumerate the submasks b of the complement of a and combine dp[a] with child[b]. Subsets taken from different children are disjoint, so only the digit masks need to be disjoint.
maxScore[u] is the largest value in the table of u. Add them all up and take the result modulo 10^9 + 7.
Complexity: Each merge enumerates all pairs of disjoint masks, at most 3^10 of them, so the time is O(n * 3^10) and the space is O(n * 2^10).
class Solution:
def goodSubtreeSum(self, vals: List[int], par: List[int]) -> int:
MOD = 10**9 + 7
n = len(vals)
g = [[] for _ in range(n)]
for i in range(1, n):
g[par[i]].append(i)
order = [0]
for u in order:
order.extend(g[u])
def digit_mask(x):
m = 0
for ch in str(x):
b = 1 << int(ch)
if m & b:
return -1
m |= b
return m
full = (1 << 10) - 1
dp = [None] * n
ans = 0
for u in reversed(order):
cur = [-1] * (full + 1)
cur[0] = 0
m = digit_mask(vals[u])
if m >= 0:
cur[m] = vals[u]
for v in g[u]:
child = dp[v]
dp[v] = None
nxt = cur[:]
for a in range(full + 1):
if cur[a] < 0:
continue
comp = full ^ a
sub = comp
while True:
if child[sub] >= 0 and cur[a] + child[sub] > nxt[a | sub]:
nxt[a | sub] = cur[a] + child[sub]
if sub == 0:
break
sub = (sub - 1) & comp
cur = nxt
dp[u] = cur
ans += max(cur)
return ans % MOD