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

#include <bits/stdc++.h>
#define my_max(a, b) ((a) > (b) ? (a) : (b))
#define my_min(a, b) ((a) < (b) ? (a) : (b))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i, s, t) for (int i = (int)(s); i <= (int)(t); i++)
#define rev(i, t, s) for (int i = (int)(t); i >= (int)(s); i--)
#define lson rt << 1
#define rson rt << 1 | 1
#define sz(x) (int)(x).size()

typedef long long ll;
typedef long double lb;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::vector<int> VI;
typedef std::vector<ll> VL;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    rbtree;
ll gcd(ll x, ll y) { return y % x == 0 ? x : gcd(y % x, x); }
template <class T> T my_abs(T a) {
  if (a < 0)
    a = -a;
  return a;
}
inline ll read() {
  ll ret = 0, sign = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret * sign;
}
inline void write(ll x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

// void expand_stack_space(){
//     int size = 256 << 20; // 256MB
//     char *p = (char*)malloc(size) + size;
//     __asm__("movl %0, %%esp\\n" :: "r"(p));
// }
const int maxn = 2e5 + 7;
int a[maxn], n, vis[maxn], st[maxn], top;
ll k;

int dfs(int u) {
  if (k == 0)
    return u;
  if (vis[u]) {
    int cycsz = 0, j;
    for (j = top; j > 0; --j)
      if (st[j] == u)
        break;
    cycsz = top - j + 1;
    k %= cycsz;
    return st[j + k];
  }
  st[++top] = u;
  vis[u] = 1;
  --k;
  return dfs(a[u]);
}
int main() {
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
  }
  printf("%d\\n", dfs(1));
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define my_max(a, b) ((a) > (b) ? (a) : (b))
#define my_min(a, b) ((a) < (b) ? (a) : (b))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i, s, t) for (int i = (int)(s); i <= (int)(t); i++)
#define rev(i, t, s) for (int i = (int)(t); i >= (int)(s); i--)
#define lson rt << 1
#define rson rt << 1 | 1
#define sz(x) (int)(x).size()

typedef long long ll;
typedef long double lb;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::vector<int> VI;
typedef std::vector<ll> VL;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    rbtree;
ll gcd(ll x, ll y) { return y % x == 0 ? x : gcd(y % x, x); }
template <class T> T my_abs(T a) {
  if (a < 0)
    a = -a;
  return a;
}
inline ll read() {
  ll ret = 0, sign = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret * sign;
}
inline void write(ll x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

// void expand_stack_space(){
//     int size = 256 << 20; // 256MB
//     char *p = (char*)malloc(size) + size;
//     __asm__("movl %0, %%esp\\n" :: "r"(p));
// }
const int maxn = 2e5 + 7;
int a[maxn], n, vis[maxn], st[maxn], top;
ll k;

int dfs(int u) {
  if (k == 0)
    return u;
  if (vis[u]) {
    int cycsz = 0, j;
    for (j = top; j > 0; --j)
      if (st[j] == u)
        break;
    cycsz = top - j + 1;
    k %= cycsz;
    return st[j + k];
  }
  st[++top] = u;
  vis[u] = 1;
  --k;
  dfs(a[u]);
}
int main() {
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
  }
  printf("%d\\n", dfs(1));
  return 0;
}