Approach
Depth-first search
For Maximum Good Subtree Score, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 131 lines of C++ from the credited upstream file maximum-good-subtree-score.cpp.
- The implementation visibly relies on sequence storage, hash lookup, cached states.
- 12 loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 int goodSubtreeSum(vector<int>& vals, vector<int>& par) {8 static const int MOD = 1e9 + 7;9 10 vector<vector<int>> adj(size(vals));11 const auto& get_mask = [](int x) {12 int mask = 0;13 for (; x; x /= 10) {14 const int d = x % 10;15 if (mask & (1 << d)) {16 return -1;17 }18 mask |= 1 << d;19 }20 return mask;21 };22 23 const auto& iter_dfs = [&]() {24 int result = 0;25 using RET = unordered_map<int, int>;26 RET ret;27 vector<tuple<int, int, int, shared_ptr<RET>, RET *>> stk = {{1, 0, -1, nullptr, &ret}};28 while (!empty(stk)) {29 const auto [step, u, i, new_ret, ret] = stk.back(); stk.pop_back();30 if (step == 1) {31 (*ret)[0] = 0;32 const auto& mask = get_mask(vals[u]);33 if (mask != -1) {34 (*ret)[mask] = vals[u];35 }36 stk.emplace_back(4, u, -1, nullptr, ret);37 stk.emplace_back(2, u, 0, nullptr, ret);38 } else if (step == 2) {39 if (i == size(adj[u])) {40 continue;41 }42 const auto& v = adj[u][i];43 stk.emplace_back(2, u, i + 1, nullptr, ret);44 const auto& new_ret = make_shared<RET>();45 stk.emplace_back(3, -1, -1, new_ret, ret);46 stk.emplace_back(1, v, -1, nullptr, new_ret.get());47 } else if (step == 3) {48 unordered_map<int, int> copy_dp(*ret);49 for (const auto& [m1, v1] : copy_dp) {50 for (const auto& [m2, v2] : *new_ret) {51 if (m1 & m2) {52 continue;53 }54 (*ret)[m1 | m2] = max((*ret)[m1 | m2], v1 + v2);55 }56 }57 } else if (step == 4) {58 int mx = 0;59 for (const auto& [_, v] : *ret) {60 mx = max(mx, v);61 }62 result = (result + mx) % MOD;63 }64 }65 return result;66 };67 68 for (int u = 1; u < size(par); ++u) {69 adj[par[u]].emplace_back(u);70 }71 return iter_dfs();72 }73};74 75767778class Solution2 {79public:80 int goodSubtreeSum(vector<int>& vals, vector<int>& par) {81 static const int MOD = 1e9 + 7;82 83 int result = 0;84 vector<vector<int>> adj(size(vals));85 const auto& get_mask = [](int x) {86 int mask = 0;87 for (; x; x /= 10) {88 const int d = x % 10;89 if (mask & (1 << d)) {90 return -1;91 }92 mask |= 1 << d;93 }94 return mask;95 };96 97 const function<unordered_map<int, int> (int)> dfs = [&](int u) {98 unordered_map<int, int> dp;99 dp[0] = 0;100 const auto& mask = get_mask(vals[u]);101 if (mask != -1) {102 dp[mask] = vals[u];103 }104 for (const auto& v : adj[u]) {105 const auto& new_dp = dfs(v);106 unordered_map<int, int> copy_dp(dp);107 for (const auto& [m1, v1] : copy_dp) {108 for (const auto& [m2, v2] : new_dp) {109 if (m1 & m2) {110 continue;111 }112 dp[m1 | m2] = max(dp[m1 | m2], v1 + v2);113 }114 }115 }116 int mx = 0;117 for (const auto& [_, v] : dp) {118 mx = max(mx, v);119 }120 result = (result + mx) % MOD;121 return dp;122 };123 124 for (int u = 1; u < size(par); ++u) {125 adj[par[u]].emplace_back(u);126 }127 dfs(0);128 return result;129 }130};131