Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////

#define fs first
#define sc second
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;

template <typename T> const T INF = (is_same<T, int>::value ? 1e9 : 1e18);
template <typename T> inline T mod(T n, T mod) {
  T res = n % mod;
  if (res < 0)
    res += mod;
  return res;
}

/// command for char arrays with spaces -> scanf(" %[^\n]", text);

////////////////////////// Solution starts below. //////////////////////////////

int main() {
  fastio;
  int n, m;
  cin >> n >> m;
  vvi adj(n + 1, vi()), vis(n + 1, vi(4, 0));
  ;
  while (m--) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
  }
  int s, t;
  cin >> s >> t;
  function<int(int)> bfs = [&](int sc) {
    queue<pii> q;
    q.push({sc, 0});
    while (!q.empty()) {
      int v = q.front().fs;
      int d = q.front().sc;
      q.pop();
      if (vis[v][d % 3])
        continue;
      if (v == t and d % 3 == 0)
        return d / 3;
      for (int u : adj[v]) {
        q.push({u, d + 1});
      }
    }
    return -1;
  };
  cout << bfs(s) << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////

#define fs first
#define sc second
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;

template <typename T> const T INF = (is_same<T, int>::value ? 1e9 : 1e18);
template <typename T> inline T mod(T n, T mod) {
  T res = n % mod;
  if (res < 0)
    res += mod;
  return res;
}

/// command for char arrays with spaces -> scanf(" %[^\n]", text);

////////////////////////// Solution starts below. //////////////////////////////

int main() {
  fastio;
  int n, m;
  cin >> n >> m;
  vvi adj(n + 1, vi()), vis(n + 1, vi(4, 0));
  ;
  while (m--) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
  }
  int s, t;
  cin >> s >> t;
  function<int(int)> bfs = [&](int sc) {
    queue<pii> q;
    q.push({sc, 0});
    while (!q.empty()) {
      int v = q.front().fs;
      int d = q.front().sc;
      q.pop();
      if (vis[v][d % 3])
        continue;
      vis[v][d % 3] = 1;
      if (v == t and d % 3 == 0)
        return d / 3;
      for (int u : adj[v]) {
        q.push({u, d + 1});
      }
    }
    return -1;
  };
  cout << bfs(s) << endl;
  return 0;
}