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

#include <bits/stdc++.h>
#include <string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9 + 7)
#define nax (ll)(1e5)
#define INFL LLONG_MAX
#define INF INT_MAX
#define disp(a, n, arr)                                                        \\
  for (int i = a; i < (ll)n; i++)                                              \\
    cout << arr[i] << " ";                                                     \\
  cout << "\\n";
#define check cout << "Working\\n";
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FIO;

  // Write your code here

  int t = 1;
  // cin >> t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vl v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }

    vl rep(n + 10, -1);
    // rep[1] = 1;
    ll cur = 1;
    ll lim = 0;
    ll i = 1;
    while (k--) {
      if (rep[cur] != -1) {
        lim = i - rep[cur];
        break;
      } else {
        rep[cur] = i;
        cur = v[cur - 1];
        // cout << cur << " ";
      }
      i++;
    }
    k++;
    if (lim == 0) {
      cout << cur << "\\n";
      return 0;
    }
    ll ans = k % lim;
    ll result = cur;
    while (ans--) {
      result = v[cur - 1];
      cur = result;
    }

    cout << result << "\\n";
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#include <string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9 + 7)
#define nax (ll)(1e5)
#define INFL LLONG_MAX
#define INF INT_MAX
#define disp(a, n, arr)                                                        \\
  for (int i = a; i < (ll)n; i++)                                              \\
    cout << arr[i] << " ";                                                     \\
  cout << "\\n";
#define check cout << "Working\\n";
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FIO;

  // Write your code here

  int t = 1;
  // cin >> t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vl v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }

    vl rep(n + 10, -1);
    // rep[1] = 1;
    ll cur = 1;
    ll lim = 0;
    ll i = 1;
    while (k--) {
      if (rep[cur] != -1) {
        lim = i - rep[cur];
        break;
      } else {
        rep[cur] = i;
        cur = v[cur - 1];
        // cout << cur << " ";
      }
      i++;
    }
    k++;
    ll ans = k % lim;
    ll result = cur;
    while (ans--) {
      result = v[cur - 1];
      cur = result;
    }

    cout << result << "\\n";
  }

  return 0;
}