Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
#define all(V) V.begin(), V.end()
using i64 = int_fast64_t;
using P = pair<int, int>;

void dfs(vector<vector<int>> &G, vector<vector<int>> &visited, int v,
         int dist) {
  if (visited[v][dist % 3] <= dist)
    return;
  visited[v][dist % 3] = dist;
  for (auto nxt : G[v])
    dfs(G, visited, nxt, dist + 1);
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  vector<vector<int>> visited(N, vector<int>(3, 1e9));

  int u, v;
  rep(i, M) {
    cin >> u >> v;
    u--, v--;
    G[u].emplace_back(v);
  }
  int S, T;
  cin >> S >> T;
  S--, T--;

  bool bfs = true;

  chrono::steady_clock::time_point start = chrono::steady_clock::now();
  const chrono::milliseconds limit = 500ms;

  struct info {
    int now, dist;
  };
  queue<info> que;
  que.push({S, 0});
  visited[S][0] = 0;
  while (!que.empty() && visited[T][0] == 1e9) {
    info foo = que.front();
    que.pop();
    visited[foo.now][foo.dist % 3] = foo.dist;
    for (int nxt : G[foo.now]) {
      chrono::steady_clock::time_point now = chrono::steady_clock::now();
      chrono::milliseconds t =
          chrono::duration_cast<chrono::milliseconds>(start - now);
      if (t > limit) {
        bfs = false;
        visited[T][0]--;
        break;
      }
      if (visited[nxt][(foo.dist + 1) % 3] != 1e9)
        continue;
      que.push({nxt, foo.dist + 1});
    }
  }

  if (!bfs) {
    visited = vector<vector<int>>(N, vector<int>(3, 1e9));
    dfs(G, visited, S, 0);
  }

  int ans = visited[T][0];
  if (ans != 1e9)
    ans /= 3;
  else
    ans = -1;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
#define all(V) V.begin(), V.end()
using i64 = int_fast64_t;
using P = pair<int, int>;

void dfs(vector<vector<int>> &G, vector<vector<int>> &visited, int v,
         int dist) {
  if (visited[v][dist % 3] <= dist)
    return;
  visited[v][dist % 3] = dist;
  for (auto nxt : G[v])
    dfs(G, visited, nxt, dist + 1);
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  vector<vector<int>> visited(N, vector<int>(3, 1e9));

  int u, v;
  rep(i, M) {
    cin >> u >> v;
    u--, v--;
    G[u].emplace_back(v);
  }
  int S, T;
  cin >> S >> T;
  S--, T--;

  bool bfs = true;

  chrono::steady_clock::time_point start = chrono::steady_clock::now();
  const chrono::milliseconds limit = 500ms;

  struct info {
    int now, dist;
  };
  queue<info> que;
  que.push({S, 0});
  visited[S][0] = 0;
  while (!que.empty() && visited[T][0] == 1e9) {
    info foo = que.front();
    que.pop();
    visited[foo.now][foo.dist % 3] = foo.dist;
    for (int nxt : G[foo.now]) {
      chrono::steady_clock::time_point now = chrono::steady_clock::now();
      chrono::milliseconds t =
          chrono::duration_cast<chrono::milliseconds>(now - start);
      if (t > limit) {
        bfs = false;
        visited[T][0]--;
        break;
      }
      if (visited[nxt][(foo.dist + 1) % 3] != 1e9)
        continue;
      que.push({nxt, foo.dist + 1});
    }
  }

  if (!bfs) {
    visited = vector<vector<int>>(N, vector<int>(3, 1e9));
    dfs(G, visited, S, 0);
  }

  int ans = visited[T][0];
  if (ans != 1e9)
    ans /= 3;
  else
    ans = -1;
  cout << ans << endl;
}