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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<double> VD;
typedef vector<LL> VLL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<long long>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<vector<double>> VVD;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(a) (a).begin(), (a).end()

const int MOD = 1000000007;

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

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

void debug(VI &arr) {
  int n = arr.size();
  rep(i, n) { cout << arr.at(i) << endl; }
}

void debug(VVI &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VVLL &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VS &board) {
  int h = board.size();
  rep(i, h) { cout << board.at(i) << endl; }
  return;
}

LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); }

LL lcm(LL a, LL b) { return a * b / gcd(a, b); }

int main() {
  // cout<<fixed;
  // cout<<setprecision(9)<<...
  int n;
  LL k;
  cin >> n >> k;
  int a[200005];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int town[200005];
  int cur = 1;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    town[i] = -1;
  }
  while (1) {
    cur = a[cur];
    k--;
    cnt++;
    if (town[cur] != -1) {
      k %= (cnt - town[cur]);
      break;
    }
    if (k == 0)
      break;
    town[cur] = cnt;
  }
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  cout << cur << endl;
  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<double> VD;
typedef vector<LL> VLL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<long long>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<vector<double>> VVD;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(a) (a).begin(), (a).end()

const int MOD = 1000000007;

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

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

void debug(VI &arr) {
  int n = arr.size();
  rep(i, n) { cout << arr.at(i) << endl; }
}

void debug(VVI &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VVLL &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VS &board) {
  int h = board.size();
  rep(i, h) { cout << board.at(i) << endl; }
  return;
}

LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); }

LL lcm(LL a, LL b) { return a * b / gcd(a, b); }

int main() {
  // cout<<fixed;
  // cout<<setprecision(9)<<...
  int n;
  LL k;
  cin >> n >> k;
  int a[20005];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int town[20005];
  int cur = 1;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    town[i] = -1;
  }
  while (1) {
    cur = a[cur];
    k--;
    cnt++;
    if (town[cur] != -1) {
      k %= (cnt - town[cur]);
      break;
    }
    town[cur] = cnt;
  }
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  cout << cur << endl;
  return 0;
}