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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define endl "\\n"
#define loop(i, s, e) for (int i = s; i < e; i++)
#define int long long
#define vi vector<int>
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define R(v) reverse(v.begin(), v.end())
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define prv(v)                                                                 \\
  for (auto &x : v)                                                            \\
    cout << x << " ";
#define piirv(v)                                                               \\
  for (auto &x : v)                                                            \\
    cout << x.ff << " " << x.ss << endl;
#define prvv(v)                                                                \\
  for (auto &x : v)                                                            \\
    for (auto &y : x.ss)                                                       \\
      cout << y << " ";
#define mp make_pair
#define pii pair<int, int>
#define mset(a, f) memset(a, f, sizeof(a))
#define pb push_back
#define inf INT_MAX
#define INF LLONG_MAX
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int N = 2e6 + 6;
int A[N];
int B[N];
bool vis[N];
vector<int> v1;
int n;
int len, ex;

void dfs(int x, int l) {
  v1.pb(x);
  vis[x] = true;
  B[x] = l;
  if (vis[A[x]] == 1) {
    len = l - B[A[x]] + 1;
    ex = l - len;
    // cout << l << " " << x << endl;
  }
  if (!vis[A[x]])
    dfs(A[x], l + 1);
}

void solve() {
  int n, k;
  cin >> n >> k;

  loop(i, 1, n + 1) cin >> A[i];

  len = 0, ex = 0;
  dfs(1, 1);

  // cout << len << " " << ex << endl;

  int rem = (k - ex) % len;
  if (k <= ex)
    cout << v1[k];
  else
    cout << v1[ex + rem];
}

int32_t main() {

  cout << fixed << setprecision(16);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define endl "\\n"
#define loop(i, s, e) for (int i = s; i < e; i++)
#define int long long
#define vi vector<int>
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define R(v) reverse(v.begin(), v.end())
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define prv(v)                                                                 \\
  for (auto &x : v)                                                            \\
    cout << x << " ";
#define piirv(v)                                                               \\
  for (auto &x : v)                                                            \\
    cout << x.ff << " " << x.ss << endl;
#define prvv(v)                                                                \\
  for (auto &x : v)                                                            \\
    for (auto &y : x.ss)                                                       \\
      cout << y << " ";
#define mp make_pair
#define pii pair<int, int>
#define mset(a, f) memset(a, f, sizeof(a))
#define pb push_back
#define inf INT_MAX
#define INF LLONG_MAX
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int N = 2e6 + 6;
int A[N];
int B[N];
bool vis[N];
vector<int> v1;
int n;
int len, ex;

void dfs(int x, int l) {
  if (vis[A[x]] == 1) {
    len = l - B[A[x]] + 1;
    ex = l - len;
    // cout << l << " " << x << endl;
  }
  v1.pb(x);
  vis[x] = true;
  B[x] = l;
  if (!vis[A[x]])
    dfs(A[x], l + 1);
}

void solve() {
  int n, k;
  cin >> n >> k;

  loop(i, 1, n + 1) cin >> A[i];

  len = 0, ex = 0;
  dfs(1, 1);

  // cout << len << " " << ex << endl;

  int rem = (k - ex) % len;

  if (k < ex)
    cout << v1[k - 1];
  else
    cout << v1[ex + rem];
}

int32_t main() {

  cout << fixed << setprecision(16);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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