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

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

int main() {
  lint N, K;
  cin >> N >> K;
  vector<lint> A(N, 0);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> path(1, 0);
  set<int> check{0};
  int cn = 0;
  int start = -1;
  while (true) {
    int nc = A[cn];
    if (check.count(nc)) {
      start = nc;
      break;
    } else {
      path.push_back(nc);
      cn = nc;
      check.insert(nc);
    }
  }
  int x = -1;
  int p = path.size();
  for (int i = 0; i < p; i++) {
    if (path[i] == start) {
      x = i;
    }
  }
  if (K < x) {
    cout << path[K] + 1 << endl;
    return 0;
  }
  K -= (lint)x;
  vector<lint> path2(p - x);
  rep(i, path2.size()) { path2[i] = path[i + x]; }
  lint mod = path2.size();
  cout << path2[K % mod] + 1 << endl;
  // dump(path);
  // d(start);
  // dump(path2);
  // d(x);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

int main() {
  lint N, K;
  cin >> N >> K;
  vector<lint> A(N, 0);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> path(1, 0);
  set<int> check{0};
  int cn = 0;
  int start = -1;
  while (true) {
    int nc = A[cn];
    if (check.count(nc)) {
      start = nc;
      break;
    } else {
      path.push_back(nc);
      cn = nc;
      check.insert(nc);
    }
  }
  int x = -1;
  int p = path.size();
  for (int i = 0; i < p; i++) {
    if (path[i] == start) {
      x = i;
    }
  }
  if (x == -1) {
    return 1;
  }
  K -= (lint)x;
  vector<lint> path2(p - x);
  rep(i, path2.size()) { path2[i] = path[i + x]; }
  lint mod = path2.size();
  cout << path2[K % mod] + 1 << endl;
  // dump(path);
  // d(start);
  // dump(path2);
  // d(x);
  return 0;
}