Language: C++

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

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

const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};

// Self settings
// clang-format off
#define MAX_N 100000
#define INF 10000
#define REP(i, N) for (int i = 0; i < (int)(N); ++i)
// clang-format on

int N, M;
int S, T;
int G[MAX_N][MAX_N];

int d[MAX_N];
bool used[MAX_N];
bool used1[MAX_N];
bool used2[MAX_N];
int s, t;

void solve()
{
  fill(d, d + N, INF);
  fill(used, used + N, false);
  fill(used1, used1 + N, false);
  fill(used2, used2 + N, false);

  queue<int> que;
  que.push(s);
  d[s] = 0;
  while (!que.empty()) {
    int cur = que.front();
    que.pop();

    // ken
    queue<int> k;
    for (int i = 0; i < N; i++) {
      if (!used1[i] && G[cur][i]) {
        used1[i] = true;
        k.push(i);
      }
    }

    queue<int> kk;
    // ken
    while (!k.empty()) {
      int ccur = k.front();
      k.pop();
      for (int i = 0; i < N; i++) {
        if (!used2[i] && G[ccur][i]) {
          used2[i] = true;
          kk.push(i);
        }
      }
    }

    // pa
    queue<int> kkp;
    while (!kk.empty()) {
      int ccur = kk.front();
      kk.pop();
      for (int i = 0; i < N; i++) {
        if (!used[i] && G[ccur][i]) {
          used[i] = true;
          kkp.push(i);
        }
      }
    }
    while (!kkp.empty()) {
      int ccur = kkp.front();
      kkp.pop();
      used[ccur] = true;
      d[ccur] = min(d[ccur], d[cur] + 1);
      que.push(ccur);
    }
  }
}

int main(void)
{
  scanf("%d%d", &N, &M);
  int u, v;
  REP(i, M)
  {
    scanf("%d%d", &u, &v);
    // 0-indexed
    G[u - 1][v - 1] = 1;
  }
  scanf("%d%d", &s, &t);
  // 0-indexed
  s = s - 1, t = t - 1;
  solve();
  int res = d[t];
  printf("%d\n", (res != INF) ? res : -1);
  return 0;
}


Compiler output:
/tmp/cctYV6Mp.o: in function `solve()':
a.cc:(.text+0x26): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0x3b): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0x56): relocation truncated to fit: R_X86_64_PC32 against symbol `used' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0x6b): relocation truncated to fit: R_X86_64_PC32 against symbol `used' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0x86): relocation truncated to fit: R_X86_64_PC32 against symbol `used1' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0x9b): relocation truncated to fit: R_X86_64_PC32 against symbol `used1' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0xb6): relocation truncated to fit: R_X86_64_PC32 against symbol `used2' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0xcb): relocation truncated to fit: R_X86_64_PC32 against symbol `used2' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0xea): relocation truncated to fit: R_X86_64_PC32 against symbol `s' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0xfb): relocation truncated to fit: R_X86_64_PC32 against symbol `s' defined in .bss section in /tmp/cctYV6Mp.o
a.cc:(.text+0x10c): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status