3607. Power Grid Maintenance Medium

@problem@discussion
#Array#Hash Table#Depth-First Search#Breadth-First Search#Union-Find#Graph Theory#Heap (Priority Queue)#Ordered Set



1/**
2 * [3607] Power Grid Maintenance
3 *
4 * <p data-end="401" data-start="120">You are given an integer <code data-end="194" data-start="191">c representing <code data-end="211" data-start="208">c power stations, each with a unique identifier id from 1 to c (1‑based indexing).
5 * <p data-end="401" data-start="120">These stations are interconnected via <code data-end="295" data-start="292">n bidirectional cables, represented by a 2D array <code data-end="357" data-start="344">connections, where each element <code data-end="430" data-start="405">connections[i] = [ui, vi] indicates a connection between station ui and station vi. Stations that are directly or indirectly connected form a power grid.
6 * <p data-end="626" data-start="586">Initially, all stations are online (operational).
7 * <p data-end="720" data-start="628">You are also given a 2D array <code data-end="667" data-start="658">queries, where each query is one of the following two types:
8 * <ul data-end="995" data-start="722">
9 * 	<li data-end="921" data-start="722">
10 * 	<p data-end="921" data-start="724"><code data-end="732" data-start="724">[1, x]: A maintenance check is requested for station <code data-end="782" data-start="779">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.
11 * 	
12 * 	<li data-end="995" data-start="923">
13 * 	<p data-end="995" data-start="925"><code data-end="933" data-start="925">[2, x]: Station <code data-end="946" data-start="943">x goes offline (i.e., it becomes non-operational).
14 * 	
15 * 
16 * <p data-end="1106" data-start="997">Return an array of integers representing the results of each query of type <code data-end="1080" data-start="1072">[1, x] in the order they appear.
17 * <p data-end="1106" data-start="997">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.
18 *  
19 * <strong class="example">Example 1:
20 * <div class="example-block">
21 * Input: <span class="example-io">c = 5, connections = [[1,2],[2,3],[3,4],[4,5]], queries = [[1,3],[2,1],[1,1],[2,2],[1,2]]</span>
22 * Output: <span class="example-io">[3,2,3]</span>
23 * Explanation:
24 * <img alt="" src="https://assets.leetcode.com/uploads/2025/04/15/powergrid.jpg" style="width: 361px; height: 42px;" />
25 * 
26 * 	<li data-end="223" data-start="143">Initially, all stations {1, 2, 3, 4, 5} are online and form a single power grid.
27 * 	<li data-end="322" data-start="226">Query [1,3]: Station 3 is online, so the maintenance check is resolved by station 3.
28 * 	<li data-end="402" data-start="325">Query [2,1]: Station 1 goes offline. The remaining online stations are {2, 3, 4, 5}.
29 * 	<li data-end="557" data-start="405">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.
30 * 	<li data-end="641" data-start="560">Query [2,2]: Station 2 goes offline. The remaining online stations are {3, 4, 5}.
31 * 	<li data-end="800" data-start="644">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.
32 * </div>
33 * <strong class="example">Example 2:
34 * <div class="example-block">
35 * Input: <span class="example-io">c = 3, connections = [], queries = [[1,1],[2,1],[1,1]]</span>
36 * Output: <span class="example-io">[1,-1]</span>
37 * Explanation:
38 * 
39 * 	<li data-end="976" data-start="909">There are no connections, so each station is its own isolated grid.
40 * 	<li data-end="1096" data-start="979">Query [1,1]: Station 1 is online in its isolated grid, so the maintenance check is resolved by station 1.
41 * 	<li data-end="1135" data-start="1099">Query [2,1]: Station 1 goes offline.
42 * 	<li data-end="1237" data-start="1138">Query [1,1]: Station 1 is offline and there are no other stations in its grid, so the result is -1.
43 * </div>
44 *  
45 * Constraints:
46 * 
47 * 	<li data-end="155" data-start="139">1 <= c <= 10^5
48 * 	<li data-end="213" data-start="158">0 <= n == connections.length <= min(10^5, c * (c - 1) / 2)
49 * 	<li data-end="244" data-start="216">connections[i].length == 2
50 * 	<li data-end="295" data-start="247">1 <= ui, vi <= c
51 * 	<li data-end="338" data-start="298">ui != vi
52 * 	<li data-end="374" data-start="341">1 <= queries.length <= 2 * 10^5
53 * 	<li data-end="401" data-start="377">queries[i].length == 2
54 * 	<li data-end="436" data-start="404">queries[i][0] is either 1 or 2.
55 * 	<li data-end="462" data-start="439">1 <= queries[i][1] <= c
56 * 
57 */
58pub struct Solution {}
59
60// problem: https://leetcode.com/problems/power-grid-maintenance/
61// discuss: https://leetcode.com/problems/power-grid-maintenance/discuss/?currentPage=1&orderBy=most_votes&query=
62
63// submission codes start here
64
65impl Solution {
66    pub fn process_queries(c: i32, connections: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {
67        vec![]
68    }
69}
70
71// submission codes end
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_3607() {
79    }
80}
81

Back
© 2026 bowen.ge All Rights Reserved.