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

#include <bits/stdc++.h>
#if ONLINE_JUDGE
#define DEBUG false
#else
#define DEBUG true
#endif
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)
#define rrep(i, n) rREP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define SORT(x) std::sort(all(x))
#define REV(x) std::reverse(all(x))
#define UNIQUE(x) x.erase(std::unique(all(x)), x.end());
#define MAX(x) *std::max_element(all(x))
#define MIN(x) *std::min_element(all(x))
#define LOWitr(x, n) std::lower_bound(all(x), n)
#define UPRitr(x, n) std::upper_bound(all(x), n)
#define cii(x)                                                                 \\
  int x;                                                                       \\
  std::cin >> x
#define cill(x)                                                                \\
  signed long long int x;                                                      \\
  std::cin >> x
#define cis(x)                                                                 \\
  std::string x;                                                               \\
  std::cin >> x
#define co(x) std::cout << x << "\\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define truecheck assert
#define dump(x)                                                                \\
  if (DEBUG)                                                                   \\
  std::cout << #x << " : " << (x) << "\\n"
template <class T> using V = std::vector<T>;
template <class T> using VV = std::vector<std::vector<T>>;
template <class T> using Q = std::queue<T>;
template <class T> using PQ = std::priority_queue<T>;
template <class T, class U> using P = std::pair<T, U>;
template <class T, class U> using M = std::map<T, U>;
template <class T, class U> using UM = std::unordered_map<T, U>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void vcin(T &v) {
  for (auto &&i : v)
    std::cin >> i;
}
template <class T> inline void dumpv(const T &v, std::ostream &os = std::cout) {
  if (DEBUG) {
    os << "{";
    std::for_each(all(v), [&os](typename T::value_type p) { os << p << ','; });
    os << "}\\n";
  }
}
using std::cin;
using std::cout;
using std::next_permutation;
using std::string;
using LL = signed long long int;
const int INF = INT_MAX, MOD = 1e9 + 7;
const LL LLINF = LLONG_MAX;
const double PI = M_PI;

void Main() {
  cill(n);
  cill(k);
  V<LL> v(n), route;
  V<bool> boo(n, false);
  for (auto &&i : v) {
    std::cin >> i;
    i--;
  }
  route.pb(0);
  boo[0] = true;
  LL breaked = -1;
  rep1(i, n) {
    LL next = v[route[i - 1]];
    if (boo[next]) {
      breaked = next;
      break;
    }
    boo[next] = true;
    route.pb(next);
  }
  LL roop = route.size(), offset = 0;
  rep(i, route.size()) {
    if (route[i] == breaked) {
      offset = i;
      break;
    }
  }
  if (breaked == 0)
    offset = 0;
  roop -= offset;
  if (offset < k) {
    co(route[offset + (k - offset) % roop] + 1);
  } else {
    co(route[k] + 1);
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#if ONLINE_JUDGE
#define DEBUG false
#else
#define DEBUG true
#endif
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)
#define rrep(i, n) rREP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define SORT(x) std::sort(all(x))
#define REV(x) std::reverse(all(x))
#define UNIQUE(x) x.erase(std::unique(all(x)), x.end());
#define MAX(x) *std::max_element(all(x))
#define MIN(x) *std::min_element(all(x))
#define LOWitr(x, n) std::lower_bound(all(x), n)
#define UPRitr(x, n) std::upper_bound(all(x), n)
#define cii(x)                                                                 \\
  int x;                                                                       \\
  std::cin >> x
#define cill(x)                                                                \\
  signed long long int x;                                                      \\
  std::cin >> x
#define cis(x)                                                                 \\
  std::string x;                                                               \\
  std::cin >> x
#define co(x) std::cout << x << "\\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define truecheck assert
#define dump(x)                                                                \\
  if (DEBUG)                                                                   \\
  std::cout << #x << " : " << (x) << "\\n"
template <class T> using V = std::vector<T>;
template <class T> using VV = std::vector<std::vector<T>>;
template <class T> using Q = std::queue<T>;
template <class T> using PQ = std::priority_queue<T>;
template <class T, class U> using P = std::pair<T, U>;
template <class T, class U> using M = std::map<T, U>;
template <class T, class U> using UM = std::unordered_map<T, U>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void vcin(T &v) {
  for (auto &&i : v)
    std::cin >> i;
}
template <class T> inline void dumpv(const T &v, std::ostream &os = std::cout) {
  if (DEBUG) {
    os << "{";
    std::for_each(all(v), [&os](typename T::value_type p) { os << p << ','; });
    os << "}\\n";
  }
}
using std::cin;
using std::cout;
using std::next_permutation;
using std::string;
using LL = signed long long int;
const int INF = INT_MAX, MOD = 1e9 + 7;
const LL LLINF = LLONG_MAX;
const double PI = M_PI;

void Main() {
  cill(n);
  cill(k);
  V<LL> v(n), route;
  V<bool> boo(n, false);
  vcin(v);
  route.pb(0);
  boo[0] = true;
  LL breaked = -1;
  rep1(i, n) {
    LL next = v[route[i - 1]] - 1;
    if (boo[next]) {
      breaked = next;
      break;
    }
    boo[next] = true;
    route.pb(next);
  }
  LL roop = route.size(), offset = 0;
  rep(i, route.size()) {
    if (route[i] == breaked) {
      offset = i;
      break;
    }
  }
  if (roop - offset != 1)
    roop -= offset;
  co(route[offset + (k - offset) % roop] + 1);
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}