Problem solution · C++

Power Grid Maintenance

Power Grid Maintenance: 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
55 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 Power Grid Maintenance, 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

  • 55 lines of C++ from the credited upstream file power-grid-maintenance.cpp.
  • The implementation visibly relies on sequence storage.
  • 7 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 codePower Grid Maintenance · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(c + n + q), n = len(connections)// Space: O(c + n) // dfs, flood fill, sortclass Solution {public:    vector<int> processQueries(int c, vector<vector<int>>& connections, vector<vector<int>>& queries) {        vector<vector<int>> adj(c);        for (const auto& c : connections) {            adj[c[0] - 1].emplace_back(c[1] - 1);            adj[c[1] - 1].emplace_back(c[0] - 1);        }        vector<int> lookup(c, -1);        const auto& iter_dfs = [&](int i) {            vector<int> stk = {i};            while (!empty(stk)) {                const auto u = stk.back(); stk.pop_back();                if (lookup[u] != -1) {                    continue;                }                lookup[u] = i;                for (const auto& v : adj[u]) {                    stk.emplace_back(v);                }            }        };         for (int i = 0; i < c; ++i) {            iter_dfs(i);        }        vector<vector<int>> groups(c);        for (int i = c - 1; i >= 0; --i) {            groups[lookup[i]].emplace_back(i);        }        vector<int> result;        vector<bool> online(c, true);        for (const auto& q : queries) {            const auto t = q[0], x = q[1] - 1;            if (t == 1) {                if (online[x]) {                    result.emplace_back(x + 1);                    continue;                }                while (!empty(groups[lookup[x]]) && !online[groups[lookup[x]].back()]) {                    groups[lookup[x]].pop_back();                }                result.emplace_back(!empty(groups[lookup[x]]) ? groups[lookup[x]].back() + 1 : -1);            } else {                online[x] = false;            }        }        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 ↗