Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
const int INF = 0x3f3f3f3f, N = 5e5 + 5;
const ll LINF = 1e18 + 5;
char s[N], t[N];
int n, m;
int nxt[N], in[N], dp[N];
bool ok[N];
vector<int> G[N];
void kmp_pre(char s[], int m, int nxt[]) {
  int i = 0, j = nxt[0] = -1;
  while (i < m) {
    while (j != -1 && s[i] != s[j])
      j = nxt[j];
    nxt[++i] = ++j;
  }
}
void kmp_work(char s[], int m, char t[], int n) {
  int i = 0, j = 0;
  kmp_pre(s, m, nxt);
  while (i < n) {
    while (j != -1 && t[i] != s[j])
      j = nxt[j];
    i++;
    j++;
    if (j >= m) {
      ok[i - m] = 1;
      j = nxt[j];
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> t;
  n = strlen(s), m = strlen(t);
  int nn = n;
  while (nn < n + m) {
    for (int i = nn, j = 0; j < n; i++, j++)
      s[i] = s[j];
    nn += n;
    s[nn] = 0;
  }
  kmp_work(t, m, s, nn);
  for (int i = 0; i < n; i++) {
    if (ok[i] && ok[(i + m) % n]) {
      in[(i + m) % n]++;
      G[i].pb((i + m) % n);
    }
  }
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (!in[i]) {
      q.push(i);
      dp[i] = ok[i];
    }
  }
  while (sz(q)) {
    int u = q.front();
    q.pop();
    for (int v : G[u]) {
      in[v]--;
      if (!in[v]) {
        q.push(v);
        dp[v] = dp[u] + 1;
      }
    }
  }
  int ans = 0;
  bool cycle = 0;
  for (int i = 0; i < n; i++)
    if (in[i])
      cycle = 1;
  if (cycle)
    ans = -1;
  else
    ans = *max_element(dp, dp + n);
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
const int INF = 0x3f3f3f3f, N = 2e6 + 5;
const ll LINF = 1e18 + 5;
char s[N], t[N];
int n, m;
int nxt[N], in[N], dp[N];
bool ok[N];
vector<int> G[N];
void kmp_pre(char s[], int m, int nxt[]) {
  int i = 0, j = nxt[0] = -1;
  while (i < m) {
    while (j != -1 && s[i] != s[j])
      j = nxt[j];
    nxt[++i] = ++j;
  }
}
void kmp_work(char s[], int m, char t[], int n) {
  int i = 0, j = 0;
  kmp_pre(s, m, nxt);
  while (i < n) {
    while (j != -1 && t[i] != s[j])
      j = nxt[j];
    i++;
    j++;
    if (j >= m) {
      ok[i - m] = 1;
      j = nxt[j];
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> t;
  n = strlen(s), m = strlen(t);
  int nn = n;
  while (nn < n + m) {
    for (int i = nn, j = 0; j < n; i++, j++)
      s[i] = s[j];
    nn += n;
    s[nn] = 0;
  }
  kmp_work(t, m, s, nn);
  for (int i = 0; i < n; i++) {
    if (ok[i] && ok[(i + m) % n]) {
      in[(i + m) % n]++;
      G[i].pb((i + m) % n);
    }
  }
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (!in[i]) {
      q.push(i);
      dp[i] = ok[i];
    }
  }
  while (sz(q)) {
    int u = q.front();
    q.pop();
    for (int v : G[u]) {
      in[v]--;
      if (!in[v]) {
        q.push(v);
        dp[v] = dp[u] + 1;
      }
    }
  }
  int ans = 0;
  bool cycle = 0;
  for (int i = 0; i < n; i++)
    if (in[i])
      cycle = 1;
  if (cycle)
    ans = -1;
  else
    ans = *max_element(dp, dp + n);
  cout << ans;
  return 0;
}