Problem p02684 (C++)

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

#define ff first
#define ss second
#define ll long long
#define dd double
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<long long>
#define vdd vector<double>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long, long long>>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mii map<int, int>
#define mll map<long long, long long>
#define umii unordered_map<int, int>
#define umll unordered_map<long long, long long>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, b) for (int i = a; i < b; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define rev2(i, a, b) for (int i = a; i >= b; i--)
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 9e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define all(x) x.begin(), x.end()
#define d0(x) cout << x << " "
#define d1(x) cout << x << endl
#define d2(x, y) cout << x << " " << y << endl
#define d3(x, y, z) cout << x << " " << y << " " << z << endl
#define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << endl
#define read2(x, y) cin >> x >> y
#define read3(x, y, z) cin >> x >> y >> z
#define read4(x, y, z, w) cin >> x >> y >> z >> w
#define read5(x, y, z, w, a) cin >> x >> y >> z >> w >> a
#define sz(a) (int)a.size()
const ll mod = 1e9 + 7;
const double PI = 3.14159265359;
const ll MOD = 998244353;
void i_o() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int main() {

  fio i_o();
  ll n, k;
  cin >> n >> k;
  vll A(n + 1);
  rep2(i, 1, n + 1) cin >> A[i];
  ll curr = 1;
  ll rep = -1;
  if (k < 100005) {
    ll k1 = k;
    while (k1--) {
      if (curr == A[curr])
        rep = curr;
      curr = A[curr];
    }
    d1(curr);
    return 0;
  }
  if (rep != -1) {
    d1(rep);
    return 0;
  }
  // d1("ok");
  map<ll, ll> m;
  ll k2 = k;
  curr = 1;
  ll used = 0;
  ll st = -1;
  // for(int i=1;i<=n;i++) d0(A[i]);
  // cout <<endl;
  while (k2--) {
    if (m.count(curr)) {
      st = curr;
      // d2("curr is ",curr);
      break;
    }
    m[curr] = used;
    curr = A[curr];
    used++;
  }
  // m.clear();
  ll k3 = k;
  curr = st;
  ll use = m[st];
  ll ini = use;
  m.clear();
  // d2("St is ",st);
  ll fin;
  while (1 > 0) {
    if (m.count(curr)) {
      fin = use;
      break;
    }
    m[curr] = use;
    curr = A[curr];
    use++;
  }
  k = k % (fin - ini);
  // d2(fin,ini);
  for (auto it : m) {
    if (it.second % (fin - ini) == k) {
      d1(it.first);
      return 0;
    }
  }

  return 0;
}


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

#define ff first
#define ss second
#define ll long long
#define dd double
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<long long>
#define vdd vector<double>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long, long long>>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mii map<int, int>
#define mll map<long long, long long>
#define umii unordered_map<int, int>
#define umll unordered_map<long long, long long>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, b) for (int i = a; i < b; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define rev2(i, a, b) for (int i = a; i >= b; i--)
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 9e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define all(x) x.begin(), x.end()
#define d0(x) cout << x << " "
#define d1(x) cout << x << endl
#define d2(x, y) cout << x << " " << y << endl
#define d3(x, y, z) cout << x << " " << y << " " << z << endl
#define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << endl
#define read2(x, y) cin >> x >> y
#define read3(x, y, z) cin >> x >> y >> z
#define read4(x, y, z, w) cin >> x >> y >> z >> w
#define read5(x, y, z, w, a) cin >> x >> y >> z >> w >> a
#define sz(a) (int)a.size()
const ll mod = 1e9 + 7;
const double PI = 3.14159265359;
const ll MOD = 998244353;
void i_o() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int main() {

  fio i_o();
  ll n, k;
  cin >> n >> k;
  vll A(n + 1);
  rep2(i, 1, n + 1) cin >> A[i];
  ll curr = 1;
  ll rep = -1;
  if (k < 400005) {
    ll k1 = k;
    while (k1--) {
      if (curr == A[curr])
        rep = curr;
      curr = A[curr];
    }
    d1(curr);
    return 0;
  }
  if (rep != -1) {
    d1(rep);
    return 0;
  }
  // d1("ok");
  map<ll, ll> m;
  ll k2 = k;
  curr = 1;
  ll used = 0;
  ll st = -1;
  // for(int i=1;i<=n;i++) d0(A[i]);
  // cout <<endl;
  while (k2--) {
    if (m.count(curr)) {
      st = curr;
      // d2("curr is ",curr);
      break;
    }
    m[curr] = used;
    curr = A[curr];
    used++;
  }
  // m.clear();
  ll k3 = k;
  curr = st;
  ll use = m[st];
  ll ini = use;
  m.clear();
  // d2("St is ",st);
  ll fin;
  while (1 > 0) {
    if (m.count(curr)) {
      fin = use;
      break;
    }
    m[curr] = use;
    curr = A[curr];
    use++;
  }
  k = k % (fin - ini);
  // d2(fin,ini);
  for (auto it : m) {
    if (it.second % (fin - ini) == k) {
      d1(it.first);
      return 0;
    }
  }

  return 0;
}