Problem solution · C++

Maximum Good Subtree Score

Maximum Good Subtree Score: a C++ solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Depth-first search
Source
Kamyu LeetCode Solutions
Length
131 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

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.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. 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.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMaximum Good Subtree Score · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n * (2^10)^2)// Space: O(2^10) // bitmasks, iterative dfs, tree dpclass Solution {public:    int goodSubtreeSum(vector<int>& vals, vector<int>& par) {        static const int MOD = 1e9 + 7;         vector<vector<int>> adj(size(vals));        const auto& get_mask = [](int x) {            int mask = 0;            for (; x; x /= 10) {                const int d = x % 10;                if (mask & (1 << d)) {                    return -1;                }                mask |= 1 << d;            }            return mask;        };         const auto& iter_dfs = [&]() {            int result = 0;            using RET = unordered_map<int, int>;            RET ret;            vector<tuple<int, int, int, shared_ptr<RET>, RET *>> stk = {{1, 0, -1, nullptr, &ret}};            while (!empty(stk)) {                const auto [step, u, i, new_ret, ret] = stk.back(); stk.pop_back();                if (step == 1) {                    (*ret)[0] = 0;                    const auto& mask = get_mask(vals[u]);                    if (mask != -1) {                        (*ret)[mask] = vals[u];                    }                    stk.emplace_back(4, u, -1, nullptr, ret);                    stk.emplace_back(2, u, 0, nullptr, ret);                } else if (step == 2) {                    if (i == size(adj[u])) {                        continue;                    }                    const auto& v = adj[u][i];                    stk.emplace_back(2, u, i + 1, nullptr, ret);                    const auto& new_ret = make_shared<RET>();                    stk.emplace_back(3, -1, -1, new_ret, ret);                    stk.emplace_back(1, v, -1, nullptr, new_ret.get());                } else if (step == 3) {                    unordered_map<int, int> copy_dp(*ret);                    for (const auto& [m1, v1] : copy_dp) {                        for (const auto& [m2, v2] : *new_ret) {                            if (m1 & m2) {                                continue;                            }                            (*ret)[m1 | m2] = max((*ret)[m1 | m2], v1 + v2);                        }                    }                } else if (step == 4) {                    int mx = 0;                    for (const auto& [_, v] : *ret) {                        mx = max(mx, v);                    }                    result = (result + mx) % MOD;                }            }            return result;        };         for (int u = 1; u < size(par); ++u) {            adj[par[u]].emplace_back(u);        }        return iter_dfs();    }}; // Time:  O(n * (2^10)^2)// Space: O(2^10)// bitmasks, dfs, tree dpclass Solution2 {public:    int goodSubtreeSum(vector<int>& vals, vector<int>& par) {        static const int MOD = 1e9 + 7;         int result = 0;        vector<vector<int>> adj(size(vals));        const auto& get_mask = [](int x) {            int mask = 0;            for (; x; x /= 10) {                const int d = x % 10;                if (mask & (1 << d)) {                    return -1;                }                mask |= 1 << d;            }            return mask;        };         const function<unordered_map<int, int> (int)> dfs = [&](int u) {            unordered_map<int, int> dp;            dp[0] = 0;            const auto& mask = get_mask(vals[u]);            if (mask != -1) {                dp[mask] = vals[u];            }            for (const auto& v : adj[u]) {                const auto& new_dp = dfs(v);                unordered_map<int, int> copy_dp(dp);                for (const auto& [m1, v1] : copy_dp) {                    for (const auto& [m2, v2] : new_dp) {                        if (m1 & m2) {                            continue;                        }                        dp[m1 | m2] = max(dp[m1 | m2], v1 + v2);                    }                }            }            int mx = 0;            for (const auto& [_, v] : dp) {                mx = max(mx, v);            }            result = (result + mx) % MOD;            return dp;        };         for (int u = 1; u < size(par); ++u) {            adj[par[u]].emplace_back(u);        }        dfs(0);        return result;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗