#3607

Power Grid Maintenance

medium · 56.3% accepted · 526 likes · top 50%

array · hash table · depth-first search · breadth-first search · union-find · graph theory · heap (priority queue) · ordered set

Description

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] = [ui, vi] indicates a connection between station ui and station vi`. 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 station x. If station x is online, it resolves the check by itself. If station x is offline, the check is resolved by the operational station with the smallest id in the same power grid as x. If no operational station exists in that grid, return -1.




[2, x]: Station x goes 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.

Solution