Problem p00550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
#define INF 1e+9
using namespace std;

int main() {
  int n, m, q, u[200000], v[200000], qs[200000], d[100000];
  vector<int> G[100000], g[100000];
  vector<int> cnt;
  bool connect[100000] = {}, used[200000] = {};
  cin >> n >> m >> q;
  connect[0] = true;
  for (int i = 0; i < n; i++)
    d[i] = INF;
  for (int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    g[v[i]].push_back(u[i]);
    g[u[i]].push_back(v[i]);
  }
  queue<int> que;
  que.push(0);
  d[0] = 0;
  for (int j = 0;; j++) {
    int siz = que.size();
    if (siz == 0)
      break;
    for (int k = 0; k < siz; k++) {
      int V = que.front();
      que.pop();
      for (int i = 0; i < g[V].size(); i++) {
        if (d[g[V][i]] > j + 1) {
          d[g[V][i]] = j + 1;
          que.push(g[V][i]);
        }
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> qs[i];
    qs[i]--;
    used[qs[i]] = true;
  }
  for (int i = 0; i < m; i++) {
    if (!used[i]) {
      G[u[i]].push_back(v[i]);
      G[v[i]].push_back(u[i]);
    }
  }
  int res = n - 1;
  que.push(0);
  for (int j = 0;; j++) {
    int siz = que.size();
    if (siz == 0)
      break;
    for (int k = 0; k < siz; k++) {
      int V = que.front();
      que.pop();
      for (int i = 0; i < G[V].size(); i++) {
        if (!connect[G[V][i]] && d[G[V][i]] == j + 1) {
          connect[G[V][i]] = true;
          que.push(G[V][i]);
          res--;
        }
      }
    }
  }
  cnt.push_back(res);
  for (int i = q - 1; i >= 1; i--) {
    int tu = u[qs[i]], tv = v[qs[i]];
    G[tu].push_back(tv);
    G[tv].push_back(tu);
    if (connect[tu] && connect[tv]) {
      cnt.push_back(res);
      continue;
    }
    if (connect[tv]) {
      int t = tu;
      tu = tv;
      tv = t;
    }
    if (!connect[tu] || d[tv] < d[tu] + 1) {
      cnt.push_back(res);
      continue;
    }
    connect[tv] = true;
    que.push(tv);
    res--;
    for (int k = 0;; k++) {
      int siz = que.size();
      if (siz == 0)
        break;
      for (int l = 0; l < siz; l++) {
        int V = que.front();
        que.pop();
        for (int j = 0; j < G[V].size(); j++) {
          if (!connect[G[V][j]] && d[G[V][j]] == d[tu] + k + 2) {
            connect[G[V][j]] = true;
            que.push(G[V][j]);
            res--;
          }
        }
      }
    }
    cnt.push_back(res);
  }
  for (int i = q - 1; i >= 0; i--)
    cout << cnt[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
#define INF 1e+18
#define int long long
using namespace std;

signed main() {
  int n, m, q, us[200000], vs[200000];
  vector<int> G[100000];
  int in[100000] = {}, d[100000];
  cin >> n >> m >> q;
  for (int i = 0; i < n; i++) {
    d[i] = INF;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    us[i] = u - 1;
    vs[i] = v - 1;
    G[u - 1].push_back(v - 1);
    G[v - 1].push_back(u - 1);
  }
  queue<int> que;
  que.push(0);
  d[0] = 0;
  for (int i = 0;; i++) {
    if (que.empty())
      break;
    int siz = que.size();
    for (int j = 0; j < siz; j++) {
      int v = que.front();
      que.pop();
      for (int k = 0; k < G[v].size(); k++) {
        int u = G[v][k];
        if (d[u] > i + 1) {
          d[u] = i + 1;
          que.push(u);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < G[i].size(); j++) {
      if (d[i] >= d[G[i][j]])
        G[i].erase(G[i].begin() + j--);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < G[i].size(); j++) {
      in[G[i][j]]++;
    }
  }
  int cnt = 0;
  for (int i = 0; i < q; i++) {
    int r, vv = -1;
    cin >> r;
    r--;
    for (int j = 0; j < G[us[r]].size(); j++) {
      if (G[us[r]][j] == vs[r]) {
        vv = vs[r];
        in[vv]--;
        G[us[r]].erase(G[us[r]].begin() + j--);
      }
    }
    for (int j = 0; j < G[vs[r]].size(); j++) {
      if (G[vs[r]][j] == us[r]) {
        vv = us[r];
        in[vv]--;
        G[vs[r]].erase(G[vs[r]].begin() + j--);
      }
    }
    if (vv != -1) {
      if (in[vv]) {
        cout << cnt << endl;
        continue;
      }
      que.push(vv);
      cnt++;
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (int j = 0; j < G[v].size(); j++) {
          in[G[v][j]]--;
          if (in[G[v][j]] == 0) {
            cnt++;
            que.push(G[v][j]);
          }
          G[v].erase(G[v].begin() + j--);
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}