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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define mem0(s) memset(s, 0, sizeof(s))
#define meminf(s) memset(s, 0x3f, sizeof(s))
#define mp_insert(a, b, c)                                                     \\
  mp.insert(map<pair<int, int>, int>::value_type(make_pair(a, b), c))
double pi = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x) { return x & (-x); }
inline ll min(ll a, ll b) { return a < b ? a : b; }
inline ll max(ll a, ll b) { return a > b ? a : b; }
const int INF = 0x3f3f3f3f, max_n = 200005, mod = 998244353;
inline ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
inline ll pow(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int qidian;
int huan;
int a[max_n];
int ci[max_n];
int bu;
int count1;
int ans[max_n];
vector<int> v;
int main() {
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int dang = 1;
  ci[dang] = 1;
  while (1) {
    dang = a[dang];
    count1++;
    ans[count1] = dang;
    if (ci[dang] == 0)
      ci[dang] = 1;
    else {
      qidian = dang;
      ci[dang]++;
      if (bu == 0)
        bu = count1;
    }
    if (qidian) {
      huan++;
      v.pb(dang);
    }
    if (ci[dang] == 3) {
      break;
    }
  }
  if (k < bu) {
    cout << ans[k];
  } else {
    huan--;
    k -= (bu - huan);
    cout << v[k % huan];
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define mem0(s) memset(s, 0, sizeof(s))
#define meminf(s) memset(s, 0x3f, sizeof(s))
#define mp_insert(a, b, c)                                                     \\
  mp.insert(map<pair<int, int>, int>::value_type(make_pair(a, b), c))
double pi = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x) { return x & (-x); }
inline ll min(ll a, ll b) { return a < b ? a : b; }
inline ll max(ll a, ll b) { return a > b ? a : b; }
const int INF = 0x3f3f3f3f, max_n = 200005, mod = 998244353;
inline ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
inline ll pow(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int qidian;
int huan;
int a[max_n];
int ci[max_n];
int bu;
int count1;
vector<int> v;
int main() {
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int dang = 1;
  ci[dang] = 1;
  while (1) {
    dang = a[dang];
    count1++;
    if (ci[dang] == 0)
      ci[dang] = 1;
    else {
      qidian = dang;
      // v.pb(qidian);
      ci[dang]++;
      if (bu == 0)
        bu = count1;
    }
    if (qidian) {
      huan++;
      v.pb(dang);
    }
    if (ci[dang] == 3) {
      break;
    }
  }
  huan--;
  k -= (bu - huan);
  cout << v[k % huan];
}