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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define square(x) pow(x 2.0)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define max_queue(x) priority_queue<x>
#define min_queue(x) priority_queue<x, vector<x>, greater<x>>
#define big_sort(v)                                                            \\
  sort(all(v), [](int a, int b) { return a > b; }) // 大きい順にソート
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
#define cins(n, v)                                                             \\
  rep(i, n) { cin >> v.at(i); }
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
/*
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
*/
int main() {
  ll n, k, next_town, town = 1, i = 0, roop = 0;
  cin >> n >> k;
  vi a(n);
  vi v;
  v.push_back(1);
  cins(n, a);
  std::map<ll, ll> mp;
  mp[town] = 0;
  while (true) {
    next_town = a[town - 1];
    i++;
    if (mp.count(next_town)) {
      roop = i - mp[next_town];
      k -= i;
      k %= roop;
      cout << v[k + mp[next_town]] << endl;
      return 0;
    } else {
      mp[next_town] = i;
      town = next_town;
      v.push_back(town);
    }
    if (i == k) {
      cout << town << endl;
      return 0;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define square(x) pow(x 2.0)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define max_queue(x) priority_queue<x>
#define min_queue(x) priority_queue<x, vector<x>, greater<x>>
#define big_sort(v)                                                            \\
  sort(all(v), [](int a, int b) { return a > b; }) // 大きい順にソート
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
#define cins(n, v)                                                             \\
  rep(i, n) { cin >> v.at(i); }
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
/*
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
*/
int main() {
  ll n, k, next_town, town = 1, i = 0, roop = 0;
  cin >> n >> k;
  vi a(n);
  cins(n, a);
  std::map<ll, ll> mp;
  while (true) {
    next_town = a[town - 1];
    if (mp.count(next_town) && roop == 0) {
      roop = i - mp[next_town];
      k -= i;
      k %= roop;
      i = 0;
      town = next_town;
    } else {
      mp[next_town] = i;
      town = next_town;
    }
    i++;
    if (i == k) {
      cout << town << endl;
      return 0;
    }
  }
}