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

#include <bits/stdc++.h>
#define p pair<int, int>
using namespace std;

p d[200000];
vector<int> rinsetu[100000];
int mincost[100000];
set<int> k[100000];
int l[100000];
queue<int> Q;
int main() {
  memset(mincost, -1, sizeof(mincost));
  int a, b, c;
  cin >> a >> b >> c;
  for (int e = 0; e < b; e++) {
    int f, g;
    scanf("%d%d", &f, &g);
    f--;
    g--;
    d[e] = p(f, g);
    rinsetu[f].push_back(g);
    rinsetu[g].push_back(f);
  }
  Q.push(0);
  mincost[0] = 0;
  while (Q.size()) {
    int h = Q.front();
    Q.pop();
    for (int i : rinsetu[h]) {
      if (mincost[i] == -1) {
        k[h].insert(i);
        l[i]++;
        mincost[i] = mincost[h] + 1;
        Q.push(i);
      } else if (mincost[i] == mincost[h] + 1) {
        k[h].insert(i);
        l[i]++;
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < c; i++) {
    int j;
    scanf("%d", &j);
    j--;
    auto v = k[d[j].first].find(d[j].second),
         t = k[d[j].second].find(d[j].first);
    if (v != k[d[j].first].end()) {
      k[d[j].first].erase(v);
      l[d[j].second]--;
      if (l[d[j].second] == 0) {
        queue<int> que;
        que.push(d[j].second);
        while (que.size()) {
          int u = que.front();
          que.pop();
          sum++;
          for (auto w = k[u].begin(); w != k[u].end(); w++) {
            l[*w]--;
            if (l[*w] == 0)
              que.push(*w);
            k[u].erase(w);
          }
          k[u].clear();
        }
      }
    }
    if (t != k[d[j].second].end()) {
      k[d[j].second].erase(t);
      l[d[j].first]--;
      if (l[d[j].first] == 0) {
        queue<int> que;
        que.push(d[j].first);
        while (que.size()) {
          int u = que.front();
          que.pop();
          sum++;
          for (auto w = k[u].begin(); w != k[u].end(); w++) {
            l[*w]--;
            if (l[*w] == 0)
              que.push(*w);
            k[u].erase(w);
          }
          k[u].clear();
        }
      }
    }
    cout << sum << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define p pair<int, int>
using namespace std;

p d[200000];
vector<int> rinsetu[100000];
int mincost[100000];
set<int> k[100000];
int l[100000];
queue<int> Q;
int main() {
  memset(mincost, -1, sizeof(mincost));
  int a, b, c;
  cin >> a >> b >> c;
  for (int e = 0; e < b; e++) {
    int f, g;
    scanf("%d%d", &f, &g);
    f--;
    g--;
    d[e] = p(f, g);
    rinsetu[f].push_back(g);
    rinsetu[g].push_back(f);
  }
  Q.push(0);
  mincost[0] = 0;
  while (Q.size()) {
    int h = Q.front();
    Q.pop();
    for (int i : rinsetu[h]) {
      if (mincost[i] == -1) {
        k[h].insert(i);
        l[i]++;
        mincost[i] = mincost[h] + 1;
        Q.push(i);
      } else if (mincost[i] == mincost[h] + 1) {
        k[h].insert(i);
        l[i]++;
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < c; i++) {
    int j;
    scanf("%d", &j);
    j--;
    auto v = k[d[j].first].find(d[j].second),
         t = k[d[j].second].find(d[j].first);
    if (v != k[d[j].first].end()) {
      k[d[j].first].erase(v);
      l[d[j].second]--;
      if (l[d[j].second] == 0) {
        queue<int> que;
        que.push(d[j].second);
        while (que.size()) {
          int u = que.front();
          que.pop();
          sum++;
          for (auto w = k[u].begin(); w != k[u].end(); w++) {
            l[*w]--;
            if (l[*w] == 0)
              que.push(*w);
            k[u].erase(w);
            w--;
          }
        }
      }
    } else if (t != k[d[j].second].end()) {
      k[d[j].second].erase(v);
      l[d[j].first]--;
      if (l[d[j].first] == 0) {
        queue<int> que;
        que.push(d[j].first);
        while (que.size()) {
          int u = que.front();
          que.pop();
          sum++;
          for (auto w = k[u].begin(); w != k[u].end(); w++) {
            l[*w]--;
            if (l[*w] == 0)
              que.push(*w);
            k[u].erase(w);
            w--;
          }
        }
      }
    }
    cout << sum << endl;
  }
}