prithivMLmods/Coder-Stat huggingface dataset:
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i, a, b) for (ll i = (a); i <= (ll)(b); ++i)

void chmax(ll &a, ll b) { a = max(a, b); }
void chmin(ll &a, ll b) { a = min(a, b); }

using P = pair<int, int>;

const int MAX_N = 1e5 + 5;
const int INF = 1001001001;

vector<int> a, ans;
int dp[MAX_N][3];

ll modnum = 1e9 + 7;

using Graph = vector<vector<int>>;
Graph to(MAX_N);

void dfs(int u, int count = 0, int rem = 2) {
  if (rem == -1) {
    count++;
    rem = 2;
  }

  if (dp[u][2 - rem] <= count) {
    return;
  } else {
    dp[u][2 - rem] = count;
  }

  for (auto v : to[u]) {
    dfs(v, count, rem - 1);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  s--;
  t--;

  rep(i, MAX_N) rep(j, 3) dp[i][j] = INF;

  dfs(s);

  if (dp[t][0] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[t][0] << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i, a, b) for (ll i = (a); i <= (ll)(b); ++i)

void chmax(ll &a, ll b) { a = max(a, b); }
void chmin(ll &a, ll b) { a = min(a, b); }

using P = pair<int, int>;

const int MAX_N = 1e5 + 5;
const int INF = 1001001001;

vector<int> a, ans;
int dp[MAX_N][3];

ll modnum = 1e9 + 7;

using Graph = vector<vector<int>>;
Graph to(MAX_N);

void dfs(int u, int count = 0, int rem = 2) {
  if (rem == -1) {
    count++;
    rem = 2;
  }

  if (dp[u][2 - rem] <= count) {
    return;
  } else {
    dp[u][2 - rem] = count;
  }

  for (auto v : to[u]) {
    dfs(v, count, rem - 1);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  s--;
  t--;

  rep(i, MAX_N) rep(j, 3) dp[i][j] = INF;
  queue<P> que;
  que.emplace(s, 0);
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (dp[p.first][p.second % 3] > p.second / 3) {
      dp[p.first][p.second % 3] = p.second / 3;
      for (auto u : to[p.first]) {
        que.emplace(u, p.second + 1);
      }
    }
  }

  if (dp[t][0] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[t][0] << endl;
  }

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int MAX = 1000000;
typedef pair<int, int> P;
int main() {
  int N, M, S, T, u, v;
  vector<int> vec[30000];
  int ans[30000];
  priority_queue<P, vector<P>, greater<P>> que;
  P p;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> u >> v;
    u--;
    v--;
    vec[u].push_back(v + N);
    vec[u + N].push_back(v + 2 * N);
    vec[u + 2 * N].push_back(v);
  }
  fill(ans, ans + 3 * N, MAX);
  cin >> S >> T;
  S--;
  T--;
  ans[S] = 0;
  que.push(P(0, S));
  while (!que.empty()) {
    p = que.top();
    que.pop();
    for (int i : vec[p.second]) {
      if (ans[i] > ans[p.second] + 1) {
        ans[i] = ans[p.second] + 1;
        que.push(P(ans[i], i));
      }
    }
  }

  if (ans[T] < MAX)
    cout << ans[T] / 3;
  else
    cout << -1;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int MAX = 1000000;
typedef pair<int, int> P;
int main() {
  int N, M, S, T, u, v;
  vector<int> vec[300000];
  int ans[300000];
  priority_queue<P, vector<P>, greater<P>> que;
  P p;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> u >> v;
    u--;
    v--;
    vec[u].push_back(v + N);
    vec[u + N].push_back(v + 2 * N);
    vec[u + 2 * N].push_back(v);
  }
  fill(ans, ans + 3 * N, MAX);
  cin >> S >> T;
  S--;
  T--;
  ans[S] = 0;
  que.push(P(0, S));
  while (!que.empty()) {
    p = que.top();
    que.pop();
    for (int i : vec[p.second]) {
      if (ans[i] > ans[p.second] + 1) {
        ans[i] = ans[p.second] + 1;
        que.push(P(ans[i], i));
      }
    }
  }

  if (ans[T] < MAX)
    cout << ans[T] / 3;
  else
    cout << -1;
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define INF 1000000007

int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> graph[3 * n + 1];
  long long u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    graph[u].push_back(v + n);
    graph[u + n].push_back(v + 2 * n);
    graph[u + 2 * n].push_back(v);
  }
  long long S, T;
  cin >> S >> T;
  long long num = 3 * n + 1;
  long long dist[num];
  for (int i = 0; i < num; i++) {
    dist[i] = INF;
  }
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  q.push(make_pair(0, S));
  while (!q.empty()) {
    pair<long long, long long> now = q.top();
    q.pop();
    if (dist[T] != INF)
      continue;
    dist[now.second] = now.first;
    for (int i = 0; i < graph[now.second].size(); i++) {
      if (dist[graph[now.second][i]] == INF) {
        q.push(make_pair(dist[now.second] + 1, graph[now.second][i]));
      }
    }
  }
  if (dist[T] == INF)
    cout << -1 << endl;
  else
    cout << dist[T] / 3 << endl;

  return 0;
}


Fixed submission:
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define INF 1000000007

int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> graph[3 * n + 1];
  long long u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    graph[u].push_back(v + n);
    graph[u + n].push_back(v + 2 * n);
    graph[u + 2 * n].push_back(v);
  }
  long long S, T;
  cin >> S >> T;
  long long num = 3 * n + 1;
  long long dist[num];
  for (int i = 0; i < num; i++) {
    dist[i] = INF;
  }
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  q.push(make_pair(0, S));
  while (!q.empty()) {
    pair<long long, long long> now = q.top();
    q.pop();
    if (dist[now.second] != INF)
      continue;
    dist[now.second] = now.first;
    for (int i = 0; i < graph[now.second].size(); i++) {
      if (dist[graph[now.second][i]] == INF) {
        q.push(make_pair(dist[now.second] + 1, graph[now.second][i]));
      }
    }
  }
  if (dist[T] == INF)
    cout << -1 << endl;
  else
    cout << dist[T] / 3 << endl;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;

const int INF = 1001001001;
int dist[20005][3];

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  s--;
  t--;
  rep(i, n) rep(j, 3) dist[i][j] = INF;
  dist[s][0] = 0;
  queue<P> q;
  q.push(P(s, 0));
  while (!q.empty()) {
    int a = q.front().first, b = q.front().second;
    q.pop();
    for (auto c : to[a]) {
      int nl = (1 + b) % 3;
      if (dist[c][nl] != INF)
        continue;
      dist[c][nl] = dist[a][b] + 1;
      q.push(P(c, nl));
    }
  }
  int ans = dist[t][0];
  if (ans == INF) {
    puts("-1");
    return 0;
  }
  cout << ans / 3 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;

const int INF = 1001001001;
int dist[100005][3];

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  s--;
  t--;
  rep(i, n) rep(j, 3) dist[i][j] = INF;
  dist[s][0] = 0;
  queue<P> q;
  q.push(P(s, 0));
  while (!q.empty()) {
    int a = q.front().first, b = q.front().second;
    q.pop();
    for (auto c : to[a]) {
      int nl = (1 + b) % 3;
      if (dist[c][nl] != INF)
        continue;
      dist[c][nl] = dist[a][b] + 1;
      q.push(P(c, nl));
    }
  }
  int ans = dist[t][0];
  if (ans == INF) {
    puts("-1");
    return 0;
  }
  cout << ans / 3 << endl;
}
Problem p02991 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 100005;
const ll INF = 1001001001001001001;
const int MOD = 1000000007;

vector<int> G[MAX];
ll dp[MAX][3];

int main() {
  int N, M;
  cin >> N >> M;

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

  int S, T;
  cin >> S >> T;
  S--, T--;

  rep(i, N) rep(j, 3) dp[i][j] = INF;

  priority_queue<tuple<ll, int, int>> q;
  dp[S][0] = 0;
  q.emplace(0, S, 0);

  while (q.size()) {
    auto [d, n, c] = q.top();
    q.pop();
    if (dp[n][c] < d)
      continue;
    for (auto next : G[n]) {
      int nc = c + 1;
      if (nc < 3) {
        if (chmin(dp[next][nc], d)) {
          q.emplace(d, next, nc);
        }
      } else {
        if (chmin(dp[next][0], d + 1)) {
          q.emplace(d + 1, next, 0);
        }
      }
    }
  }

  ll ans = dp[T][0];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}

Fixed submission:
// #include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
// using namespace atcoder;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 100005;
const ll INF = 1001001001001001001;
const int MOD = 1000000007;

vector<int> G[MAX];
ll dp[MAX][3];

int main() {
  int N, M;
  cin >> N >> M;

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

  int S, T;
  cin >> S >> T;
  S--, T--;

  rep(i, N) rep(j, 3) dp[i][j] = INF;

  priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>,
                 greater<tuple<ll, int, int>>>
      q;
  dp[S][0] = 0;
  q.emplace(0, S, 0);

  while (q.size()) {
    auto [d, n, c] = q.top();
    q.pop();
    if (dp[n][c] < d)
      continue;
    for (auto next : G[n]) {
      int nc = c + 1;
      if (nc < 3) {
        if (chmin(dp[next][nc], d)) {
          q.emplace(d, next, nc);
        }
      } else {
        if (chmin(dp[next][0], d + 1)) {
          q.emplace(d + 1, next, 0);
        }
      }
    }
  }

  ll ans = dp[T][0];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const ll mod = 1e9 + 7;
const ll MAXN = 2e3 + 5;

vector<vector<ll>> adj(MAXN, vector<ll>());
vector<vector<ll>> d(MAXN, vector<ll>(3, LLONG_MAX));
vector<bool> used(MAXN, false);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }
  ll st, t;
  cin >> st >> t;

  d[st][0] = 0;
  set<pair<vector<ll>, ll>> s;
  s.insert({d[st], st});
  while (!s.empty()) {
    ll u = s.begin()->second;
    s.erase(s.begin());
    for (auto to : adj[u]) {
      if (d[u][0] != LLONG_MAX && d[to][1] > d[u][0] + 1) {
        s.erase({d[to], to});
        d[to][1] = d[u][0] + 1;
        s.insert({d[to], to});
      }
      if (d[u][1] != LLONG_MAX && d[to][2] > d[u][1] + 1) {
        s.erase({d[to], to});
        d[to][2] = d[u][1] + 1;
        s.insert({d[to], to});
      }
      if (d[u][2] != LLONG_MAX && d[to][0] > d[u][2] + 1) {
        s.erase({d[to], to});
        d[to][0] = d[u][2] + 1;
        s.insert({d[to], to});
      }
    }
  }

  if (d[t][0] == LLONG_MAX)
    cout << -1 << endl;
  else
    cout << d[t][0] / 3 << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const ll mod = 1e9 + 7;
const ll MAXN = 1e5 + 5;

vector<vector<ll>> adj(MAXN, vector<ll>());
vector<vector<ll>> d(MAXN, vector<ll>(3, LLONG_MAX));
vector<bool> used(MAXN, false);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }
  ll st, t;
  cin >> st >> t;

  d[st][0] = 0;
  set<pair<vector<ll>, ll>> s;
  s.insert({d[st], st});
  while (!s.empty()) {
    ll u = s.begin()->second;
    s.erase(s.begin());
    for (auto to : adj[u]) {
      if (d[u][0] != LLONG_MAX && d[to][1] > d[u][0] + 1) {
        s.erase({d[to], to});
        d[to][1] = d[u][0] + 1;
        s.insert({d[to], to});
      }
      if (d[u][1] != LLONG_MAX && d[to][2] > d[u][1] + 1) {
        s.erase({d[to], to});
        d[to][2] = d[u][1] + 1;
        s.insert({d[to], to});
      }
      if (d[u][2] != LLONG_MAX && d[to][0] > d[u][2] + 1) {
        s.erase({d[to], to});
        d[to][0] = d[u][2] + 1;
        s.insert({d[to], to});
      }
    }
  }

  if (d[t][0] == LLONG_MAX)
    cout << -1 << endl;
  else
    cout << d[t][0] / 3 << endl;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MAX 100000
#define INF 2140000000
#define MOD 1000000007

int bfs(vector<vector<int>> &adj, int s, int t) {
  vector<int> d(adj.size(), MAX);
  int flag[MAX] = {0};
  queue<int> q;
  flag[s] = 1;
  d[s] = 0;
  q.push(s);

  while (!q.empty()) {
    int u = q.front();
    if (u == t) {
      return d[u] / 3;
    }
    q.pop();

    for (int i = 0; i < adj[u].size(); ++i) {
      int v = adj[u][i];

      if (!flag[v]) {
        flag[v] = 1;
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }

  return -1;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> adj(3 * n);
  int u, v;

  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v + n);
    adj[u + n].push_back(v + 2 * n);
    adj[u + 2 * n].push_back(v);
  }

  int s, t;
  cin >> s >> t;
  cout << bfs(adj, s - 1, t - 1) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MAX 300000
#define INF 2140000000
#define MOD 1000000007

int bfs(vector<vector<int>> &adj, int s, int t) {
  vector<int> d(adj.size(), MAX);
  int flag[MAX] = {0};
  queue<int> q;
  flag[s] = 1;
  d[s] = 0;
  q.push(s);

  while (!q.empty()) {
    int u = q.front();
    if (u == t) {
      return d[u] / 3;
    }
    q.pop();

    for (int i = 0; i < adj[u].size(); ++i) {
      int v = adj[u][i];

      if (!flag[v]) {
        flag[v] = 1;
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }

  return -1;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> adj(3 * n);
  int u, v;

  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v + n);
    adj[u + n].push_back(v + 2 * n);
    adj[u + 2 * n].push_back(v);
  }

  int s, t;
  cin >> s >> t;
  cout << bfs(adj, s - 1, t - 1) << endl;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

int n;
vector<edge> g[300030];
int d[100010];
int inf = 2000000007;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i <= 3 * n; i++) {
    d[i] = inf;
  }
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
        // cout << 'v' << v << endl;
      }
    }
  }
}

int main() {
  int m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edge e;
    e.to = v + n;
    e.cost = 1;
    g[u].push_back(e);
    e.to = v + 2 * n;
    g[u + n].push_back(e);
    e.to = v;
    g[u + 2 * n].push_back(e);
  }
  int s, t;
  cin >> s >> t;
  s--;
  t--;
  dijkstra(s);
  if (d[t] == inf) {
    cout << -1 << endl;
  } else {
    cout << d[t] / 3 << endl;
  }
}

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

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

int n;
vector<edge> g[300030];
int d[300030];
int inf = 2000000007;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i <= 3 * n; i++) {
    d[i] = inf;
  }
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
        // cout << 'v' << v << endl;
      }
    }
  }
}

int main() {
  int m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edge e;
    e.to = v + n;
    e.cost = 1;
    g[u].push_back(e);
    e.to = v + 2 * n;
    g[u + n].push_back(e);
    e.to = v;
    g[u + 2 * n].push_back(e);
  }
  int s, t;
  cin >> s >> t;
  s--;
  t--;
  dijkstra(s);
  if (d[t] == inf) {
    cout << -1 << endl;
  } else {
    cout << d[t] / 3 << endl;
  }
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int, int>;

int n, m;
vector<vector<int>> G;
int s, t;
ll dist[10010][3];

ll bfs() {
  memset(dist, -1LL, sizeof(dist));
  dist[s][0] = 0;
  queue<P> stt;
  stt.push(P(s, 0));
  while (!stt.empty()) {
    int v = stt.front().first;
    int m = stt.front().second;
    stt.pop();
    for (auto nv : G[v]) {
      if (dist[nv][(m + 1) % 3] != -1) {
        continue;
      } else {
        dist[nv][(m + 1) % 3] = dist[v][m] + 1;
        stt.push(P(nv, (m + 1) % 3));
      }
    }
  }
  if (dist[t][0] == -1)
    return -1;
  else
    return dist[t][0] / 3;
}

int main(void) {
  cin >> n >> m;
  G.assign(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    // 有向グラフなのでuからvだけ辺を張りましょう。
    // G[v].push_back(u);
  }
  cin >> s >> t;
  s--;
  t--;

  cout << bfs() << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int, int>;

int n, m;
vector<vector<int>> G;
int s, t;
ll dist[100010][3];

ll bfs() {
  memset(dist, -1LL, sizeof(dist));
  dist[s][0] = 0;
  queue<P> stt;
  stt.push(P(s, 0));
  while (!stt.empty()) {
    int v = stt.front().first;
    int m = stt.front().second;
    stt.pop();
    for (auto nv : G[v]) {
      if (dist[nv][(m + 1) % 3] != -1) {
        continue;
      } else {
        dist[nv][(m + 1) % 3] = dist[v][m] + 1;
        stt.push(P(nv, (m + 1) % 3));
      }
    }
  }
  if (dist[t][0] == -1)
    return -1;
  else
    return dist[t][0] / 3;
}

int main(void) {
  cin >> n >> m;
  G.assign(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    // 有向グラフなのでuからvだけ辺を張りましょう。
    // G[v].push_back(u);
  }
  cin >> s >> t;
  s--;
  t--;

  cout << bfs() << endl;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;

int ans, n, m, s, t;
int d[100005];
vector<int> g[100005];

void bfs(int s) {
  queue<int> q1, q2, q3;
  rep(i, 0, n) d[i] = -1;

  q1.push(s);
  d[s] = 0;
  while (!q1.empty()) {
    int u = q1.front();
    q1.pop();
    for (int v1 : g[u]) {
      q2.push(v1);
    }

    while (!q2.empty()) {
      int v1 = q2.front();
      q2.pop();
      for (int v2 : g[v1]) {
        q3.push(v2);
      }
    }

    while (!q3.empty()) {
      int v2 = q3.front();
      q3.pop();
      for (int v3 : g[v2]) {
        if (d[v3] == -1) {
          d[v3] = d[u] + 1;
          q1.push(v3);
        }
      }
    }
  }
}

int main(void) {

  cin >> n >> m;

  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
  }
  cin >> s >> t;
  s--;
  t--;

  bfs(s);

  cout << d[t] << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;

int ans, n, m, s, t;
int d[100005];
vector<int> g[100005];

void bfs(int s) {
  queue<int> q1, q2, q3;
  rep(i, 0, n) d[i] = -1;

  q1.push(s);
  d[s] = 0;
  while (!q1.empty()) {
    int u = q1.front();
    q1.pop();
    for (int v1 : g[u]) {
      q2.push(v1);
    }

    while (!q2.empty()) {
      int v1 = q2.front();
      q2.pop();
      for (int v2 : g[v1]) {
        q3.push(v2);
      }
    }

    while (!q3.empty()) {
      int v2 = q3.front();
      q3.pop();
      for (int v3 : g[v2]) {
        if (d[v3] == -1) {
          d[v3] = d[u] + 1;
          if (v3 == t)
            return;
          q1.push(v3);
        }
      }
    }
  }
}

int main(void) {

  cin >> n >> m;

  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
  }
  cin >> s >> t;
  s--;
  t--;

  bfs(s);

  cout << d[t] << "\n";
  return 0;
}
Problem p02991 (C++)

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

#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define pb push_back

using ll = long long;
using pii = pair<int, int>;

template <typename T> using V = vector<T>;

const int MAX_N = 1e5 + 5;
vector<int> adj[3 * MAX_N];
bool visit[3 * MAX_N];
int dist[3 * MAX_N];

inline void make_edge(int u, int v) { adj[u].pb(v); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    make_edge(3 * u, 3 * v + 1);
    make_edge(3 * u + 1, 3 * v + 2);
    make_edge(3 * u + 2, 3 * v);
  }
  int S, T;
  cin >> S >> T;
  S *= 3;
  T *= 3;
  queue<int> Q;
  dist[S] = 0;
  Q.push(S);
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    visit[cur] = true;
    for (auto nxt : adj[cur]) {
      if (!visit[nxt]) {
        Q.push(nxt);
        dist[nxt] = dist[cur] + 1;
      }
    }
  }
  cout << (visit[T] ? (dist[T] / 3) : -1) << '\n';
  return 0;
}

Fixed submission:
/*input
 */
#include <bits/stdc++.h>
using namespace std;

#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define pb push_back

using ll = long long;
using pii = pair<int, int>;

template <typename T> using V = vector<T>;

const int MAX_N = 1e5 + 5;
vector<int> adj[3 * MAX_N];
bool visit[3 * MAX_N];
int dist[3 * MAX_N];

inline void make_edge(int u, int v) { adj[u].pb(v); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    make_edge(3 * u, 3 * v + 1);
    make_edge(3 * u + 1, 3 * v + 2);
    make_edge(3 * u + 2, 3 * v);
  }
  int S, T;
  cin >> S >> T;
  S *= 3;
  T *= 3;
  queue<int> Q;
  dist[S] = 0;
  Q.push(S);
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    if (visit[cur])
      continue;
    visit[cur] = true;
    for (auto nxt : adj[cur]) {
      if (!visit[nxt]) {
        Q.push(nxt);
        dist[nxt] = dist[cur] + 1;
      }
    }
  }
  cout << (visit[T] ? (dist[T] / 3) : -1) << '\n';
  return 0;
}
Problem p02991 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define FOR(k, m, n) for (ll(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define WAITING(str)                                                           \
  int str;                                                                     \
  std::cin >> str;
#define DEBUGING(str) cout << #str << " " str << endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007; // 10^9+7

int main() {
  int N, M, S, T;
  cin >> N >> M;
  vector<vector<int>> edges(N);
  vector<vector<int>> dp(N, vector<int>(3, INF));
  REP(i, M) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].push_back(v);
  }
  cin >> S >> T;
  S--;
  T--;

  queue<pair<int, int>> q;
  q.push({0, S});
  dp[S][0] = 0;

  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int dist = p.first;
    int pos = p.second;
    for (auto next : edges[pos]) {
      int ndist = dist + 1;
      if (dp[next][ndist % 3] <= dist)
        continue;
      dp[next][ndist % 3] = ndist;
      q.push({ndist, next});
    }
  }
  if (dp[T][0] == INF)
    cout << -1 << endl;
  else
    cout << dp[T][0] / 3 << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define FOR(k, m, n) for (ll(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define WAITING(str)                                                           \
  int str;                                                                     \
  std::cin >> str;
#define DEBUGING(str) cout << #str << " " str << endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007; // 10^9+7

int main() {
  int N, M, S, T;
  cin >> N >> M;
  vector<vector<int>> edges(N);
  vector<vector<int>> dp(N, vector<int>(3, INF));
  REP(i, M) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].push_back(v);
  }
  cin >> S >> T;
  S--;
  T--;

  queue<pair<int, int>> q;
  q.push({0, S});
  dp[S][0] = 0;

  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int dist = p.first;
    int pos = p.second;
    for (auto next : edges[pos]) {
      int ndist = dist + 1;
      if (dp[next][ndist % 3] <= ndist)
        continue;
      dp[next][ndist % 3] = ndist;
      q.push({ndist, next});
    }
  }
  if (dp[T][0] == INF)
    cout << -1 << endl;
  else
    cout << dp[T][0] / 3 << endl;
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int INF = 1001001001;

int dist[100005][3];

int main() {
  int n, m;
  cin >> n >> m;
  cin >> n >> m;
  Graph G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].pb(b);
  }
  int sv, tv;
  cin >> sv >> tv;
  --sv, --tv;
  rep(i, n) rep(j, 3) dist[i][j] = INF;
  queue<P> q;
  q.push(P(sv, 0));
  dist[sv][0] = 0;
  while (!q.empty()) {
    int v = q.front().first;
    int l = q.front().second;
    q.pop();
    for (int u : G[v]) {
      int nl = (l + 1) % 3;
      if (dist[u][nl] != INF)
        continue;
      dist[u][nl] = dist[v][l] + 1;
      q.push(P(u, nl));
    }
  }
  int ans = dist[tv][0];
  if (ans == INF)
    ans = -1;
  else
    ans /= 3;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int INF = 1001001001;

int dist[100005][3];

int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].pb(b);
  }
  int sv, tv;
  cin >> sv >> tv;
  --sv, --tv;
  rep(i, n) rep(j, 3) dist[i][j] = INF;
  queue<P> q;
  q.push(P(sv, 0));
  dist[sv][0] = 0;
  while (!q.empty()) {
    int v = q.front().first;
    int l = q.front().second;
    q.pop();
    for (int u : G[v]) {
      int nl = (l + 1) % 3;
      if (dist[u][nl] != INF)
        continue;
      dist[u][nl] = dist[v][l] + 1;
      q.push(P(u, nl));
    }
  }
  int ans = dist[tv][0];
  if (ans == INF)
    ans = -1;
  else
    ans /= 3;
  cout << ans << endl;
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define Int int64_t
#define dump(x) cout << (x) << endl
#define fi first
#define se second
Int mod = 1e9 + 7;
Int INF = 1e18;
double EPS = 0.00000001;
// Int mod = 1e6+3;

Int n, m;
vector<Int> G[300005];
Int s, t;
Int d[300005];

void dfs(Int x) {
  for (Int i = 0; i < (Int)G[x].size(); i++) {
    Int to = G[x][i];
    if (d[x] + 1 < d[to]) {
      d[to] = d[x] + 1;
      dfs(to);
    }
  }
}

int main() {
  cin >> n >> m;
  for (Int i = 0; i < m; i++) {
    Int a, b;
    cin >> a >> b;
    a *= 3;
    b *= 3;
    G[a].emplace_back(b + 1);
    G[a + 1].emplace_back(b + 2);
    G[a + 2].emplace_back(b);
  }
  cin >> s >> t;
  for (Int i = 1; i < (n + 1) * 3; i++)
    d[i] = INF;
  d[3 * s] = 0;
  dfs(3 * s);
  // for (Int i = 3; i < (n + 1) * 3; i++) cout << d[i] << " ";
  // cout << endl;
  dump(d[3 * t] == INF ? -1 : d[3 * t] / 3);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Int int64_t
#define dump(x) cout << (x) << endl
#define fi first
#define se second
Int mod = 1e9 + 7;
Int INF = 1e18;
double EPS = 0.00000001;
// Int mod = 1e6+3;

Int n, m;
vector<Int> G[300005];
Int s, t;
Int d[300005];

void dfs(Int x) {
  for (Int i = 0; i < (Int)G[x].size(); i++) {
    Int to = G[x][i];
    if (d[x] + 1 < d[to]) {
      d[to] = d[x] + 1;
      dfs(to);
    }
  }
}

int main() {
  cin >> n >> m;
  for (Int i = 0; i < m; i++) {
    Int a, b;
    cin >> a >> b;
    a *= 3;
    b *= 3;
    G[a].emplace_back(b + 1);
    G[a + 1].emplace_back(b + 2);
    G[a + 2].emplace_back(b);
  }
  cin >> s >> t;
  for (Int i = 1; i < (n + 1) * 3; i++)
    d[i] = INF;
  d[3 * s] = 0;
  queue<Int> q;
  q.push(3 * s);
  while (!q.empty()) {
    Int x = q.front();
    q.pop();
    for (Int i = 0; i < (Int)G[x].size(); i++) {
      Int to = G[x][i];
      if (d[x] + 1 < d[to]) {
        d[to] = d[x] + 1;
        q.push(to);
      }
    }
  }
  dump(d[3 * t] == INF ? -1 : d[3 * t] / 3);
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int n, m, s, t;
vi adj[100001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
  }
  cin >> s >> t;
  queue<pair<int, int>> q;
  q.emplace(s, 0);
  vector<vi> d(n + 1, vi(n + 1, -1));
  d[s][0] = 0;
  while (!q.empty()) {
    int x, k;
    tie(x, k) = q.front();
    q.pop();
    for (int y : adj[x]) {
      if (d[y][(k + 1) % 3] == -1) {
        d[y][(k + 1) % 3] = d[x][k] + 1;
        q.emplace(y, (k + 1) % 3);
      }
    }
  }
  if (d[t][0] == -1)
    cout << -1;
  else
    cout << d[t][0] / 3;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int n, m, s, t;
vi adj[100001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
  }
  cin >> s >> t;
  queue<pair<int, int>> q;
  q.emplace(s, 0);
  vector<vi> d(n + 1, vi(3, -1));
  d[s][0] = 0;
  while (!q.empty()) {
    int x, k;
    tie(x, k) = q.front();
    q.pop();
    for (int y : adj[x]) {
      if (d[y][(k + 1) % 3] == -1) {
        d[y][(k + 1) % 3] = d[x][k] + 1;
        q.emplace(y, (k + 1) % 3);
      }
    }
  }
  if (d[t][0] == -1)
    cout << -1;
  else
    cout << d[t][0] / 3;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
struct edge {
  ll to, cost;
};

const int MAX_V = 100000;
const ll INF = 1LL << 60;

int V;
vector<edge> G[MAX_V];
ll d[MAX_V];

void dijkstra(ll s) {
  // greater<P>を指定することでfirstが小さい順に取り出せるようにする
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int N, M, S, T;
  cin >> N >> M;
  V = N * 3;
  vector<int> u(M), v(M);
  rep(i, M) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    G[u[i]].push_back({v[i] + N, 1});
    G[u[i] + N].push_back({v[i] + N + N, 1});
    G[u[i] + N + N].push_back({v[i], 1});
  }
  cin >> S >> T;
  S--;
  T--;
  dijkstra(S);
  if (d[T] == INF)
    cout << "-1\n";
  else
    cout << d[T] / 3 << endl;
  // rep(i, V) cout << i << " " << d[i] << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
struct edge {
  ll to, cost;
};

const int MAX_V = 300000;
const ll INF = 1LL << 60;

int V;
vector<edge> G[MAX_V];
ll d[MAX_V];

void dijkstra(ll s) {
  // greater<P>を指定することでfirstが小さい順に取り出せるようにする
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int N, M, S, T;
  cin >> N >> M;
  V = N * 3;
  vector<int> u(M), v(M);
  rep(i, M) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    G[u[i]].push_back({v[i] + N, 1});
    G[u[i] + N].push_back({v[i] + N + N, 1});
    G[u[i] + N + N].push_back({v[i], 1});
  }
  cin >> S >> T;
  S--;
  T--;
  dijkstra(S);
  if (d[T] == INF)
    cout << "-1\n";
  else
    cout << d[T] / 3 << endl;
  // rep(i, V) cout << i << " " << d[i] << endl;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
#define ll long long
// <最短距離, 頂点の番号>
typedef pair<ll, ll> P;
const int INF = 1e9;
const int MAX_N = 1000;

struct edge {
  int to;
  int cost;
};
// ノード数
int V;
// ある頂点から出るエッジを保持
vector<edge> G[MAX_N];
// コストを保持
int d[MAX_N];

void dijkstra(int s) {
  // cout << V;
  // Pのコスト昇順の優先度付きキュー
  priority_queue<P, vector<P>, greater<P>> que;
  // INFで初期化
  fill(d, d + V + 10, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int E, S, T, V_r;
  cin >> V >> E;
  V_r = V;
  V = 3 * V;
  // cout << V_r << endl;
  for (int i = 0; i < E; ++i) {
    int a, b;
    int c = 1;
    cin >> a >> b;
    edge e1 = {b + V_r, c};
    edge e2 = {b + 2 * V_r, c};
    edge e3 = {b, c};
    G[a].push_back(e1);
    G[a + V_r].push_back(e2);
    G[a + 2 * V_r].push_back(e3);
  }
  cin >> S >> T;
  dijkstra(S);
  for (int i = 1; i <= V; ++i) {
    // cout << "始点：" << i << endl;
    // for(int j = 0; j < G[i].size(); j++){
    //     cout << "行き先：" << G[i][j].to << endl;
    // }
    // cout << S <<"から" << i << "までのコスト: " << d[i] << endl;
  }
  if (d[T] != INF)
    cout << d[T] / 3 << endl;
  else
    cout << -1 << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
#define ll long long
// <最短距離, 頂点の番号>
typedef pair<ll, ll> P;
const int INF = 1e9;
const int MAX_N = 3 * 1000000;

struct edge {
  int to;
  int cost;
};
// ノード数
int V;
// ある頂点から出るエッジを保持
vector<edge> G[MAX_N];
// コストを保持
int d[MAX_N];

void dijkstra(int s) {
  // cout << V;
  // Pのコスト昇順の優先度付きキュー
  priority_queue<P, vector<P>, greater<P>> que;
  // INFで初期化
  fill(d, d + V + 10, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int E, S, T, V_r;
  cin >> V >> E;
  V_r = V;
  V = 3 * V;
  // cout << V_r << endl;
  for (int i = 0; i < E; ++i) {
    int a, b;
    int c = 1;
    cin >> a >> b;
    edge e1 = {b + V_r, c};
    edge e2 = {b + 2 * V_r, c};
    edge e3 = {b, c};
    G[a].push_back(e1);
    G[a + V_r].push_back(e2);
    G[a + 2 * V_r].push_back(e3);
  }
  cin >> S >> T;
  dijkstra(S);
  for (int i = 1; i <= V; ++i) {
    // cout << "始点：" << i << endl;
    // for(int j = 0; j < G[i].size(); j++){
    //     cout << "行き先：" << G[i][j].to << endl;
    // }
    // cout << S <<"から" << i << "までのコスト: " << d[i] << endl;
  }
  if (d[T] != INF)
    cout << d[T] / 3 << endl;
  else
    cout << -1 << endl;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int N, M;
vector<int> G[100005];

bool visited[100005][3];

int main() {
  ios_base::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    //        G[b].push_back(a);
  }

  int S, T;
  cin >> S >> T;

  queue<pair<int, int>> q;
  q.push({S, 0});

  while (!q.empty()) {
    int v = q.front().first;
    int dis = q.front().second;
    q.pop();
    // if(visited[v][dis % 3])
    // continue;
    visited[v][dis % 3] = true;
    if (v == T && dis % 3 == 0) {
      cout << dis / 3 << endl;
      return 0;
    }

    for (auto u : G[v])
      if (!visited[u][(dis + 1) % 3]) {
        q.push({u, dis + 1});
      }
  }
  cout << -1;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int N, M;
vector<int> G[100005];

bool visited[100005][3];

int main() {
  ios_base::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    //        G[b].push_back(a);
  }

  int S, T;
  cin >> S >> T;

  queue<pair<int, int>> q;
  q.push({S, 0});

  while (!q.empty()) {
    int v = q.front().first;
    int dis = q.front().second;
    q.pop();
    if (visited[v][dis % 3])
      continue;
    visited[v][dis % 3] = true;
    if (v == T && dis % 3 == 0) {
      cout << dis / 3 << endl;
      return 0;
    }

    for (auto u : G[v])
      if (!visited[u][(dis + 1) % 3]) {
        q.push({u, dis + 1});
      }
  }
  cout << -1;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

int main() {
  using namespace std;
  size_t N, M;
  cin >> N >> M;
  vector<vector<unsigned long>> edge(3 * N);
  for (size_t i = 0, u, v; i < M; ++i) {
    cin >> u >> v;
    --u;
    --v;
    edge[u].push_back(v + N);
    edge[u + N].push_back(v + 2 * N);
    edge[u + 2 * N].push_back(v);
  }
  deque<bool> visited(3 * N);
  queue<pair<size_t, size_t>> q;
  size_t S, T;
  cin >> S >> T;
  --S;
  --T;
  q.emplace(S, 0);
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    if (t.first == T) {
      cout << t.second / 3 << endl;
      return 0;
    }
    visited[t.first] = true;
    for (const auto &i : edge[t.first])
      if (!visited[i])
        q.emplace(i, t.second + 1);
  }
  puts("-1");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

int main() {
  using namespace std;
  size_t N, M;
  cin >> N >> M;
  vector<vector<unsigned long>> edge(3 * N);
  for (size_t i = 0, u, v; i < M; ++i) {
    cin >> u >> v;
    --u;
    --v;
    edge[u].push_back(v + N);
    edge[u + N].push_back(v + 2 * N);
    edge[u + 2 * N].push_back(v);
  }
  deque<bool> visited(3 * N);
  queue<pair<size_t, size_t>> q;
  size_t S, T;
  cin >> S >> T;
  --S;
  --T;
  q.emplace(S, 0);
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    if (visited[t.first])
      continue;
    if (t.first == T) {
      cout << t.second / 3 << endl;
      return 0;
    }
    visited[t.first] = true;
    for (const auto &i : edge[t.first])
      if (!visited[i])
        q.emplace(i, t.second + 1);
  }
  puts("-1");
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <unistd.h>
#include <vector>
#define mod (1e9 + 7)
#define pb push_back
using namespace std;
typedef pair<int, int> pii;

vector<vector<int>> vvi;
int vis[111111][3];

int main() {

  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    vvi.pb(vector<int>());
    vis[i][0] = mod;
    vis[i][1] = mod;
    vis[i][2] = mod;
  }

  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    vvi[u].pb(v);
  }
  int s, t;
  cin >> s >> t;

  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(pii(0, s));

  while (!pq.empty()) {
    pii cur_q = pq.top();
    pq.pop();

    int cur_c, cur_v;
    cur_c = cur_q.first;
    cur_v = cur_q.second;

    if (cur_c >= vis[cur_v][cur_c % 3])
      vis[-1000][-1000] = 1000;
    vis[cur_v][cur_c % 3] = cur_c;

    // printf("%d %d\n",cur_c,cur_v);
    // sleep(1);
    for (int i = 0; i < vvi[cur_v].size(); i++) {
      if (vis[vvi[cur_v][i]][(cur_c + 1) % 3] != mod)
        continue;

      pq.push(pii(cur_c + 1, vvi[cur_v][i]));
    }
  }

  if (vis[t][0] != mod) {
    cout << vis[t][0] / 3 << endl;
  } else
    cout << -1 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <unistd.h>
#include <vector>
#define mod (1e9 + 7)
#define pb push_back
using namespace std;
typedef pair<int, int> pii;

vector<vector<int>> vvi;
int vis[111111][3];

int main() {

  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    vvi.pb(vector<int>());
    vis[i][0] = mod;
    vis[i][1] = mod;
    vis[i][2] = mod;
  }

  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    vvi[u].pb(v);
  }
  int s, t;
  cin >> s >> t;

  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(pii(0, s));

  while (!pq.empty()) {
    pii cur_q = pq.top();
    pq.pop();

    int cur_c, cur_v;
    cur_c = cur_q.first;
    cur_v = cur_q.second;

    if (cur_c >= vis[cur_v][cur_c % 3])
      continue; // vis[-1000][-1000]=1000;
    vis[cur_v][cur_c % 3] = cur_c;

    // printf("%d %d\n",cur_c,cur_v);
    // sleep(1);
    for (int i = 0; i < vvi[cur_v].size(); i++) {
      if (vis[vvi[cur_v][i]][(cur_c + 1) % 3] != mod)
        continue;

      pq.push(pii(cur_c + 1, vvi[cur_v][i]));
    }
  }

  if (vis[t][0] != mod) {
    cout << vis[t][0] / 3 << endl;
  } else
    cout << -1 << endl;

  return 0;
}