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

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

typedef long long ll;
#define ar array
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define fork(i, n) for (ll i = 0; i < (n); i++)
#define forn for (ll i = 0; i < (n); i++)
#define fori(i, k, n) for (ll i = k; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define en '\\n'
#define FILL(x, v) memset(x, v, sizeof(x))
#define pb push_back
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
template <typename T> void print(vector<T> &a) {
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << ' ';
  cout << en;
}
template <typename T> void print(deque<T> &a) {
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << ' ';
  cout << en;
}
template <typename T> void print(vector<vector<T>> &a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++)
      cout << a[i][j] << ' ';
    cout << en;
  }
}
int a[200005];
int flag[200005];

void solve() {
  // teleport k times
  // 1<=i<=n
  // telep town i -> a[i]
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int cur = 1, cnt = 0;
  while (k--) {
    cur = a[cur];
    cnt++;
    if (flag[cur])
      k %= (cnt - flag[cur]);
    else
      flag[cur] = cnt;
  }
  cout << cur << en;
}

int main() {
  fast;
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
#define ar array
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define fork(i, n) for (ll i = 0; i < (n); i++)
#define forn for (ll i = 0; i < (n); i++)
#define fori(i, k, n) for (ll i = k; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define en '\\n'
#define FILL(x, v) memset(x, v, sizeof(x))
#define pb push_back
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
template <typename T> void print(vector<T> &a) {
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << ' ';
  cout << en;
}
template <typename T> void print(deque<T> &a) {
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << ' ';
  cout << en;
}
template <typename T> void print(vector<vector<T>> &a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++)
      cout << a[i][j] << ' ';
    cout << en;
  }
}

void solve() {
  // teleport k times
  // 1<=i<=n
  // telep town i -> a[i]
  ll n, k;
  cin >> n >> k;
  vi a(n);
  for (auto &it : a) {
    cin >> it;
    it--;
  }
  vi cycle;
  int cur = 0, cnt = 0;
  cycle.push_back(cur);
  bitset<(int)2e5> m;
  bool flag = 0;
  fork(i, k) {
    m[cur] = 1;
    cur = a[cur];
    cnt++;
    if (m[cur]) {
      flag = 1;
      break;
    }
    cycle.push_back(cur);
  }
  cycle = vi(cycle.begin() + (cur ? cur + 1 : 0), cycle.end());
  // print(cycle);
  // cout << k << " " << cur << " " << cnt << en;
  cout << cycle[(k - cur) % cnt] + 1 << en;
  // cout << k << " " << cnt << " " << cur << en;
}

int main() {
  fast;
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}