Maximum Good Subtree Score - Problem
You're exploring a family tree where each person has a unique value, and you want to find the most valuable group from each family branch with one special rule: no digit (0-9) can appear more than once across all the values in your selected group.
Given an undirected tree rooted at node 0 with
Your goal: For each node
Example: If a subtree has nodes with values [123, 456, 789], this is good because digits 1,2,3,4,5,6,7,8,9 each appear once. But [123, 124] would not be good because digit '2' appears twice.
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]. A good subset within any subtree means every digit from 0 to 9 appears at most once in the decimal representation of all selected node values combined.Your goal: For each node
u, find the maximum possible sum of a good subset from its subtree (including itself and all descendants). Return the sum of all these maximum values modulo 109 + 7.Example: If a subtree has nodes with values [123, 456, 789], this is good because digits 1,2,3,4,5,6,7,8,9 each appear once. But [123, 124] would not be good because digit '2' appears twice.
Input & Output
example_1.py — Simple Tree
$
Input:
vals = [1, 2, 3, 4], par = [0, 0, 1, 1]
›
Output:
15
💡 Note:
Tree structure: 0(1) has children 1(2), 1(2) has children 2(3) and 3(4). For subtree at 0: best subset is {1,2,3,4} with no repeated digits, sum=10. For subtree at 1: best is {2,3,4}, sum=9. For leaves 2 and 3: sums are 3 and 4. Total: 10+9+3+4=26. Wait, let me recalculate... Actually for node 0's subtree: {1,2,3,4} sum=10. Node 1's subtree: {2,3,4} sum=9. Node 2: {3} sum=3. Node 3: {4} sum=4. But we need to check digit constraints properly.
example_2.py — Digit Conflict
$
Input:
vals = [12, 23, 34], par = [0, 0, 1]
›
Output:
81
💡 Note:
Tree: 0(12) -> 1(23) -> 2(34). Node 0's subtree can use {12,34} (digits 1,2,3,4 - valid) sum=46. Node 1's subtree: {23,34} (digits 2,3,4 - digit 3 conflicts) so max is either {23}=23 or {34}=34. Node 2: {34}=34. Total: 46+34+34=114? Need to verify digit logic.
example_3.py — Single Node
$
Input:
vals = [100], par = [0]
›
Output:
100
💡 Note:
Only one node with value 100. The only subtree (rooted at 0) has maximum score 100. Total sum = 100.
Visualization
Tap to expand
Understanding the Visualization
1
Map the Family Tree
Build the tree structure and convert each value to a digit bitmask
2
Start from Leaves
Use post-order traversal to process children before parents
3
Combine Compatible Sets
For each node, merge digit sets from children if no conflicts
4
Track Best Scores
Maintain maximum score for each possible digit combination
Key Takeaway
🎯 Key Insight: By using bitmasks to represent digit usage, we reduce the problem from exponential (2ⁿ subsets) to manageable (2¹⁰ = 1024 digit combinations), making the solution efficient even for larger inputs.
Time & Space Complexity
Time Complexity
O(n × 2^10)
Each node processes at most 2^10 possible digit masks (10 digits), visited once in DFS
✓ Linear Growth
Space Complexity
O(n × 2^10)
DP table storing maximum scores for each node and digit mask combination
⚡ Linearithmic Space
Constraints
- 1 ≤ n ≤ 103
- 0 ≤ vals[i] ≤ 109
- par[0] = 0 (root has itself as parent)
- 0 ≤ par[i] < i for 1 ≤ i < n
- Tree structure guaranteed - no cycles, connected
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code