Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define bit(x, v) ((ll)x << v)

const ll INF = 1000000007;
const int MAX = 210000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
  for (size_t i = 0; i <= n; i++) {
    pr[i] = i;
  }
}

int parent(int x) {
  if (x == pr[x])
    return x;
  return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
  int px = parent(x);
  int py = parent(y);

  if (px == py)
    return false;
  if (px < py) {
    pr[py] = px;
  } else {
    pr[px] = py;
  }
  return true;
}

// res[i][c] := i 文字目以降で最初に文字 c が登場する index (存在しないときは n)
vector<vector<int>> calcNext(const string &S) {
  int n = (int)S.size();
  vector<vector<int>> res(n + 1, vector<int>(26, n));
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j < 26; ++j)
      res[i][j] = res[i + 1][j];
    res[i][S[i] - 'a'] = i;
  }
  return res;
}
vector<int> es[100010];

void solv() {

  int n, m;
  cin >> n >> m;
  for (size_t i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    es[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
  q.push({0, 2, s});

  int d[3][100010];
  memset(d, -1, sizeof(d));

  while (!q.empty()) {
    auto v = q.top();
    q.pop();
    int cnt = v[0];
    int step = v[1];
    int f = v[2];
    if (d[step][f] != -1 && d[step][f] < cnt)
      continue;
    d[step][f] = cnt;
    int nextcnt = step == 2 ? cnt + 1 : cnt;
    int nextstep = (step + 1) % 3;

    for (auto e : es[f]) {

      q.push({nextcnt, nextstep, e, f});
    }
  }

  cout << d[2][t] << endl;
}

int main() {
  COMinit();
  solv();

  return 0;
}


Fixed submission:
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define bit(x, v) ((ll)x << v)

const ll INF = 1000000007;
const int MAX = 210000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
  for (size_t i = 0; i <= n; i++) {
    pr[i] = i;
  }
}

int parent(int x) {
  if (x == pr[x])
    return x;
  return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
  int px = parent(x);
  int py = parent(y);

  if (px == py)
    return false;
  if (px < py) {
    pr[py] = px;
  } else {
    pr[px] = py;
  }
  return true;
}

// res[i][c] := i 文字目以降で最初に文字 c が登場する index (存在しないときは n)
vector<vector<int>> calcNext(const string &S) {
  int n = (int)S.size();
  vector<vector<int>> res(n + 1, vector<int>(26, n));
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j < 26; ++j)
      res[i][j] = res[i + 1][j];
    res[i][S[i] - 'a'] = i;
  }
  return res;
}
vector<int> es[100010];

void solv() {

  int n, m;
  cin >> n >> m;
  for (size_t i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    es[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
  q.push({0, 2, s});

  int d[3][100010];
  memset(d, -1, sizeof(d));

  while (!q.empty()) {
    auto v = q.top();
    q.pop();
    int cnt = v[0];
    int step = v[1];
    int f = v[2];
    if (d[step][f] != -1 && d[step][f] <= cnt)
      continue;
    d[step][f] = cnt;
    int nextcnt = step == 2 ? cnt + 1 : cnt;
    int nextstep = (step + 1) % 3;

    for (auto e : es[f]) {

      q.push({nextcnt, nextstep, e, f});
    }
  }

  cout << d[2][t] << endl;
}

int main() {
  COMinit();
  solv();

  return 0;
}