Problem solution · C++

Count Connected Subgraphs with Even Node Sum

Count Connected Subgraphs with Even Node Sum: 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
107 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 Count Connected Subgraphs with Even Node Sum, 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

  • 107 lines of C++ from the credited upstream file count-connected-subgraphs-with-even-node-sum.cpp.
  • The implementation visibly relies on sequence storage.
  • 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 codeCount Connected Subgraphs with Even Node Sum · C++C++
Use this to learn the idea, then write your own version.
// Time:  O((n + e) * 2^n)// Space: O(n + e) // bitmask, dfsclass Solution {public:    int evenSumSubgraphs(vector<int>& nums, vector<vector<int>>& edges) {        int odd_mask = 0;        const auto& even = [&](int mask) {            return !(__builtin_popcount(mask & odd_mask) % 2);        };         vector<vector<int>> adj(size(nums));        const auto& connected = [&](int mask) {            int i = 0;            for (; i < size(nums); ++i) {                if (mask & (1 << i)) {                    break;                }            }            mask ^= 1 << i;            vector<int> stk = {i};            while (!empty(stk)) {                const int u = stk.back(); stk.pop_back();                for (const auto& v : adj[u]) {                    if (!(mask & (1 << v))) {                        continue;                    }                    mask ^= 1 << v;                    stk.emplace_back(v);                }            }            return !mask;        };         for (const auto& e : edges) {            adj[e[0]].emplace_back(e[1]);            adj[e[1]].emplace_back(e[0]);        }        for (int i = 0; i < size(nums); ++i) {            if (nums[i]) {                odd_mask |= 1 << i;            }        }        int result = 0;        for (int mask = 1; mask < (1 << size(nums)); ++mask) {            if (even(mask) && connected(mask)) {                ++result;            }        }        return result;    }}; // Time:  O((n + e) * 2^n)// Space: O(n + e)// bitmask, dfsclass Solution2 {public:    int evenSumSubgraphs(vector<int>& nums, vector<vector<int>>& edges) {        const auto& even = [&](int mask) {            int parity = 0;            for (int i = 0; i < size(nums); ++i) {                if (mask & (1 << i)) {                    parity ^= nums[i];                }            }            return !parity;        };         vector<vector<int>> adj(size(nums));        const auto& connected = [&](int mask) {            int i = 0;            for (; i < size(nums); ++i) {                if (mask & (1 << i)) {                    break;                }            }            mask ^= 1 << i;            vector<int> stk = {i};            while (!empty(stk)) {                const int u = stk.back(); stk.pop_back();                for (const auto& v : adj[u]) {                    if (!(mask & (1 << v))) {                        continue;                    }                    mask ^= 1 << v;                    stk.emplace_back(v);                }            }            return !mask;        };         for (const auto& e : edges) {            adj[e[0]].emplace_back(e[1]);            adj[e[1]].emplace_back(e[0]);        }        int result = 0;        for (int mask = 1; mask < (1 << size(nums)); ++mask) {            if (even(mask) && connected(mask)) {                ++result;            }        }        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 ↗