Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define loop(i, n) for (int i = 0; i < int(n); i++)
#define rloop(i, n) for (int i = int(n); i >= 0; i--)
#define range(i, a, b) for (int i = int(a); i <= int(b); i++)
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define sfi1(v) scanf("%d", &v)
#define sfi2(v1, v2) scanf("%d %d", &v1, &v2)
#define sfi3(v1, v2, v3) scanf("%d %d %d", &v1, &v2, &v3)
#define sfll1(v) scanf("%I64d", &v);
#define sfll2(v1, v2) scanf("%I64d %I64d", &v1, &v2)
#define sfll3(v1, v2, v3) scanf("%I64d %I64d %I64d", &v1, &v2, &v3)
#define endl '\n'

typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5 + 5;
int dis[N];
set<int> adj[3][N];
vi radj[3][N];
int n, m;

void Remove(int u, int i) {
  for (auto nxt : radj[i][u]) {
    adj[i][nxt].erase(u);
  }
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  // freopen("out.in", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int a, b, s, t;
  cin >> n >> m;
  loop(i, m) {
    cin >> a >> b;
    loop(j, 3) {
      adj[j][a].insert(b);
      radj[j][b].pb(a);
    }
  }

  cin >> s >> t;
  memset(dis, -1, sizeof dis);
  dis[s] = 0;
  queue<int> q;
  q.push(s);
  Remove(s, 2);

  int tmp;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto it0 = adj[0][u].begin(); it0 != adj[0][u].end();) {
      for (auto it1 = adj[1][*it0].begin(); it1 != adj[1][*it0].end();) {
        for (auto it2 = adj[2][*it1].begin(); it2 != adj[2][*it1].end();) {
          dis[*it2] = dis[u] + 1;
          q.push(*it2);
          tmp = *it2;
          it2++;
          Remove(tmp, 2);
        }
        tmp = *it1;
        it1++;
        Remove(tmp, 1);
      }
      tmp = *it0;
      it0++;
      Remove(tmp, 0);
    }
  }

  cout << dis[t] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define loop(i, n) for (int i = 0; i < int(n); i++)
#define rloop(i, n) for (int i = int(n); i >= 0; i--)
#define range(i, a, b) for (int i = int(a); i <= int(b); i++)
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define sfi1(v) scanf("%d", &v)
#define sfi2(v1, v2) scanf("%d %d", &v1, &v2)
#define sfi3(v1, v2, v3) scanf("%d %d %d", &v1, &v2, &v3)
#define sfll1(v) scanf("%I64d", &v);
#define sfll2(v1, v2) scanf("%I64d %I64d", &v1, &v2)
#define sfll3(v1, v2, v3) scanf("%I64d %I64d %I64d", &v1, &v2, &v3)
#define endl '\n'

typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5 + 5;
int dis[N];
set<int> adj[3][N];
vi radj[3][N];
int n, m;

void Remove(int u, int i) {
  for (auto nxt : radj[i][u]) {
    adj[i][nxt].erase(u);
  }
}

int main() {

#ifndef ONLINE_JUDGE
//    freopen("in.in", "r", stdin);
// freopen("out.in", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int a, b, s, t;
  cin >> n >> m;
  loop(i, m) {
    cin >> a >> b;
    loop(j, 3) {
      adj[j][a].insert(b);
      radj[j][b].pb(a);
    }
  }

  cin >> s >> t;
  memset(dis, -1, sizeof dis);
  dis[s] = 0;
  queue<int> q;
  q.push(s);
  Remove(s, 2);

  int tmp;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto it0 = adj[0][u].begin(); it0 != adj[0][u].end();) {
      for (auto it1 = adj[1][*it0].begin(); it1 != adj[1][*it0].end();) {
        for (auto it2 = adj[2][*it1].begin(); it2 != adj[2][*it1].end();) {
          dis[*it2] = dis[u] + 1;
          q.push(*it2);
          tmp = *it2;
          it2++;
          Remove(tmp, 2);
        }
        tmp = *it1;
        it1++;
        Remove(tmp, 1);
      }
      tmp = *it0;
      it0++;
      Remove(tmp, 0);
    }
  }

  cout << dis[t] << endl;

  return 0;
}