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

#include <bits/stdc++.h>
using namespace std;
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_CAT_I(x, y) x##y

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
using ll = long long;
constexpr i64 INF = 1010000000000000017LL;
constexpr i64 MOD = 1000000007LL;
constexpr f64 EPS = 1e-12;
constexpr f64 PI = 3.14159265358979323846;
#define FOR(i, start, end)                                                     \\
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \\
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(f, c, ...)                                                         \\
  (([&](decltype((c)) cccc) {                                                  \\
    return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__);               \\
  })(c))
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x))
    xmax = x;
  return true;
  return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin))
    xmin = x;
  return true;
  return false;
}
template <typename Val> bool bitcheck(Val bit, size_t idx) {
  return bit & (1 << idx);
}
struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;
  ProconInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} PROCON_INIT;

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  FOR(i, 0, n) cin >> a[i], a[i]--;

  vector<ll> time(n, -1);
  vector<ll> seq = {0};
  size_t s;
  time[0] = 0;
  while (true) {
    size_t x = a[seq.back()];
    if (time[x] != -1) {
      s = time[x];
      break;
    }
    time[x] = time[seq.back()] + 1;
    seq.push_back(x);
  }

  ll ret;
  if (k < s) {
    ret = seq[k] + 1;
  } else {
    ret = seq[(k - s) % (seq.size() - s) + s] + 1;
  }

  cout << ret << endl;
}

signed main() {
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_CAT_I(x, y) x##y

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
using ll = long long;
constexpr i64 INF = 1010000000000000017LL;
constexpr i64 MOD = 1000000007LL;
constexpr f64 EPS = 1e-12;
constexpr f64 PI = 3.14159265358979323846;
#define FOR(i, start, end)                                                     \\
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \\
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(f, c, ...)                                                         \\
  (([&](decltype((c)) cccc) {                                                  \\
    return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__);               \\
  })(c))
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x))
    xmax = x;
  return true;
  return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin))
    xmin = x;
  return true;
  return false;
}
template <typename Val> bool bitcheck(Val bit, size_t idx) {
  return bit & (1 << idx);
}
struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;
  ProconInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} PROCON_INIT;
using Graph = vector<vector<ll>>;
vector<bool> seen;
ll dfs(const Graph &G, ll v, ll &count) {
  seen[v] = true;
  ll idx = 0;
  for (auto next : G[v]) {
    if (seen[next]) {
      return next;
    }
    if (!seen[next]) {
      idx = dfs(G, next, ++count);
    }
  }
  return idx;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  Graph G(n);
  FOR(i, 0, n) {
    ll tmp = 0;
    cin >> tmp;
    --tmp;
    G[i].push_back(tmp);
  }

  seen.assign(n, false);
  ll count = 1;
  ll idx = 0;
  idx = dfs(G, 0, count);

  ll count2 = count;
  if (idx != 0) {
    FOR(i, 0, count) {
      if (G[i][0] != idx) {
        --count;
      }
    }
  }

  k -= count2 - count;
  ll mod = k % count;
  ll ret = idx;
  FOR(i, 0, mod) { ret = G[ret][0]; }
  cout << ++ret << endl;
}

signed main() {
  solve();
  return 0;
}