Skip to content

Practice Power Grid Maintenance

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Problem Statement

Stations 1 through c form fixed connected components given by the bidirectional connections, and all begin online. Query [2, x] takes station x offline without changing connectivity. Query [1, x] returns x if it is online; otherwise it returns the smallest online station ID in x’s component, or -1 if none exists. Return the answers to type-1 queries in order.

Source: Power Grid Maintenance on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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]

# Example 2

Input
c = 3, connections = [], queries = [[1,1],[2,1],[1,1]]
Output
[1,-1]

Constraints

1 <= c <= 10^5
0 <= n == connections.length <= min(10^5, c * (c - 1) / 2)
connections[i].length == 2
1 <= ui, vi <= c
ui != vi
1 <= queries.length <= 2 * 10^5
queries[i].length == 2
queries[i][0] is either 1 or 2.
1 <= queries[i][1] <= c

Related LeetCode Problems

Practice more problems similar to Power Grid Maintenance.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.