Problem p00550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define rep(i, x) for (int i = 0; i < x; ++i)
#define all(a) begin(a), end(a)
#define PB push_back

const int inf = 1e9;

struct edge {
  int to, id;
};

vector<edge> G0[100010], G[100010];
vector<pair<int, int>> edges;
bool used[200010];
int d[100010];
int indeg[100010];

int N, M, Q;

void build_G() {
  queue<int> q;
  q.push(0);

  fill_n(d, N, inf);
  d[0] = 0;

  while (q.size()) {
    int v = q.front();
    q.pop();

    for (edge &e : G0[v]) {
      if (d[e.to] >= d[v] + 1) {
        used[e.id] = true;

        G[v].PB(edge{e.to, e.id});
        indeg[e.to]++;

        if (d[e.to] > d[v] + 1) {
          d[e.to] = d[v] + 1;
          q.push(e.to);
        }
      }
    }
  }
}

signed main() {
  cin >> N >> M >> Q;

  rep(i, M) {
    int U, V;
    cin >> U >> V;
    --U, --V;
    G0[U].PB(edge{V, i});
    G0[V].PB(edge{U, i});
    edges.PB(pair<int, int>(U, V));
  }

  build_G();

  int ans = 0;

  rep(i, Q) {
    int r;
    cin >> r;
    --r;

    if (!used[r]) {
      cout << ans << endl;
      continue;
    }

    used[r] = false;

    int u = edges[r].first, v = edges[r].second;

    if (d[u] > d[v])
      swap(u, v);

    // d[u] < d[v]
    indeg[v]--;

    if (indeg[v] == 0) {
      ans++;

      queue<int> q;
      q.push(v);

      while (q.size()) {
        int v = q.front();
        q.pop();

        for (edge &e : G[v]) {
          int u = e.to, id = e.id;

          if (!used[id])
            continue;
          used[id] = false;
          indeg[u]--;

          if (indeg[u] == 0) {
            ans++;
            q.push(u);
          }
        }
      }
    }

    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, x) for (int i = 0; i < x; ++i)
#define all(a) begin(a), end(a)
#define PB push_back

const int inf = 1e9;

struct edge {
  int to, id;
};

vector<edge> G0[100010];
vector<pair<int, int>> edges;
vector<int> G[100010];
bool used[100010];
int d[100010];
int indeg[100010];

int N, M, Q;

void build_G() {
  queue<int> q;
  q.push(0);

  fill_n(d, N, inf);
  d[0] = 0;

  while (q.size()) {
    int v = q.front();
    q.pop();

    for (edge &e : G0[v]) {
      if (d[e.to] >= d[v] + 1) {
        used[e.id] = true;

        G[v].PB(e.to);
        indeg[e.to]++;

        if (d[e.to] > d[v] + 1) {
          d[e.to] = d[v] + 1;
          q.push(e.to);
        }
      }
    }
  }
}

signed main() {
  cin >> N >> M >> Q;

  rep(i, M) {
    int U, V;
    cin >> U >> V;
    --U, --V;
    G0[U].PB(edge{V, i});
    G0[V].PB(edge{U, i});
    edges.PB(pair<int, int>(U, V));
  }

  build_G();

  int ans = 0;

  rep(i, Q) {
    int r;
    cin >> r;
    --r;

    if (!used[r]) {
      cout << ans << endl;
      continue;
    }

    int u = edges[r].first, v = edges[r].second;

    if (d[u] > d[v])
      swap(u, v);

    // d[u] < d[v]
    indeg[v]--;

    if (indeg[v] == 0) {
      ans++;

      queue<int> q;
      q.push(v);

      while (q.size()) {
        int v = q.front();
        q.pop();

        for (int u : G[v]) {
          indeg[u]--;

          if (indeg[u] == 0) {
            ans++;
            q.push(u);
          }
        }
      }
    }
    cout << ans << endl;
  }
}