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 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: 26
💡 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 subtree at 2: {3} sum=3. For subtree at 3: {4} sum=4. Total: 10+9+3+4=26.
example_2.py — Digit Conflict
$ Input: vals = [12, 23, 34], par = [0, 0, 1]
Output: 114
💡 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} have conflicting digit 3, so max is {34}=34. Node 2: {34}=34. Total: 46+34+34=114.
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.

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

Visualization

Tap to expand
Maximum Good Subtree Score INPUT Tree Structure (rooted at 0) 0 val=1 1 val=2 2 val=3 3 val=4 4 Input Arrays: vals: 1 2 3 4 par: 0 0 1 1 ALGORITHM (DFS) 1 Build Digit Masks Track digits used in each value 1-->mask:0010, 2-->mask:0100 2 DFS Post-Order Process children before parent Visit: 3,4 --> 1 --> 2 --> 0 3 Merge DP States Combine child subsets if no digit conflicts (bitmask AND=0) 4 Find Max Good Subset For each node, track best sum Max Good Subset per Subtree: Node 3: max = 4 (only 4) Node 4: max = 0 (no value) Node 1: max = 2+4 = 6 Node 0: max = 1+2+3+4 = 10 FINAL RESULT Max Subset Sum per Node: Node 0 = 10 Node 1 = 6 Node 2 = 3 N3 = 4 N4 = 0 Sum of All Max Values: 10 + 6 + 3 + 4 + 0 = 15 Key Insight: Use bitmasks to track which digits (0-9) are used. Two values can be combined in a "good subset" only if their digit masks have no overlap (bitwise AND = 0). DFS merges child DP states while maintaining all reachable (mask, sum) pairs. Maximum sum for each subtree is tracked and summed. TutorialsPoint - Maximum Good Subtree Score | DFS with Bitmask DP
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28 Apple 22
36.2K Views
Medium-High Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen