Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// vector<vector<int> > dp;
// vector<vector<vector<int> > > vvvi;
// dp=vector<vector<int> >(N, vector<int>(M,0));
// vector<pair<int,int> > v;
// v.push_back(make_pair(x,y));
// priority_queue<int,vector<int>, greater<int> > q2;

int dist[100005][3];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vector<VI> to(N);
  REP(i, M) {
    int a, b;
    a--;
    b--;
    to[a].push_back(b);
  }
  int sv, tv;
  cin >> sv >> tv;
  sv--;
  tv--;
  REP(i, N) {
    REP(j, 3) { dist[i][j] = LLINF; }
  }
  queue<pii> q;
  q.push(pii(sv, 0));
  dist[sv][0] = 0;
  while (!q.empty()) {
    int v = q.front().first;
    int l = q.front().second;
    q.pop();

    for (int u : to[v]) {
      int nl = (l + 1) % 3;
      if (dist[u][nl] != LLINF)
        continue;
      dist[u][nl] = dist[v][l] + 1;
      q.push(pii(u, nl));
    }
  }
  int ans = dist[tv][0];
  if (ans == LLINF)
    ans = -1;
  else
    ans /= 3;

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// vector<vector<int> > dp;
// vector<vector<vector<int> > > vvvi;
// dp=vector<vector<int> >(N, vector<int>(M,0));
// vector<pair<int,int> > v;
// v.push_back(make_pair(x,y));
// priority_queue<int,vector<int>, greater<int> > q2;

int dist[100005][3];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vector<VI> to(N);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
  }
  int sv, tv;
  cin >> sv >> tv;
  sv--;
  tv--;
  REP(i, N) {
    REP(j, 3) { dist[i][j] = LLINF; }
  }
  queue<pii> q;
  q.push(pii(sv, 0));
  dist[sv][0] = 0;
  while (!q.empty()) {
    int v = q.front().first;
    int l = q.front().second;
    q.pop();

    for (int u : to[v]) {
      int nl = (l + 1) % 3;
      if (dist[u][nl] != LLINF)
        continue;
      dist[u][nl] = dist[v][l] + 1;
      q.push(pii(u, nl));
    }
  }
  int ans = dist[tv][0];
  if (ans == LLINF)
    ans = -1;
  else
    ans /= 3;

  cout << ans << endl;
  return 0;
}