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

// INCLUDES
#include <assert.h>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// DEFINES
#define MAX 1000001
#define pii pair<int, int>
#define F first
#define S second
#define all(s) begin(s), end(s)
#define MIN3POS(a, b, c)                                                       \\
  (a) = < (b) ? ((a) = < (c) ? 1 : 3) : ((b) = < (c) ? 2 : 3)
#define MAX3POS(a, b, c)                                                       \\
  (a) >= (b) ? ((a) >= (c) ? 1 : 3) : ((b) >= (c) ? 2 : 3)
#define endl '\\n'
#define int long long
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define FOR(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
// NAMESPACES
using namespace __gnu_pbds;
using namespace std;
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
// HELPERS

vector<int> powers2(32, 0);
void initialize() {
  for (int i = 1; i < powers2.size(); i++)
    powers2[i] = powers2[i - 1] * 2;
}
int32_t main() {
  IOS int n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  FOR(i, 1, n + 1)
  cin >> v[i];
  int pos = 1;
  int count = 0;
  vector<int> p(n + 1, 0), visited(n + 1, 0);
  int cycle = 0, rot = 0;
  while (visited[pos] != 1) {
    p[pos] = count;
    visited[pos] = 1;
    if (visited[v[pos]]) {
      cycle = count - p[v[pos]] + 1, rot = v[pos];
      break;
    }
    pos = v[pos];
    count++;
  };
  int dest = 1;
  if (k <= p[rot]) {
    FOR(i, 0, k) dest = v[dest];
  } else {
    k -= p[rot];
    dest = rot;
    FOR(i, 0, k % cycle) dest = v[dest];
  }
  cout << dest;
  return 0;
}
// Use emplace back in place of pushback
// Use tie function , for object destructuring

Error: WA

Fixed submission:

// INCLUDES
#include <assert.h>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// DEFINES
#define MAX 1000001
#define pii pair<int, int>
#define F first
#define S second
#define all(s) begin(s), end(s)
#define MIN3POS(a, b, c)                                                       \\
  (a) = < (b) ? ((a) = < (c) ? 1 : 3) : ((b) = < (c) ? 2 : 3)
#define MAX3POS(a, b, c)                                                       \\
  (a) >= (b) ? ((a) >= (c) ? 1 : 3) : ((b) >= (c) ? 2 : 3)
#define endl '\\n'
#define int long long
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define FOR(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
// NAMESPACES
using namespace __gnu_pbds;
using namespace std;
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
// HELPERS

vector<int> powers2(32, 0);
void initialize() {
  for (int i = 1; i < powers2.size(); i++)
    powers2[i] = powers2[i - 1] * 2;
}
int32_t main() {
  IOS int n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  FOR(i, 1, n + 1)
  cin >> v[i];
  int pos = 1;
  int count = 0;
  vector<int> p(n + 1, 0), visited(n + 1, 0);
  int cycle = 0, rot = 0;
  while (visited[pos] != 1) {
    p[pos] = count;
    if (visited[v[pos]])
      cycle = count - p[v[pos]] + 1, rot = v[pos];
    visited[pos] = 1;
    pos = v[pos];
    count++;
  };
  int dest = 1;
  if (k <= p[rot]) {
    FOR(i, 0, k) dest = v[dest];
  } else {
    k -= p[rot];
    dest = rot;
    FOR(i, 0, k % cycle) dest = v[dest];
  }
  cout << dest;
  return 0;
}
// Use emplace back in place of pushback
// Use tie function , for object destructuring