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

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli d[200100];
lli b[200100];

int main() {

  lli n, k;
  cin >> n >> k;

  REP(i, 0, n) {
    d[i] = -INF;
    b[i] = -INF;
  }

  vector<lli> a(n);
  REP(i, 0, n) {
    lli tmp;
    cin >> tmp;
    tmp--;
    a[i] = tmp;
  }

  lli cnt = 0;
  lli now = 0;
  if (a[0] == 0) {
    cout << 1 << endl;
    return 0;
  }
  lli breakIndex, breakCnt;
  while (1) {
    LOG2(now, cnt);
    if (b[now] != -INF) {
      breakCnt = cnt;
      breakIndex = now;
      break;
    }
    d[cnt] = now;
    b[now] = cnt;
    now = a[now];
    cnt++;
  }

  LOG(breakCnt);
  lli loopCnt = breakCnt - b[breakIndex];
  LOG(loopCnt);

  if (k <= b[breakIndex]) {
    cout << d[k] + 1 << endl;
  } else {
    cout << d[(k - b[breakIndex]) % loopCnt + b[breakIndex]] + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli d[100100];
lli b[100100];

int main() {

  lli n, k;
  cin >> n >> k;

  REP(i, 0, n) {
    d[i] = -INF;
    b[i] = -INF;
  }

  vector<lli> a(n);
  REP(i, 0, n) {
    lli tmp;
    cin >> tmp;
    tmp--;
    a[i] = tmp;
  }

  lli cnt = 0;
  lli now = 0;
  if (a[0] == 0) {
    cout << 1 << endl;
    return 0;
  }
  lli breakIndex, breakCnt;
  while (1) {
    LOG2(now, cnt);
    if (b[now] != -INF) {
      breakCnt = cnt;
      breakIndex = now;
      break;
    }
    d[cnt] = now;
    b[now] = cnt;
    now = a[now];
    cnt++;
  }

  LOG(breakCnt);
  lli loopCnt = breakCnt - b[breakIndex];
  LOG(loopCnt);

  cout << d[(k - b[breakIndex]) % loopCnt + b[breakIndex]] + 1 << endl;

  return 0;
}