Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;
using Graph = vector<vector<int>>;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REPA(i, a)                                                             \
  for (ll i = 0; i < (ll)(a.size()); i++)                                      \
    ;
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define COUT(a) cout << (a) << endl;
#define ENDL(a) cout << endl;
#define COUTA(i, a)                                                            \
  for (ll i = 0; i < (ll)(a.size()); i++) {                                    \
    cout << (a)[i] << " ";                                                     \
  }                                                                            \
  cout << endl;

const unsigned int BIT_FLAG_0 = (1 << 0);   // 0000 0000 0000 0001
const unsigned int BIT_FLAG_1 = (1 << 1);   // 0000 0000 0000 0010
const unsigned int BIT_FLAG_2 = (1 << 2);   // 0000 0000 0000 0100
const unsigned int BIT_FLAG_3 = (1 << 3);   // 0000 0000 0000 1000
const unsigned int BIT_FLAG_4 = (1 << 4);   // 0000 0000 0001 0000
const unsigned int BIT_FLAG_5 = (1 << 5);   // 0000 0000 0010 0000
const unsigned int BIT_FLAG_6 = (1 << 6);   // 0000 0000 0100 0000
const unsigned int BIT_FLAG_7 = (1 << 7);   // 0000 0000 1000 0000
const unsigned int BIT_FLAG_8 = (1 << 8);   // 0000 0001 0000 0000
const unsigned int BIT_FLAG_9 = (1 << 9);   // 0000 0010 0000 0000
const unsigned int BIT_FLAG_10 = (1 << 10); // 0000 0100 0000 0000
const unsigned int BIT_FLAG_11 = (1 << 11); // 0000 1000 0000 0000

const double PI = 3.14159265358979323846; // or M_PI

void Main() {
  ll n, k, d, count = 0, roop = 0, roop_start = 0, in_a;

  ifstream in("sub1_22.txt");
  cin.rdbuf(in.rdbuf());

  cin >> n >> k;
  vector<ll> a;
  vector<ll> dest;
  vector<ll> ikeru_check(n + 1, -1);

  a.push_back(0);
  REP(i, n) {
    cin >> in_a;
    a.push_back(in_a);
  }
  // COUTA(i, a);

  d = 1;
  REP(i, k) {
    if (ikeru_check[d] != -1) {
      roop_start = ikeru_check[d];
      roop = count - ikeru_check[d];
      // COUT(roop);
      break;
    }
    ikeru_check[d] = count;
    dest.push_back(d);
    d = a[d];
    count++;
  }
  dest.push_back(d);
  // COUT(count);
  // COUT(k);
  // COUT(dest.size());

  vector<ll> l(dest.begin() + roop_start, dest.end());
  // COUTA(i, dest);
  // COUTA(i, l);

  if (k <= count) {
    COUT(dest[k]);
  } else {
    COUT(l[((k - roop_start) % roop)]);
  }

  // COUT(count);
  //  COUTA(i, a);
  //  COUTA(i, dest);
  //  COUTA(i, ikeru_check);
  //  COUT(roop);
  //  COUT(roop_start);
  //  COUT(k % roop);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}


Fixed submission:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;
using Graph = vector<vector<int>>;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REPA(i, a)                                                             \
  for (ll i = 0; i < (ll)(a.size()); i++)                                      \
    ;
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define COUT(a) cout << (a) << endl;
#define ENDL(a) cout << endl;
#define COUTA(i, a)                                                            \
  for (ll i = 0; i < (ll)(a.size()); i++) {                                    \
    cout << (a)[i] << " ";                                                     \
  }                                                                            \
  cout << endl;

const unsigned int BIT_FLAG_0 = (1 << 0);   // 0000 0000 0000 0001
const unsigned int BIT_FLAG_1 = (1 << 1);   // 0000 0000 0000 0010
const unsigned int BIT_FLAG_2 = (1 << 2);   // 0000 0000 0000 0100
const unsigned int BIT_FLAG_3 = (1 << 3);   // 0000 0000 0000 1000
const unsigned int BIT_FLAG_4 = (1 << 4);   // 0000 0000 0001 0000
const unsigned int BIT_FLAG_5 = (1 << 5);   // 0000 0000 0010 0000
const unsigned int BIT_FLAG_6 = (1 << 6);   // 0000 0000 0100 0000
const unsigned int BIT_FLAG_7 = (1 << 7);   // 0000 0000 1000 0000
const unsigned int BIT_FLAG_8 = (1 << 8);   // 0000 0001 0000 0000
const unsigned int BIT_FLAG_9 = (1 << 9);   // 0000 0010 0000 0000
const unsigned int BIT_FLAG_10 = (1 << 10); // 0000 0100 0000 0000
const unsigned int BIT_FLAG_11 = (1 << 11); // 0000 1000 0000 0000

const double PI = 3.14159265358979323846; // or M_PI

void Main() {
  ll n, k, d, count = 0, roop = 0, roop_start = 0, in_a;

  // ifstream in("sub1_22.txt");
  // cin.rdbuf(in.rdbuf());

  cin >> n >> k;
  vector<ll> a;
  vector<ll> dest;
  vector<ll> ikeru_check(n + 1, -1);

  a.push_back(0);
  REP(i, n) {
    cin >> in_a;
    a.push_back(in_a);
  }
  // COUTA(i, a);

  d = 1;
  REP(i, k) {
    if (ikeru_check[d] != -1) {
      roop_start = ikeru_check[d];
      roop = count - ikeru_check[d];
      // COUT(roop);
      break;
    }
    ikeru_check[d] = count;
    dest.push_back(d);
    d = a[d];
    count++;
  }
  dest.push_back(d);
  // COUT(count);
  // COUT(k);
  // COUT(dest.size());

  vector<ll> l(dest.begin() + roop_start, dest.end());
  // COUTA(i, dest);
  // COUTA(i, l);

  if (k <= count) {
    COUT(dest[k]);
  } else {
    COUT(l[((k - roop_start) % roop)]);
  }

  // COUT(count);
  //  COUTA(i, a);
  //  COUTA(i, dest);
  //  COUTA(i, ikeru_check);
  //  COUT(roop);
  //  COUT(roop_start);
  //  COUT(k % roop);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}