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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <vector>
#define all(container) (container).begin(), (container).end()
#define rall(container) (container).rbegin(), (container).rend()
#define sz(container) (int)container.size()
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<vector<int>> vvi;
typedef vector<bool> vb;
typedef vector<long long> vll;

#ifdef LOCAL_DEFINE
#include "DEBUG.hpp"
#else
#define debug(...) 0
#endif

void read(int &a) { scanf("%d", &a); }
void read(ll &a) { scanf("%lld", &a); }
void read(char *a, string &s) {
  scanf("%s", a);
  s = string(a);
}
void read(float &a) { scanf("%f", &a); }
void read(double &a) { scanf("%lf", &a); }

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
vi teleport, seq, visited;

void solve() {
  int n;
  read(n);
  ll k;
  read(k);
  teleport.resize(n + 1);
  for (int i = 1; i <= n; i++)
    read(teleport[i]);
  visited.resize(n + 1, -1);
  visited[1] = 0;
  seq.push_back(1);
  int curr = teleport[1];
  while (visited[curr] == -1) {
    seq.push_back(curr);
    visited[curr] = sz(seq) - 1;
    curr = teleport[curr];
  }
  int cycle_start = visited[curr];
  debug(cycle_start, seq);
  if (k <= cycle_start)
    printf("%d\\n", seq[k]);
  else {
    k -= cycle_start;
    int ans = seq[cycle_start + k % (ll)(sz(seq) - cycle_start)];
    printf("%d\\n", ans);
  }
}

int main(void) {
  int T = 1;
  while (T--)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_set>
#include <vector>
#define forn(x, n) for (int x = 0; x < n; x++)
#define fore(x, a, b, c) for (int x = a; x <= b; x += c)
#define forr(x, a) for (int x = a; x >= 0; x--)
#define all(container) (container).begin(), (container).end()
#define rall(container) (container).rbegin(), (container).rend()
#define sz(container) (int)container.size()
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<vector<int>> vvi;
typedef vector<bool> vb;
typedef vector<long long> vll;

#ifdef LOCAL_DEFINE
#include "DEBUG.hpp"
#else
#define debug(...) 0
#endif

void read(int &a) { scanf("%d", &a); }
void read(ll &a) { scanf("%lld", &a); }
void read(char *a, string &s) {
  scanf("%s", a);
  s = string(a);
}
void read(float &a) { scanf("%f", &a); }
void read(double &a) { scanf("%lf", &a); }

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
vi seq, A, last;

void solve() {
  ll n, k;
  read(n), read(k);
  A.resize(n + 1);
  last.resize(n + 1, -1);
  forn(i, n) read(A[i + 1]);
  int curr = 1, cycle_start = 0;
  while (last[curr] == -1) {
    seq.push_back(curr);
    last[curr] = sz(seq) - 1;
    curr = A[curr];
  }
  debug(seq);
  cycle_start = last[curr];
  debug(cycle_start);
  if (!cycle_start) {
    vi cycle(seq.begin() + 1, seq.end());
    cycle.push_back(1);
    printf("%d\\n", cycle[k % n]);
  } else {
    vi cycle(seq.begin() + cycle_start, seq.end());
    if (k < cycle_start)
      printf("%d\\n", seq[k]);
    else {
      k -= cycle_start;
      printf("%d\\n", cycle[k % (n - cycle_start - 1)]);
    }
  }
}

int main(void) {
  int T = 1;
  while (T--)
    solve();
  return 0;
}