Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define irep(i, a, n) for (int i = a; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; --i)
#define allrep(V, v) for (auto &&V : v)
#define all(x) (x).begin(), (x).end()
using lint = long long;
constexpr int INF = 1 << 30;
constexpr lint INFL = 1LL << 62;
constexpr int MOD = 1e9 + 7;
constexpr double EPS = 1e-9;
using namespace std;

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(void) {}
  Edge(int src_val, int dst_val, Weight weight_val)
      : src(src_val), dst(dst_val), weight(weight_val) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;

Graph edges_to_graph(const Edges &edges, const int vertices_num,
                     const bool directed = true) {
  Graph ret(vertices_num);
  for (const auto &e : edges) {
    ret[e.src].push_back(e);
    if (!directed)
      ret[e.src].push_back(Edge(e.dst, e.src, e.weight));
  }
  return ret;
}

void dfs(Graph &g, vector<vector<lint>> &check, int v, int cnt) {
  int step = cnt % 3;
  if (check[v][step] <= cnt)
    return;
  check[v][step] = cnt;
  for (const auto es : g[v]) {
    dfs(g, check, es.dst, cnt + 1);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  int n, m;
  cin >> n >> m;
  Edges edges(m);
  rep(i, m) cin >> edges[i].src >> edges[i].dst;
  int s, t;
  cin >> s >> t;
  Graph graph = edges_to_graph(edges, n + 1);
  vector<vector<lint>> check(n + 1, vector<lint>(3, INFL));
  dfs(graph, check, s, 0);
  cout << (check[t][0] != INFL ? check[t][0] / 3 : -1) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define irep(i, a, n) for (int i = a; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; --i)
#define allrep(V, v) for (auto &&V : v)
#define all(x) (x).begin(), (x).end()
using lint = long long;
constexpr int INF = 1 << 30;
constexpr lint INFL = 1LL << 62;
constexpr int MOD = 1e9 + 7;
constexpr double EPS = 1e-9;
using namespace std;

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(void) {}
  Edge(int src_val, int dst_val, Weight weight_val)
      : src(src_val), dst(dst_val), weight(weight_val) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;

Graph edges_to_graph(const Edges &edges, const int vertices_num,
                     const bool directed = true) {
  Graph ret(vertices_num);
  for (const auto &e : edges) {
    ret[e.src].push_back(e);
    if (!directed)
      ret[e.src].push_back(Edge(e.dst, e.src, e.weight));
  }
  return ret;
}

void dfs(Graph &g, vector<vector<lint>> &check, int v, int cnt) {
  int step = cnt % 3;
  if (check[v][step] <= cnt)
    return;
  check[v][step] = cnt;
  for (const auto es : g[v]) {
    dfs(g, check, es.dst, cnt + 1);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  int n, m;
  cin >> n >> m;
  Edges edges(m);
  rep(i, m) cin >> edges[i].src >> edges[i].dst;
  int s, t;
  cin >> s >> t;
  Graph graph = edges_to_graph(edges, n + 1);
  vector<vector<lint>> check(n + 1, vector<lint>(3, INFL));
  queue<pair<int, int>> que;
  que.push({s, 0});
  while (!que.empty()) {
    int v = que.front().first, cnt = que.front().second, step = cnt % 3;
    que.pop();
    if (check[v][step] <= cnt)
      continue;
    check[v][step] = cnt;
    for (const auto es : graph[v]) {
      que.push({es.dst, cnt + 1});
    }
  }
  cout << (check[t][0] != INFL ? check[t][0] / 3 : -1) << endl;
  return 0;
}