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

/*
Author : Xinyuan
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define INF 1000000007
#define mod 1005
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long int ll;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
VI tele(200005);

int f(int x) { return tele[x]; }

int main() {
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 0; i < n; i += 1) {
    int a;
    cin >> a;
    tele[i] = a - 1;
  }
  int tortoise = f(0), hare = f(f(0)); // f(x0) is the node next to x0
  while (tortoise != hare) {
    tortoise = f(tortoise);
    hare = f(f(hare));
  }
  // 2nd part: finding mu, hare and tortoise move at the same speed
  ll mu = 0;
  hare = 0;
  while (tortoise != hare) {
    tortoise = f(tortoise);
    hare = f(hare);
    mu++;
  }
  // 3rd part: finding lambda, hare moves, tortoise stays
  ll lambda = 1;
  hare = f(tortoise);
  while (tortoise != hare) {
    hare = f(hare);
    lambda++;
  }
  if (k < mu) {
    int ans = 0;
    for (int i = 0; i < (int)k; i += 1) {
      ans = tele[ans];
    }
    printf("%d", ans + 1);
  } else {
    int ans = 0;
    for (int i = 0; i < (int)mu; i += 1) {
      ans = tele[ans];
    }
    k -= mu;
    k %= lambda;
    for (int i = 0; i < k; i += 1) {
      ans = tele[ans];
    }
    printf("%d", ans + 1);
  }
  return 0;
}


Error: WA

Fixed submission:

/*
Author : Xinyuan
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define INF 1000000007
#define mod 1005
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long int ll;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
VI tele(20005);

int f(int x) { return tele[x]; }

int main() {
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 0; i < n; i += 1) {
    int a;
    cin >> a;
    tele[i] = a - 1;
  }
  // int tortoise = f(0), hare = f(f(0));    // f(x0) is the node next to x0
  // while (tortoise != hare) { tortoise = f(tortoise); hare = f(f(hare)); }
  // // 2nd part: finding mu, hare and tortoise move at the same speed
  // ll mu = 0; hare = 0;
  // while (tortoise != hare) { tortoise = f(tortoise); hare = f(hare); mu++; }
  // // 3rd part: finding lambda, hare moves, tortoise stays
  // ll lambda = 1; hare = f(tortoise);
  // while (tortoise != hare) { hare = f(hare); lambda++; }
  // if(k < mu){
  //     int ans = 0;
  //     for (int i = 0; i < (int)k; i+=1){
  //         ans = tele[ans];
  //     }
  //     printf("%d", ans + 1);
  // }else{
  //     int ans = 0;
  //     for (int i = 0; i < (int)mu; i+=1){
  //         ans = tele[ans];
  //     }
  //     k-=mu;
  //     k%=lambda;
  //     for (int i = 0; i < k; i+=1){
  //         ans = tele[ans];
  //     }
  //     printf("%d", ans + 1);
  // }
  return 0;
}