Power Grid Maintenance Problem
Description
LeetCode Problem 3607.
You are given an integer c representing c power stations, each with a unique identifier id from 1 to c (1‑based indexing).
These stations are interconnected via n bidirectional cables, represented by a 2D array connections, where each element connections[i] = [u_i, v_i] indicates a connection between station u_i and station v_i. Stations that are directly or indirectly connected form a power grid.
Initially, all stations are online (operational).
You are also given a 2D array queries, where each query is one of the following two types:
[1, x]: A maintenance check is requested for stationx. If stationxis online, it resolves the check by itself. If stationxis offline, the check is resolved by the operational station with the smallestidin the same power grid asx. If no operational station exists in that grid, return -1.[2, x]: Stationxgoes offline (i.e., it becomes non-operational).
Return an array of integers representing the results of each query of type [1, x] in the order they appear.
Note: The power grid preserves its structure; an offline (non‑operational) node remains part of its grid and taking it offline does not alter connectivity.
Example 1:
Input: c = 5, connections = [[1,2],[2,3],[3,4],[4,5]], queries = [[1,3],[2,1],[1,1],[2,2],[1,2]]
Output: [3,2,3]
Explanation:

- Initially, all stations {1, 2, 3, 4, 5} are online and form a single power grid.
- Query [1,3]: Station 3 is online, so the maintenance check is resolved by station 3.
- Query [2,1]: Station 1 goes offline. The remaining online stations are {2, 3, 4, 5}.
- Query [1,1]: Station 1 is offline, so the check is resolved by the operational station with the smallest id among {2, 3, 4, 5}, which is station 2.
- Query [2,2]: Station 2 goes offline. The remaining online stations are {3, 4, 5}.
- Query [1,2]: Station 2 is offline, so the check is resolved by the operational station with the smallest id among {3, 4, 5}, which is station 3.
Example 2:
Input: c = 3, connections = [], queries = [[1,1],[2,1],[1,1]]
Output: [1,-1]
Explanation:
- There are no connections, so each station is its own isolated grid.
- Query [1,1]: Station 1 is online in its isolated grid, so the maintenance check is resolved by station 1.
- Query [2,1]: Station 1 goes offline.
- Query [1,1]: Station 1 is offline and there are no other stations in its grid, so the result is -1.
Constraints:
1 <= c <= 10^50 <= n == connections.length <= min(10^5, c * (c - 1) / 2)connections[i].length == 21 <= u_i, v_i <= cu_i != v_i1 <= queries.length <= 2 * 10^5queries[i].length == 2queries[i][0]is either 1 or 2.1 <= queries[i][1] <= c
Solution
Approach: Union-Find + Sorted Lists with Pointers
Use a union-find to group stations into grids. Then list the stations of every grid in increasing order and give each grid a pointer to its smallest station that might still be online.
- Query
[2, x]: markxoffline. - Query
[1, x]: ifxis online, the answer isx. Otherwise move the pointer ofx's grid past every offline station and answer with the station it lands on, or-1if it runs off the end.
Stations only go offline and never return, so each pointer only moves forward and the total pointer movement is at most c.
Complexity: O((c + n + q) alpha(c)) time and O(c) extra space.
class Solution:
def processQueries(
self, c: int, connections: List[List[int]], queries: List[List[int]]
) -> List[int]:
p = list(range(c + 1))
def find(x: int) -> int:
while p[x] != x:
p[x] = p[p[x]]
x = p[x]
return x
for u, v in connections:
p[find(u)] = find(v)
# stations of each grid in increasing order, with a pointer past the offline ones
members = [[] for _ in range(c + 1)]
for i in range(1, c + 1):
members[find(i)].append(i)
ptr = [0] * (c + 1)
online = [True] * (c + 1)
ans = []
for a, x in queries:
if a == 1:
if online[x]:
ans.append(x)
continue
root = find(x)
lst = members[root]
while ptr[root] < len(lst) and not online[lst[ptr[root]]]:
ptr[root] += 1
ans.append(lst[ptr[root]] if ptr[root] < len(lst) else -1)
else:
online[x] = False
return ans