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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rando                                                                  \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y)                                                           \\
  cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi << "," << x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll) x.size()
#define ld long double
#define ll long long
#define int ll
#define ii pair<int, int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int>>
#define mp make_pair
#define INF 1000000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
int A[200005], idx, d[200005];
v vec;
bitset<200005> vis;
void dfs(int node) {
  if (vis[node]) {
    idx = node;
    return;
  }
  vec.pb(node);
  vis[node] = 1;
  if (!vis[A[node]])
    d[A[node]] = d[node] + 1;
  dfs(A[node]);
}
int32_t main() {
  boost;
  // freopen(".in","r",stdin);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> A[i];
  dfs(1);
  if (k <= d[idx])
    cout << vec[k] << "\\n";
  else {
    k -= d[idx];
    cout << vec[(k % (sz(vec) - d[idx])) + d[idx]] << "\\n";
  }
  return 0;
}
// long long
// array bounds
// special cases
// binary search

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rando                                                                  \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y)                                                           \\
  cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi << "," << x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll) x.size()
#define ld long double
#define ll long long
#define int ll
#define ii pair<int, int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int>>
#define mp make_pair
#define INF 1000000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
int A[200005], idx, d[200005];
v vec;
bitset<200005> vis;
void dfs(int node) {
  if (vis[node]) {
    idx = node;
    return;
  }
  vec.pb(node);
  if (!vis[A[node]])
    d[A[node]] = d[node] + 1;
  vis[node] = 1;
  dfs(A[node]);
}
int32_t main() {
  boost;
  // freopen(".in","r",stdin);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> A[i];
  dfs(1);
  if (k <= d[idx])
    cout << vec[k] << "\\n";
  else {
    k -= d[idx];
    cout << vec[(k % (sz(vec) - d[idx])) + d[idx]] << "\\n";
  }
  return 0;
}
// long long
// array bounds
// special cases
// binary search