Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
using namespace std;
using ll = long long;

/*struct mint {
    ll x; // typedef long long ll;
    mint(ll x = 0) :x((x% mod + mod) % mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; }
    mint operator+(const mint a) const { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= a; }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return *this *= a.inv(); }
    mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream & is, const mint & a) { return is >> a.x; }
ostream& operator<<(ostream & os, const mint & a) { return os << a.x; }*/

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> exp(n, 0);
  vector<int> rode(0);
  rep(i, n) cin >> a[i];

  int cu_to = 0;
  int kuri = 0;
  while (1) {
    exp[cu_to]++;
    if (exp[cu_to] == 2) {
      while (1) {
        if (rode[0] != cu_to) {
          k--;
          rode.erase(rode.begin() + 0);
        } else {
          break;
        }
      }
      break;
    }
    rode.emplace_back(cu_to);
    cu_to = a[cu_to] - 1;
    kuri++;
    if (kuri == k) {
      cout << cu_to + 1 << endl;
      return 0;
    }
  }

  int rem = k % rode.size();

  cout << rode[rem] + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
using namespace std;
using ll = long long;

/*struct mint {
    ll x; // typedef long long ll;
    mint(ll x = 0) :x((x% mod + mod) % mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; }
    mint operator+(const mint a) const { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= a; }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return *this *= a.inv(); }
    mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream & is, const mint & a) { return is >> a.x; }
ostream& operator<<(ostream & os, const mint & a) { return os << a.x; }*/

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> exp(n, 0);
  vector<ll> rode(0);
  rep(i, n) cin >> a[i];

  int cu_to = 0;
  int kuri = 0;
  while (1) {
    exp[cu_to]++;
    if (exp[cu_to] == 2) {
      while (1) {
        if (rode[0] != cu_to) {
          k--;
          rode.erase(rode.begin() + 0);
        } else {
          break;
        }
      }
      break;
    }
    rode.emplace_back(cu_to);
    cu_to = a[cu_to] - 1;
    kuri++;
    if (kuri == k) {
      cout << cu_to + 1 << endl;
      return 0;
    }
  }

  int rem = k % rode.size();

  cout << rode[rem] + 1 << endl;
  return 0;
}