Problem p02991 (C++)

Buggy submission (status: Runtime Error):
/*
 * atcoder/abc132/e.cpp
 */

// C++ 14
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring> // memset
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++)
template <class T> ostream &operator<<(ostream &out, vector<T> const &v) {
  for (auto &&a : v)
    out << a << " ";
  out << endl;
  return out;
}
template <class T> void dump(T &a) { cout << a << endl; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#include <queue>
const int MAX = 100000;
const int INF = 10000000;
int N, M, S, T;
vector<int> G[MAX];
int dist[MAX][3];
int bfs() {
  queue<pair<int, int>> Q;
  Q.push({S, 0});
  dist[S][0] = 0;

  while (Q.size()) {
    auto p = Q.front();
    Q.pop();
    int u = p.first;
    int d = p.second;
    d++;
    for (auto &&v : G[u]) {
      if (dist[v][d % 3] < INF)
        continue;
      dist[v][d % 3] = d;
      Q.push({v, d});
      if (v == T and d % 3 == 0) {
        return d / 3;
      }
    }
  }

  return -1;
}

void solve() {
  cin >> N >> M;
  int a, b;
  loop(m, 0, M) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
  }
  cin >> S >> T;
  S--;
  T--;
  loop(i, 0, N + 1) {
    loop(j, 0, 3) { dist[i][j] = INF; }
  }

  cout << bfs() << endl;
}
int main() {
  // cout.precision(15); cout << fixed;

  solve();

  return 0;
}


Fixed submission:
/*
 * atcoder/abc132/e.cpp
 */

// C++ 14
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring> // memset
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++)
template <class T> ostream &operator<<(ostream &out, vector<T> const &v) {
  for (auto &&a : v)
    out << a << " ";
  out << endl;
  return out;
}
template <class T> void dump(T &a) { cout << a << endl; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#include <queue>
const int MAX = 100001;
const int INF = 10000000;
int N, M, S, T;
vector<int> G[MAX];
int dist[MAX][3];
int bfs() {
  queue<pair<int, int>> Q;
  Q.push({S, 0});
  dist[S][0] = 0;

  while (Q.size()) {
    auto p = Q.front();
    Q.pop();
    int u = p.first;
    int d = p.second;
    d++;
    for (auto &&v : G[u]) {
      if (dist[v][d % 3] < INF)
        continue;
      dist[v][d % 3] = d;
      Q.push({v, d});
      if (v == T and d % 3 == 0) {
        return d / 3;
      }
    }
  }

  return -1;
}

void solve() {
  cin >> N >> M;
  int a, b;
  loop(m, 0, M) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
  }
  cin >> S >> T;
  S--;
  T--;
  loop(i, 0, N + 1) {
    loop(j, 0, 3) { dist[i][j] = INF; }
  }

  cout << bfs() << endl;
}
int main() {
  // cout.precision(15); cout << fixed;

  solve();

  return 0;
}