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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define sqr(x) ((x) * (x))
#define mp make_pair
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i, 1, n)
#define REP(i, n) FOR(i, 0, (int)(n)-1)
#define all(x) (x).begin(), (x).end()
#define fst first
#define snd second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef tuple<int, int, int> iii;

string to_string(string s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

void fastIO() {
  cin.sync_with_stdio(false);
  cin.tie(0);
}

template <typename T> vector<T> make_unique(vector<T> v) {
  sort(all(v));
  return v.resize(unique(all(v)) - v.begin());
}

int nxt() {
  int x;
  cin >> x;
  return x;
}

int XMOV[4] = {0, 0, 1, -1};
int YMOV[4] = {1, -1, 0, 0};

// #############################
const int MAXN = 1000005;

int main() {
  ll n, k;
  int t = 1;
  cin >> n >> k;
  vector<int> a(n + 1, 0);
  RE(i, n)
  cin >> a[i];
  // cout << n << " " << k << " " << to_string(a) << endl;
  map<int, int> tm;

  ll cur = 1, lambda, theta;
  bool flag = false;
  while (t < k) {
    if (tm.count(cur)) {
      lambda = cur;
      theta = t - tm[cur];
      flag = true;
      break;
    }

    // cout << to_string(tm) << endl;

    tm[cur] = t++;
    cur = a[cur];
  }

  if (!flag) {
    cout << a[cur] << endl;
    return 0;
  }

  // cout << "t: " << t << endl;
  int steps = ((k - t + 1) % theta);
  cur = lambda;
  for (int i = 0; i < steps; i++) {
    // cout << cur << endl;
    cur = a[cur];
  }
  // cout << lambda << " " << theta << " " << steps << endl;
  cout << cur << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define sqr(x) ((x) * (x))
#define mp make_pair
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i, 1, n)
#define REP(i, n) FOR(i, 0, (int)(n)-1)
#define all(x) (x).begin(), (x).end()
#define fst first
#define snd second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef tuple<int, int, int> iii;

string to_string(string s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

void fastIO() {
  cin.sync_with_stdio(false);
  cin.tie(0);
}

template <typename T> vector<T> make_unique(vector<T> v) {
  sort(all(v));
  return v.resize(unique(all(v)) - v.begin());
}

int nxt() {
  int x;
  cin >> x;
  return x;
}

int XMOV[4] = {0, 0, 1, -1};
int YMOV[4] = {1, -1, 0, 0};

// #############################
const int MAXN = 1000005;

int main() {
  ll n, k;
  int t = 1;
  cin >> n >> k;
  vector<int> a(n + 1, 0);
  RE(i, n)
  cin >> a[i];
  // cout << n << " " << k << " " << to_string(a) << endl;
  map<int, int> tm;

  int cur = 1, lambda, theta;
  bool flag = false;
  while (t < k) {
    if (tm.count(cur)) {
      lambda = cur;
      theta = t - tm[cur] - 1;
      flag = true;
      break;
    }

    // cout << to_string(tm) << endl;

    tm[cur] = t++;
    cur = a[cur];
  }

  if (!flag) {
    cout << a[t] << endl;
    return 0;
  }

  // cout << "t: " << t << endl;
  int steps = lambda + ((k - t) % theta);
  cur = lambda;
  for (int i = 0; i < steps; i++)
    cur = a[cur];
  // cout << lambda << " " << theta << " " << steps << endl;
  cout << cur << endl;
  return 0;
}