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

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

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define INF LLONG_MAX
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define tr(con, it) for (auto it = con.begin(); it != con.end(); it++)
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define present(con, ele) (con.find(ele) != con.end())
#define cpresent(con, ele) (find(all(con), ele) != con.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double lf;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vint a(n + 1);
  fr(i, 0, n) { cin >> a[i + 1]; }
  int now = 0, node = 1, ans = -1;
  vint vis(n + 1, 0), le(n + 1, 0);
  while (vis[node] == 0) {
    if (now == k) {
      ans = node;
    }
    vis[node] = 1;
    le[node] = now;
    node = a[node];
    now++;
  }
  if (ans == -1) {
    vint c;
    k -= le[node];
    c.pb(node);
    int ch = a[node];
    while (node != ch) {
      c.pb(ch);
      ch = a[ch];
    }
    k = k % sz(c);
    ans = c[k];
  }
  cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

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

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define INF LLONG_MAX
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define tr(con, it) for (auto it = con.begin(); it != con.end(); it++)
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define present(con, ele) (con.find(ele) != con.end())
#define cpresent(con, ele) (find(all(con), ele) != con.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double lf;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vint a(n + 1);
  fr(i, 0, n) { cin >> a[i + 1]; }
  int now = 0, node = 1, ans = -1, cyc = 0;
  vint vis(n + 1, 0), le(n + 1, 0);
  while (vis[node] == 0) {
    if (now == k) {
      ans = node;
    }
    vis[node] = 1;
    le[node] = now;
    node = a[node];
    now++;
  }
  if (ans = -1) {
    vint c;
    k -= le[node];
    c.pb(node);
    int ch = a[node];
    while (node != ch) {
      c.pb(ch);
      ch = a[ch];
    }
    k = k % sz(c);
    ans = c[k];
  }
  cout << ans << "\\n";
  return 0;
}