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

/*
            _           __                _      _____ _____ _____
  ___ _   _| |__   ___ / _|_ __ ___  __ _| | __ |___  |___  |___  |
 / __| | | | '_ \\ / _ \\ |_| '__/ _ \\/ _` | |/ /    / /   / /   / /
| (__| |_| | |_) |  __/  _| | |  __/ (_| |   <    / /   / /   / /
 \\___|\\__,_|_.__/ \\___|_| |_|  \\___|\\__,_|_|\\_\\  /_/   /_/   /_/

*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;
typedef vector<pll> vpll;
typedef unordered_map<ll, ll> umap;
// #pragma GCC optimize "trapv"
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define For(i, n) for (int i = 0; i < (ll)n; i++)
#define Rev(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, n) for (int i = 1; i <= n; ++i)
#define F first
#define S second
#define pb push_back
#define em emplace_back
#define all(v) (v).begin(), (v).end()
#define mems(x, y) memset(x, y, sizeof(x))
#define sz(v) (v).size()
#define mp(a, b) make_pair(a, b)
#define pf(n) cout << n << "\\n"
#define pff(n) cout << n << " ";
#define ar array
#define endl "\\n"
#define int ll
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
long const M = 1e9 + 7;
const long mxN = 2e5 + 2;
ll a[mxN];
void solve() {
  ll n, k;
  cin >> n >> k;
  Rep(i, n) { cin >> a[i]; }
  int x = 1;
  int cnt = 0, steps = 1;
  map<int, int> check;
  check[x] = steps;
  int cyc = 0;
  vector<int> pos = {x};

  while (1) {
    // cout << "iteration  " << steps <<" " << x << endl ;
    if (check[a[x]] != 0) {
      // cout << check[a[x]] << endl ;
      ++steps;
      cyc = steps - check[a[x]];
      break;
    }
    ++steps;
    x = a[x];
    check[x] = steps;
    pos.pb(x);
  }
  vi cycle;
  while (sz(cycle) != cyc) {
    cycle.pb(pos.back());
    pos.pop_back();
  }
  reverse(all(cycle));
  if (k < sz(pos)) {
    cout << pos[k] << endl;
    return;
  }
  k -= sz(pos);
  ll rem = k % cyc;
  cout << cycle[rem] << endl;

  // for(int x:pos)pff(x) ;
  // cout << endl ;
  // // cout << steps << endl ;
  // cout << cyc << endl ;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // cout << setprecision(20) << fixed ;
  solve();
  // ll t ; cin >> t ; while(t--)solve();
  return 0;
}
// if(s.count()){
//   ++steps ;
//   break ;
// }
// ++steps ;
// x=a[x] ;


Error: WA

Fixed submission:

/*
            _           __                _      _____ _____ _____
  ___ _   _| |__   ___ / _|_ __ ___  __ _| | __ |___  |___  |___  |
 / __| | | | '_ \\ / _ \\ |_| '__/ _ \\/ _` | |/ /    / /   / /   / /
| (__| |_| | |_) |  __/  _| | |  __/ (_| |   <    / /   / /   / /
 \\___|\\__,_|_.__/ \\___|_| |_|  \\___|\\__,_|_|\\_\\  /_/   /_/   /_/

*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;
typedef vector<pll> vpll;
typedef unordered_map<ll, ll> umap;
// #pragma GCC optimize "trapv"
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define For(i, n) for (int i = 0; i < (ll)n; i++)
#define Rev(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, n) for (int i = 1; i <= n; ++i)
#define F first
#define S second
#define pb push_back
#define em emplace_back
#define all(v) (v).begin(), (v).end()
#define mems(x, y) memset(x, y, sizeof(x))
#define sz(v) (v).size()
#define mp(a, b) make_pair(a, b)
#define pf(n) cout << n << "\\n"
#define pff(n) cout << n << " ";
#define ar array
#define endl "\\n"
#define int ll
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
long const M = 1e9 + 7;
const long mxN = 2e5 + 2;
ll a[mxN];
void solve() {
  ll n, k;
  cin >> n >> k;
  Rep(i, n) { cin >> a[i]; }
  ll x = 1;
  ll cnt = 0, steps = 1;
  map<ll, ll> check;
  check[x] = steps;
  ll cyc = 0;
  vector<ll> pos = {};

  while (1) {
    // cout << "iteration  " << steps <<" " << x << endl ;
    if (check[a[x]] != 0) {
      // cout << check[a[x]] << endl ;
      ++steps;
      cyc = steps - check[a[x]];
      break;
    }
    ++steps;
    x = a[x];
    check[x] = steps;
    pos.pb(x);
  }
  vi cycle;
  while (sz(cycle) != cyc) {
    cycle.pb(pos.back());
    pos.pop_back();
  }
  reverse(all(cycle));
  if (k <= sz(pos)) {
    cout << pos[k - 1] << endl;
    return;
  }
  k -= sz(pos);
  ll rem = k % cyc;
  cout << cycle[rem] << endl;

  // for(int x:pos)pff(x) ;
  // cout << endl ;
  // // cout << steps << endl ;
  // cout << cyc << endl ;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // cout << setprecision(20) << fixed ;
  solve();
  // ll t ; cin >> t ; while(t--)solve();
  return 0;
}
// if(s.count()){
//   ++steps ;
//   break ;
// }
// ++steps ;
// x=a[x] ;