Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;
// const ll RANDOM =
// chrono::high_resolution_clock::now().time_since_epoch().count();
// gp_hash_table<int, int> table;

#define ll long long int
#define vii vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define all(x) x.begin(), x.end()
#define S second
#define F first
#define ins insert
#define ld long double
#define lp(l, r) for (i = l; i <= r; ++i)

#define lpj(l, r) for (j = l; j <= r; ++j)
#define lpr(l, r) for (i = r; i >= l; --i)
#define lprj(l, r) for (j = r; j >= l; --j)

#define swap(a, b)                                                             \
  {                                                                            \
    auto _t = a;                                                               \
    a = b, b = _t;                                                             \
  }
#define flush() cout.flush()
#define popcount __builtin_popcount

// typedef __int128 bign;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define MD 1000000007
#define M 3000
#define LN 14
#define fftmod 998244353
#define EPS (double)1e-10

ll md = MD;
ll inf = 1e18;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

inline ll exp(ll a, ll b) {
  ll r = 1LL;
  while (b > 0) {
    if (b & 1) {
      r = r * (a % md);
      r = (r + md) % md;
    }
    b /= 2;
    a = (a % md) * (a % md);
    a = (a + md) % md;
  }
  return (r + md) % md;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  if (a == 0)
    return b;
  return gcd(b, a % b);
}
inline ll poww(ll a, ll b) {
  ll r = 1LL;
  while (b > 0) {
    if (b & 1)
      r = r * a;
    b /= 2;
    a = a * a;
  }
  return (ll)r;
}

using cd = complex<double>;
const double PI = acos(-1);

inline ll rand(ll l, ll r) { return l + rng() % (r - l + 1); }

vii g[M];
ll in[M], out[M];
ll vis[M][3];
ll dp[M][3];
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // ifstream fin("input.txt");ofstream fout("outut.txt");
  ll t, i, j, m, n, s, z, k, p, T;
  string s1, s2;
  ll qa;
  ll x, y;
  cin >> n >> m;
  lp(1, m) {
    cin >> x >> y;
    g[x].pb(y);
  }
  cin >> x >> y;
  memset(dp, -1, sizeof(dp));
  dp[x][0] = 0;
  queue<pii> q;
  q.push({x, 0});
  while (!q.empty()) {
    pii u = q.front();
    q.pop();
    vis[u.F][u.S] = 1;
    for (auto v : g[u.F]) {
      ll nx = u.S + 1;
      nx %= 3;
      if (!vis[v][nx]) {
        vis[v][nx] = 1;
        dp[v][nx] = dp[u.F][u.S] + 1;
        q.push({v, nx});
      }
    }
  }
  if (dp[y][0] == -1)
    cout << -1 << "\n";
  else
    cout << dp[y][0] / 3 << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;
// const ll RANDOM =
// chrono::high_resolution_clock::now().time_since_epoch().count();
// gp_hash_table<int, int> table;

#define ll long long int
#define vii vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define all(x) x.begin(), x.end()
#define S second
#define F first
#define ins insert
#define ld long double
#define lp(l, r) for (i = l; i <= r; ++i)

#define lpj(l, r) for (j = l; j <= r; ++j)
#define lpr(l, r) for (i = r; i >= l; --i)
#define lprj(l, r) for (j = r; j >= l; --j)

#define swap(a, b)                                                             \
  {                                                                            \
    auto _t = a;                                                               \
    a = b, b = _t;                                                             \
  }
#define flush() cout.flush()
#define popcount __builtin_popcount

// typedef __int128 bign;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define MD 1000000007
#define M 100011
#define LN 14
#define fftmod 998244353
#define EPS (double)1e-10

ll md = MD;
ll inf = 1e18;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

inline ll exp(ll a, ll b) {
  ll r = 1LL;
  while (b > 0) {
    if (b & 1) {
      r = r * (a % md);
      r = (r + md) % md;
    }
    b /= 2;
    a = (a % md) * (a % md);
    a = (a + md) % md;
  }
  return (r + md) % md;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  if (a == 0)
    return b;
  return gcd(b, a % b);
}
inline ll poww(ll a, ll b) {
  ll r = 1LL;
  while (b > 0) {
    if (b & 1)
      r = r * a;
    b /= 2;
    a = a * a;
  }
  return (ll)r;
}

using cd = complex<double>;
const double PI = acos(-1);

inline ll rand(ll l, ll r) { return l + rng() % (r - l + 1); }

vii g[M];
ll in[M], out[M];
ll vis[M][3];
ll dp[M][3];
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // ifstream fin("input.txt");ofstream fout("outut.txt");
  ll t, i, j, m, n, s, z, k, p, T;
  string s1, s2;
  ll qa;
  ll x, y;
  cin >> n >> m;
  lp(1, m) {
    cin >> x >> y;
    g[x].pb(y);
  }
  cin >> x >> y;
  memset(dp, -1, sizeof(dp));
  dp[x][0] = 0;
  queue<pii> q;
  q.push({x, 0});
  while (!q.empty()) {
    pii u = q.front();
    q.pop();
    vis[u.F][u.S] = 1;
    for (auto v : g[u.F]) {
      ll nx = u.S + 1;
      nx %= 3;
      if (!vis[v][nx]) {
        vis[v][nx] = 1;
        dp[v][nx] = dp[u.F][u.S] + 1;
        q.push({v, nx});
      }
    }
  }
  if (dp[y][0] == -1)
    cout << -1 << "\n";
  else
    cout << dp[y][0] / 3 << "\n";

  return 0;
}