Problem p02684 (C++)

Buggy submission (status: Runtime Error):
// D_Teleporter.cpp
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define mp make_pair
#define pb push_back
#define ppi pair<ll, ll>
#define fi first
#define si second
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F(a, n)                                                                \
  for (int i = 0; i < n; i++) {                                                \
    cin >> a[i];                                                               \
  }
#define F1(a, n)                                                               \
  for (int i = 1; i <= n; i++) {                                               \
    cin >> a[i];                                                               \
  }
#define P(a, n)                                                                \
  for (int i = 0; i < n; i++) {                                                \
    cout << a[i] << ' ';                                                       \
  }                                                                            \
  cout << endl;
#define P1(a, n)                                                               \
  for (int i = 1; i <= n; i++) {                                               \
    cout << a[i] << ' ';                                                       \
  }                                                                            \
  cout << endl;
#define NF(a, n, m)                                                            \
  for (int i = 0; i < n; i++) {                                                \
    for (int j = 0; j < m; j++) {                                              \
      cin >> a[i][j];                                                          \
    }                                                                          \
  }
#define NF1(a, n, m)                                                           \
  for (int i = 1; i <= n; i++) {                                               \
    for (int j = 1; j <= m; j++) {                                             \
      cin >> a[i][j];                                                          \
    }                                                                          \
  }
#define PNF(a, n, m)                                                           \
  for (int i = 0; i < n; i++) {                                                \
    for (int j = 0; j < m; j++) {                                              \
      cout << a[i][j] << ' ';                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }                                                                            \
  cout << endl;
#define PNF1(a, n, m)                                                          \
  for (int i = 1; i <= n; i++) {                                               \
    for (int j = 1; j <= m; j++) {                                             \
      cout << a[i][j] << ' ';                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }                                                                            \
  cout << endl;
#define AS 200005
#define mod 1000000007

int main() {

  fastIO

#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, k;

  cin >> n >> k;

  int a[n];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  vector<ll> Next(AS);
  unordered_map<ll, ll> m;

  m[0] = 0;

  int ith = 1;
  int current = 0;
  int start, end;

  for (int i = 0; i <= n; i++) {

    Next[ith] = a[current];

    if (m.count(Next[ith])) {

      start = m[Next[ith]];
      end = ith;
      break;
    }

    else {
      m[Next[ith]] = ith;
    }
    ith++;
    current = a[current];
  }

  if (k < end) {
    cout << Next[k] + 1 << endl;
  }

  else {
    ll len = end - start;
    k -= start;
    k = k % len;
    cout << Next[start + k] + 1 << endl;
  }
  return 0;
}


Fixed submission:
// D_Teleporter.cpp
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define mp make_pair
#define pb push_back
#define ppi pair<ll, ll>
#define fi first
#define si second
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F(a, n)                                                                \
  for (int i = 0; i < n; i++) {                                                \
    cin >> a[i];                                                               \
  }
#define F1(a, n)                                                               \
  for (int i = 1; i <= n; i++) {                                               \
    cin >> a[i];                                                               \
  }
#define P(a, n)                                                                \
  for (int i = 0; i < n; i++) {                                                \
    cout << a[i] << ' ';                                                       \
  }                                                                            \
  cout << endl;
#define P1(a, n)                                                               \
  for (int i = 1; i <= n; i++) {                                               \
    cout << a[i] << ' ';                                                       \
  }                                                                            \
  cout << endl;
#define NF(a, n, m)                                                            \
  for (int i = 0; i < n; i++) {                                                \
    for (int j = 0; j < m; j++) {                                              \
      cin >> a[i][j];                                                          \
    }                                                                          \
  }
#define NF1(a, n, m)                                                           \
  for (int i = 1; i <= n; i++) {                                               \
    for (int j = 1; j <= m; j++) {                                             \
      cin >> a[i][j];                                                          \
    }                                                                          \
  }
#define PNF(a, n, m)                                                           \
  for (int i = 0; i < n; i++) {                                                \
    for (int j = 0; j < m; j++) {                                              \
      cout << a[i][j] << ' ';                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }                                                                            \
  cout << endl;
#define PNF1(a, n, m)                                                          \
  for (int i = 1; i <= n; i++) {                                               \
    for (int j = 1; j <= m; j++) {                                             \
      cout << a[i][j] << ' ';                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }                                                                            \
  cout << endl;
#define AS 200005
#define mod 1000000007

int main() {

  fastIO

#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n, k;

  cin >> n >> k;

  int a[n];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  vector<ll> Next(AS);
  unordered_map<ll, ll> m;

  m[0] = 0;

  int ith = 1;
  int current = 0;
  int start, end;

  for (int i = 0; i <= n; i++) {

    Next[ith] = a[current];

    if (m.count(Next[ith])) {

      start = m[Next[ith]];
      end = ith;
      break;
    }

    else {
      m[Next[ith]] = ith;
    }
    ith++;
    current = a[current];
  }

  if (k < end) {
    cout << Next[k] + 1 << endl;
  }

  else {
    ll len = end - start;
    k -= start;
    k = k % len;
    cout << Next[start + k] + 1 << endl;
  }
  return 0;
}