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

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = INT_MAX / 2;
const ll inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  /*------------------------------*/

  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> visited(n, 0);

  vector<int> start;
  int next = 0;
  rep(i, n + 1) {
    if (visited[next] > 0) {
      start.push_back(next);
      start.push_back(i);
      break;
    }
    visited[next]++;
    next = a[next];
  }

  next = start[0];
  rep(i, n) {
    if (a[next] == start[0]) {
      start.push_back(i + 1);
      break;
    }
    next = a[next];
  }

  if (k < start[1]) {
    next = 0;
    rep(i, k) { next = a[next]; }
    out(next + 1);
  } else {
    k -= start[1];
    k = k % start[2];
    next = start[0];
    rep(i, k) { next = a[next]; }
    out(next + 1);
  }

  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = INT_MAX / 2;
const ll inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  /*------------------------------*/

  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> visited(n, 0);

  vector<int> start;
  int next = 0;
  rep(i, n + 1) {
    if (visited[next] > 0) {
      start.push_back(next);
      start.push_back(i);
      break;
    }
    visited[next]++;
    next = a[next];
  }

  next = start[0];
  rep(i, n) {
    if (a[next] == start[0]) {
      start.push_back(i);
      break;
    }
    next = a[next];
  }

  if (k < start[1]) {
    next = 0;
    rep(i, k) { next = a[next]; }
    out(next + 1);
  } else {
    k -= start[1];
    k = k % start[2];
    next = start[0];
    rep(i, k) { next = a[next]; }
    out(next + 1);
  }

  return 0;
}