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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Kein Yukiyoshi
 */

#include "bits/stdc++.h"
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(obj) begin(obj), end(obj)
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define LOWER_BOUND(A, key) distance(A.begin(), lower_bound(ALL(A), key))
#define UPPER_BOUND(A, key) distance(A.begin(), upper_bound(ALL(A), key))
#define Endl endl

using namespace std;
using ll = long long;
const int MOD = (int)(1e9 + 7);
const int INF = (int)(1e13 + 7);

int ceil_(int a, int b) {
  return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b);
}
int mod(int a, int b) { return a >= 0 ? a % b : a - (b * ceil_(a, b)); }
int bpm(int x, unsigned int y) {
  if (x == 0)
    return 0;
  if (y == 0)
    return 1;
  int ans = 1;
  int digit = (int)((log((double)y) / log((double)2) / 1 + 1));
  x %= MOD;
  for (int i = 0; i < digit; i++) {
    if (((y >> i) & 1u) == 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
  }
  return ans;
}
template <class T> void cumulative_sum(T &container) {
  for (int i = 0; i < container.size() - 1; i++)
    container[i + 1] += container[i];
}

class DTeleporter {
public:
  static void solve(std::istream &cin, std::ostream &cout) {
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    rep(i, N) cin >> A[i];
    int first;
    int now = 1;
    int num;
    vector<int> temp{};
    map<int, int> where{};
    FOR(i, 1, 10000000) {
      temp.emplace_back(now);
      if (where[now]) {
        num = i - where[now];
        first = where[now] - 1;
        break;
      }
      where[now] = i;
      now = A[now - 1];
    }
    if (K >= first) {
      cout << temp[(K - first) % num + first] << endl;
    } else {
      cout << temp[K] << endl;
    }
  }
};

signed main() {
  DTeleporter solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Kein Yukiyoshi
 */

#include "bits/stdc++.h"
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(obj) begin(obj), end(obj)
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define LOWER_BOUND(A, key) distance(A.begin(), lower_bound(ALL(A), key))
#define UPPER_BOUND(A, key) distance(A.begin(), upper_bound(ALL(A), key))
#define Endl endl

using namespace std;
using ll = long long;
const int MOD = (int)(1e9 + 7);
const int INF = (int)(1e13 + 7);

int ceil_(int a, int b) {
  return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b);
}
int mod(int a, int b) { return a >= 0 ? a % b : a - (b * ceil_(a, b)); }
int bpm(int x, unsigned int y) {
  if (x == 0)
    return 0;
  if (y == 0)
    return 1;
  int ans = 1;
  int digit = (int)((log((double)y) / log((double)2) / 1 + 1));
  x %= MOD;
  for (int i = 0; i < digit; i++) {
    if (((y >> i) & 1u) == 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
  }
  return ans;
}
template <class T> void cumulative_sum(T &container) {
  for (int i = 0; i < container.size() - 1; i++)
    container[i + 1] += container[i];
}

class DTeleporter {
public:
  static void solve(std::istream &cin, std::ostream &cout) {
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    rep(i, N) cin >> A[i];
    int now = 1;
    int num = 1;
    vector<int> temp;
    rep(i, N) {
      now = A[now - 1];
      if (now == 1) {
        break;
      }
      num++;
      temp.emplace_back(now);
    }
    cout << temp[K % num - 1] << endl;
  }
};

signed main() {
  DTeleporter solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}