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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef unordered_set<int> si;
typedef pair<int, int> pp;
typedef vector<pp> vpp;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define all(e) (e).begin(), (e).end()
#define pb push_back

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> void printV(vector<T> A) {
  if (A.empty())
    cerr << "EMTPY\\n";
  else {
    cerr << "[";
    for (int i = 0; i < A.size() - 1; ++i) {
      cerr << A[i] << ", ";
    }
    if (A.size())
      cerr << A.back() << "]\\n";
  }
}

template <typename T> void printVV(vector<vector<T>> A) {
  cerr << "[";
  for (auto v : A) {
    printV<T>(v);
  }
  cerr << "]";
}

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  vi arr(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
  }

  vector<ll> in(n + 1);

  ll cur = 1;
  ll cycle = 0;
  ll timer = 0;
  while (k) {
    if (in[cur] != 0) {
      cycle = 1 + timer - in[cur];
      break;
    }
    in[cur] = ++timer;
    cur = arr[cur];
    k -= 1;
  }

  if (k) {
    k %= cycle;
    for (int i = 0; i < k; ++i) {
      cur = arr[cur];
    }
  }
  cout << cur << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef unordered_set<int> si;
typedef pair<int, int> pp;
typedef vector<pp> vpp;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define all(e) (e).begin(), (e).end()
#define pb push_back

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> void printV(vector<T> A) {
  if (A.empty())
    cerr << "EMTPY\\n";
  else {
    cerr << "[";
    for (int i = 0; i < A.size() - 1; ++i) {
      cerr << A[i] << ", ";
    }
    if (A.size())
      cerr << A.back() << "]\\n";
  }
}

template <typename T> void printVV(vector<vector<T>> A) {
  cerr << "[";
  for (auto v : A) {
    printV<T>(v);
  }
  cerr << "]";
}

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  vi arr(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
  }

  vector<int> in(n + 1);

  int cur = 1;
  int cycle = 0;
  int timer = 0;
  while (k) {
    if (in[cur] != 0) {
      cycle = 1 + timer - in[cur];
      break;
    }
    in[cur] = ++timer;
    cur = arr[cur];
    k -= 1;
  }

  k %= cycle;
  for (int i = 0; i < k; ++i) {
    cur = arr[cur];
  }
  cout << cur << endl;

  return 0;
}