Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

// const long long int MOD = 1000000007;
const int MOD = 1000000007;
// const int MOD = 998244353;
// const long long int MOD = 998244353;

// long long int N, M, K, H, W, L, R;
int N, M, K, H, W, L, R;

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

  cin >> N;
  vector<vector<pair<int, int>>> edge(N + 10);
  for (int i = 0; i < N; i++) {
    cin >> M;
    if (!M) {
      for (int j = 1; j <= 6; j++) {
        edge[i].push_back({i + j, 1});
      }
    } else {
      edge[i].push_back({i + M, 0});
    }
  }
  vector<int> dis(N + 10, MOD);
  vector<int> ddis(N + 10, MOD);
  dis[0] = 0;
  deque<int> Q;
  Q.push_back(0);
  while (!Q.empty()) {
    int cn = Q.front();
    //	cout << cn << endl;
    Q.pop_front();
    if (ddis[cn] == dis[cn])
      continue;
    ddis[cn] = dis[cn];
    for (auto i : edge[cn]) {
      if (dis[i.first] > dis[cn] + i.second) {
        dis[i.first] = dis[cn] + i.second;
        if (i.second)
          Q.push_back(i.first);
        else
          Q.push_front(i.first);
      }
    }
  }
  cout << dis[N - 1] << endl;
}


Error: -11

Fixed submission:

#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

int Search(vector<int> &flag, vector<int> &to, vector<int> &dp, int n) {
  // cout << n <<" "<<to[n]<< endl;
  if (flag[n] == -1)
    return flag[n];
  if (dp[n] != MOD)
    return dp[n];
  if (n == to[n]) {
    int box = MOD;
    for (int i = 1; i <= 6; i++) {
      if (n + i >= N)
        continue;
      if (flag[n + i] == -1)
        continue;
      if (to[n + i] < n + i && dp[to[n + 1]] == MOD)
        continue;
      box = min(box, Search(flag, to, dp, n + i) + 1);
    }
    dp[n] = box;
  } else {
    if (flag[to[n]] == -1)
      return dp[n];
    dp[n] = Search(flag, to, dp, to[n]);
  }
  return dp[n];
}

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

  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    cin >> v[i];
  vector<int> flag(N);
  vector<int> to(N);
  for (int i = 0; i < N; i++) {
    if (!v[i]) {
      flag[i] = 1;
      to[i] = i;
    }
  }
  for (int i = 0; i < N; i++) {
    if (flag[i])
      continue;
    set<int> s;
    int cn = i;
    s.insert(i);
    while (1) {
      if (s.find(cn + v[cn]) == s.end()) {
        s.insert(cn + v[cn]);
        cn += v[cn];
      } else {
        if (flag[cn + v[cn]] == 1) {
          for (auto j : s) {
            flag[j] = 1;
            to[j] = cn + v[cn];
          }
        } else {
          for (auto j : s) {
            flag[j] = -1;
          }
        }
        break;
      }
    }
  }
  vector<int> dp(N, MOD);
  dp[N - 1] = 0;
  for (auto i : to)
    cout << i << endl;
  for (int i = 0; i < N; i++) {
    //	cout << i << endl;
    if (flag[i] == -1)
      continue;
    if (dp[i] != MOD)
      continue;
    Search(flag, to, dp, i);
  }
  // for (auto i : dp)cout << i << endl;
  cout << dp[0] << endl;
  return 0;
}