Problem p00550 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define fir first
#define sec second
int R[200002];
const int INF = 1001001001;

vector<pair<int, int>> edge[100002];
int d[100002];
int s[100002];
int sans[200002];
int N, M, Q;
int main() {
  scanf("%d %d %d", &N, &M, &Q);
  rep(i, M) {
    R[i] = INF;
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    edge[u].pb(make_pair(v, i));
    edge[v].pb(make_pair(u, i));
  }
  rep(i, Q) {
    int r;
    scanf("%d", &r);
    r--;
    R[r] = i;
  }

  fill(&d[0], &d[N + 1], INF);
  fill(&s[0], &s[N + 1], INF);
  // dijkstra
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push(make_pair(0, 0));
  while (q.size()) {
    pair<int, int> p = q.top();
    q.pop();
    int n = p.second;
    int c = p.first;
    if (d[n] < c)
      continue;
    rep(i, edge[n].size()) {
      int to = edge[n][i].fir;
      int ro = edge[n][i].sec;
      if (c + 1 == d[to]) {
        s[to] = max(s[to], min(s[n], R[ro]));
        if (s[to] < min(s[n], R[ro]))
          q.push(make_pair(c + 1, to));
      } else if (c + 1 < d[to]) {
        s[to] = min(s[n], R[ro]);
        d[to] = c + 1;
        if (s[to] < min(s[n], R[ro]))
          q.push(make_pair(c + 1, to));
        q.push(make_pair(c + 1, to));
      }
    }
  }
  rep(i, N) {
    if (i != 0)
      sans[s[i]]++;
  }
  int ans = 0;
  rep(i, Q) {
    ans += sans[i];
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define fir first
#define sec second
int R[200002];
const int INF = 1001001001;

vector<pair<int, int>> edge[100002];
int d[100002];
int s[100002];
int sans[200002];
int N, M, Q;
int main() {
  scanf("%d %d %d", &N, &M, &Q);
  rep(i, M) {
    R[i] = INF;
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    edge[u].pb(make_pair(v, i));
    edge[v].pb(make_pair(u, i));
  }
  rep(i, Q) {
    int r;
    scanf("%d", &r);
    r--;
    R[r] = i;
  }

  fill(&d[0], &d[N + 1], INF);
  fill(&s[0], &s[N + 1], INF);
  // dijkstra
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push(make_pair(0, 0));
  while (q.size()) {
    pair<int, int> p = q.top();
    q.pop();
    int n = p.second;
    int c = p.first;
    if (d[n] < c)
      continue;
    rep(i, edge[n].size()) {
      int to = edge[n][i].fir;
      int ro = edge[n][i].sec;
      if (c + 1 == d[to]) {
        s[to] = max(s[to], min(s[n], R[ro]));
        if (s[to] < min(s[n], R[ro]))
          q.push(make_pair(c + 1, to));
      } else if (c + 1 < d[to]) {
        s[to] = min(s[n], R[ro]);
        d[to] = c + 1;
        if (s[to] < min(s[n], R[ro]))
          q.push(make_pair(c + 1, to));
        q.push(make_pair(c + 1, to));
      }
    }
  }
  rep(i, N) {
    if (i != 0 && s[i] < Q)
      sans[s[i]]++;
  }
  int ans = 0;
  rep(i, Q) {
    ans += sans[i];
    printf("%d\n", ans);
  }
  return 0;
}