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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int lp;
vi a, all, loop;
vector<bool> visited;
void dfs_a(int i) {
  if (visited[i]) {
    lp = i;
    return;
  }
  visited[i] = true;
  all.emplace_back(i);
  dfs_a(a[i]);
}
void dfs_l(int i) {
  if (visited[i]) {
    lp = i;
    return;
  }
  visited[i] = true;
  loop.emplace_back(i);
  dfs_l(a[i]);
}

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

  int N;
  ll K;
  cin >> N >> K;

  a.resize(N);
  REP(i, N) {
    cin >> a[i];
    a[i]--;
  }
  visited = vector<bool>(N, false);
  dfs_a(0);
  visited = vector<bool>(N, false);
  dfs_l(lp);

  if (all.size() > K) {
    cout << all[K] + 1 << endl;
  } else {
    int l = all.size() - loop.size();
    int idx = (K - l) % loop.size();
    cout << loop[idx] + 1 << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int lp;
vi a, all, loop;
vector<bool> visited;
void dfs_a(int i) {
  if (visited[i]) {
    lp = i;
    return;
  }
  all.emplace_back(i);
  dfs_a(a[i]);
}
void dfs_l(int i) {
  if (visited[i]) {
    return;
  }
  loop.emplace_back(i);
  dfs_l(a[i]);
}

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

  int N;
  ll K;
  cin >> N >> K;

  a.resize(N);
  REP(i, N) {
    cin >> a[i];
    a[i]--;
  }
  visited = vector<bool>(N, false);
  dfs_a(0);
  visited = vector<bool>(N, false);
  dfs_l(lp);

  if (all.size() > K) {
    cout << all[K] + 1 << endl;
  } else {
    int l = all.size() - loop.size();
    int idx = (K - l) % loop.size();
    cout << loop[idx] + 1 << endl;
  }

  return 0;
}