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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define endl "\\n"
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define pr(x) cout << x << endl;
#define pr2(x, y) cout << x << " " << y << endl;
#define pr3(x, y, z) cout << x << " " << y << endl;
#define prv(v)                                                                 \\
  for (auto x : v)                                                             \\
    cout << x << " ";
#define ffs fflush(stdout);
#define int ll
using namespace std;

const ll MOD = 1e9 + 7;
const ll INF = 1e12;
const ll LOG = 29;
#define PI 3.141592653589793238

long long binpow(long long a, long long b) {
  a %= MOD;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;

    b >>= 1;
  }
  res %= MOD;
  return res;
}
const ll N = (2e5 + 5);
ll nxt[N];
ll a[N];
void solve() {
  ll n, k;
  cin >> n;
  cin >> k;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    nxt[i] = a[i];
  }
  ll sz = 0;
  ll cur = 1;
  set<ll> se;
  vll gg;
  ll pos = -1;
  while (true) {
    if (se.find(cur) != se.end()) {
      pos = cur;
      break;
    }
    sz++;
    se.insert(cur);
    gg.pb(cur);
    cur = nxt[cur];
  }
  assert(pos != -1);
  vll gg2;
  se.clear();
  while (true) {
    if (se.find(pos) != se.end())
      break;
    gg2.pb(pos);
    se.insert(pos);
    pos = nxt[pos];
  }
  ll sz2 = 1ll * gg2.size();

  ll pehle = sz - sz2;
  if (k <= pehle) {
    cout << gg[k] << endl;
    return;
  }
  k -= pehle;

  k %= sz2;
  cout << gg2[k] << endl;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  ll t = 1;
  //   cin>>t;
  while (t--) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define endl "\\n"
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define pr(x) cout << x << endl;
#define pr2(x, y) cout << x << " " << y << endl;
#define pr3(x, y, z) cout << x << " " << y << endl;
#define prv(v)                                                                 \\
  for (auto x : v)                                                             \\
    cout << x << " ";
#define ffs fflush(stdout);
#define int ll
using namespace std;

const ll MOD = 1e9 + 7;
const ll INF = 1e12;
const ll LOG = 29;
#define PI 3.141592653589793238

long long binpow(long long a, long long b) {
  a %= MOD;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;

    b >>= 1;
  }
  res %= MOD;
  return res;
}
const ll N = (2e5 + 5);
ll nxt[N];
ll a[N];
void solve() {
  ll n, k;
  cin >> n;
  cin >> k;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    nxt[i] = a[i];
  }
  ll sz = 0;
  ll cur = 1;
  set<ll> se;
  vll gg;
  ll pos = -1;
  while (true) {
    if (se.find(cur) != se.end()) {
      pos = cur;
      break;
    }
    sz++;
    se.insert(cur);
    gg.pb(cur);
    cur = nxt[cur];
  }
  assert(pos != -1);
  vll gg2;
  se.clear();
  while (true) {
    if (se.find(pos) != se.end())
      break;
    gg2.pb(pos);
    se.insert(pos);
    pos = nxt[pos];
  }
  ll sz2 = 1ll * gg2.size();

  ll pehle = sz - sz2;
  k -= pehle;
  if (k <= pehle) {
    cout << gg[k] << endl;
    return;
  }
  k %= sz2;
  cout << gg2[k] << endl;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  ll t = 1;
  //   cin>>t;
  while (t--) {
    solve();
  }
}