3607. Power Grid Maintenance #2387
-
|
Topics: You are given an integer These stations are interconnected via Initially, all stations are online (operational). You are also given a 2D array
Return an array of integers representing the results of each query of type 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:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to handle two types of queries on a power grid where stations can go offline, and I need to find the smallest operational station in the same connected component. Approach
Let's implement this solution in PHP: 3607. Power Grid Maintenance <?php
/**
* @param Integer $c
* @param Integer[][] $connections
* @param Integer[][] $queries
* @return Integer[]
*/
function processQueries($c, $connections, $queries) {
// Build graph
$graph = array_fill(1, $c, []);
foreach ($connections as $conn) {
$graph[$conn[0]][] = $conn[1];
$graph[$conn[1]][] = $conn[0];
}
// Find connected components using BFS
$visited = array_fill(1, $c, false);
$componentId = array_fill(1, $c, 0);
$componentStations = [];
$compId = 1;
for ($i = 1; $i <= $c; $i++) {
if (!$visited[$i]) {
$queue = new SplQueue();
$queue->enqueue($i);
$visited[$i] = true;
$componentId[$i] = $compId;
$componentStations[$compId] = [$i];
while (!$queue->isEmpty()) {
$current = $queue->dequeue();
foreach ($graph[$current] as $neighbor) {
if (!$visited[$neighbor]) {
$visited[$neighbor] = true;
$componentId[$neighbor] = $compId;
$componentStations[$compId][] = $neighbor;
$queue->enqueue($neighbor);
}
}
}
$compId++;
}
}
// Create min-heaps for each component
$componentHeaps = [];
$online = array_fill(1, $c, true);
foreach ($componentStations as $id => $stations) {
$heap = new SplMinHeap();
foreach ($stations as $station) {
$heap->insert($station);
}
$componentHeaps[$id] = $heap;
}
$result = [];
foreach ($queries as $query) {
$type = $query[0];
$station = $query[1];
$compId = $componentId[$station];
$heap = $componentHeaps[$compId];
if ($type == 1) {
// Maintenance check
if ($online[$station]) {
$result[] = $station;
} else {
// Find smallest operational station using lazy heap
while (!$heap->isEmpty() && !$online[$heap->top()]) {
$heap->extract();
}
if ($heap->isEmpty()) {
$result[] = -1;
} else {
$result[] = $heap->top();
}
}
} else {
// Station goes offline
$online[$station] = false;
}
}
return $result;
}
// Test cases
print_r(processQueries(5, [[1,2],[2,3],[3,4],[4,5]], [[1,3],[2,1],[1,1],[2,2],[1,2]])); // Output: [3, 2, 3]
print_r(processQueries(3, [], [[1,1],[2,1],[1,1]]); // Output: [1,-1]
?>Explanation:
Complexity
Space Complexity: O(c + n) for storing graph and component data |
Beta Was this translation helpful? Give feedback.

We need to handle two types of queries on a power grid where stations can go offline, and I need to find the smallest operational station in the same connected component.
Approach
Union-Find for Connectivity: First, I'll use Union-Find to group stations into connected components based on the given connections.
Track Operational Status: Maintain an array to track which stations are operational (online).
Component Minimum Tracking: For each component, track the smallest operational station ID. When stations go offline, I need to efficiently update this minimum.
Query Processing: