Tap here to switch tabs
Problem
Submissions

Problem: Power Grid Maintenance

med
30 min
Try to solve the Power Grid Maintenance problem.

Statement

You are given c power stations labeled from 11 to c, and an undirected network described by connections, where each pair [u, v] means stations u and v are directly connected. These connections define connected components (power grids).

Initially, every station is online.

You must process queries in order. Each query is a pair [type, x] with the following meaning.

  • If type equals 11, perform a maintenance check for station x and return a station id as follows.

    • If station x is currently online, return x.

    • Otherwise, consider the connected component (in the original graph defined by connections) that contains x, and among the stations in that component that are currently online, return the smallest station id. If no station in that component is online, return −1-1.

  • If type equals 22, station x goes offline. Once a station is offline, it stays offline for the rest of the queries.

Return an array containing the results of all queries with type equal to 11, in the same order they appear.

Constraints:

  • 1≤1 \leq c ≤105\leq 10^5

  • 0≤0 \leq n ==== connections.length ≤min⁡(105,c⋅(c−1)2)\leq \min\left(10^5, \frac{c \cdot (c - 1)}{2}\right)

  • connections[i].length ==== 22

  • 1≤1 \leq u_i, v_i ≤\leq c

  • u_i ≠\neq v_i

  • 1≤1 \leq queries.length ≤2⋅105\leq 2 \cdot 10^5

  • queries[i].length ==== 22

  • queries[i][0] is either 11 or 22

  • 1≤1 \leq queries[i][1] ≤\leq c

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Power Grid Maintenance

med
30 min
Try to solve the Power Grid Maintenance problem.

Statement

You are given c power stations labeled from 11 to c, and an undirected network described by connections, where each pair [u, v] means stations u and v are directly connected. These connections define connected components (power grids).

Initially, every station is online.

You must process queries in order. Each query is a pair [type, x] with the following meaning.

  • If type equals 11, perform a maintenance check for station x and return a station id as follows.

    • If station x is currently online, return x.

    • Otherwise, consider the connected component (in the original graph defined by connections) that contains x, and among the stations in that component that are currently online, return the smallest station id. If no station in that component is online, return −1-1.

  • If type equals 22, station x goes offline. Once a station is offline, it stays offline for the rest of the queries.

Return an array containing the results of all queries with type equal to 11, in the same order they appear.

Constraints:

  • 1≤1 \leq c ≤105\leq 10^5

  • 0≤0 \leq n ==== connections.length ≤min⁡(105,c⋅(c−1)2)\leq \min\left(10^5, \frac{c \cdot (c - 1)}{2}\right)

  • connections[i].length ==== 22

  • 1≤1 \leq u_i, v_i ≤\leq c

  • u_i ≠\neq v_i

  • 1≤1 \leq queries.length ≤2⋅105\leq 2 \cdot 10^5

  • queries[i].length ==== 22

  • queries[i][0] is either 11 or 22

  • 1≤1 \leq queries[i][1] ≤\leq c