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