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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound, __gcd, count, find, next_permutation
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   // setprecsion
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector

#include <cassert> // assert
#include <cctype>  // isupper, islower, isdigit, toupper, tolower
#include <cmath>   // abs, sin, cos, tan
#include <ctime>   // clock

// __builtin_popcountll

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

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T> void fin(T a) {
  cout << a << endl;
  exit(0);
}

void Main() {
  ll n, k;
  cin >> n >> k;
  vll a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vll loop;
  map<ll, ll> twin;
  ll my = 0;
  twin[0] = 0;
  ll frist, end;
  rep(i, n) {
    my = a[my];
    if (twin.count(my)) {
      frist = twin[my];
      end = i + 1;
      break;
    } else {
      twin[my] = i + 1;
    }
  }

  // e
  if (k < 2 * 1000000) {
    my = 0;
    rep(i, k) { my = a[my]; }
    cout << my + 1 << endl;
    return;
  }

  k -= frist;
  my = 0;
  for (ll i = 0; i < end; i++) {
    if (i >= frist) {
      loop.pb(my);
    }
    my = a[my];
  }
  cout << loop[k % loop.size()] + 1 << endl;
  return;
}

// 前処理
int main() {
  int ti = clock();
  srand((unsigned)time(NULL));
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(20); //高精度少数表示
  // cout << setfill('0') << internal << setw(4); //0埋め４桁
  Main();
  // printf("Execution Time: %.4lf sec\\n", 1.0 * (clock() - ti) /
  // CLOCKS_PER_SEC); // 実行時間計測
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   //setprecsion
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector

#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>  //sin, cos, tan

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T> void fin(T a) {
  cout << a << endl;
  exit(0);
}

void Main() {
  ll n, k;
  cin >> n >> k;
  vll a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  ll ans = 0;
  if (n >= k) {
    rep(i, k) { ans = a[ans]; }
  } else {
    map<ll, ll> m;
    ll base;
    ll flag;
    vll G;
    ll size;
    m[0] = 0;
    rep(i, k) {

      ans = a[ans];
      if (m.count(ans)) {
        base = ans;
        size = i - m[ans] + 1;
        k -= m[ans] + 1;
        break;
      } else {
        m[ans] = i;
      }
      G.pb(ans);
    }
    ans = G[k % size];
  }
  fin(ans + 1);
}

// 前処理
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  cout << fixed << setprecision(20); // 高精度少数表示
  // cout << setfill('0') << internal << setw(4); 0埋め４桁
  Main();
  return 0;
}