2025-11-06 20:39:45 +0300 MSK
Power Grid Maintenance
Links
Code
class DSU:
def __init__(self, size):
self.parent = list(range(size))
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def join(self, u, v):
self.parent[self.find(v)] = self.find(u)
class Solution:
def processQueries(
self, c: int, connections: List[List[int]], queries: List[List[int]]
) -> List[int]:
dsu = DSU(c + 1)
for p in connections:
dsu.join(p[0], p[1])
online = [True] * (c + 1)
offline_counts = [0] * (c + 1)
minimum_online_stations = {}
for q in queries:
op, x = q[0], q[1]
if op == 2:
online[x] = False
offline_counts[x] += 1
for i in range(1, c + 1):
root = dsu.find(i)
if root not in minimum_online_stations:
minimum_online_stations[root] = -1
station = minimum_online_stations[root]
if online[i]:
if station == -1 or station > i:
minimum_online_stations[root] = i
ans = []
for i in range(len(queries) - 1, -1, -1):
op, x = queries[i][0], queries[i][1]
root = dsu.find(x)
station = minimum_online_stations[root]
if op == 1:
if online[x]:
ans.append(x)
else:
ans.append(station)
if op == 2:
if offline_counts[x] > 1:
offline_counts[x] -= 1
else:
online[x] = True
if station == -1 or station > x:
minimum_online_stations[root] = x
return ans[::-1]