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

#include <bits/stdc++.h>
typedef long long int lli;
typedef unsigned long long int ulli;
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vlli vector<lli>
#define vulli vector<ulli>
#define vchar vector<char>
#define vstr vector<string>
#define vb vector<bool>
#define fst first
#define sec second
#define srt(a) sort(a.begin(), a.end())
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define vplli vector<pair<lli, lli>>
#define vpulli vector<pair<ulli, ulli>>
#define vvi vector<vector<int>>
#define vvpii vector<vector<pair<int, int>>>
#define vvb vector<vector<bool>>
#define fr(i, n) for (int i = 0; i < (n); i++)
#define frbig(i, n) for (lli i = 0; i < (n); i++)
#define fra(i, a, n) for (lli i = a; i < (n); i++)
#define run(c, s) for (auto c : s)
#define read(n)                                                                \\
  int n;                                                                       \\
  cin >> n;
#define read2(x, y)                                                            \\
  int x, y;                                                                    \\
  cin >> x >> y;
#define read2ll(x, y)                                                          \\
  long long int x, y;                                                          \\
  cin >> x >> y;
#define readll(n)                                                              \\
  lli n;                                                                       \\
  cin >> n;
#define watch(x) cout << (#x) << " is " << (x) << endl
#define print_array(x) fr(i, x.size()) cout << x[i] << " ";
using namespace std;
const lli MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  lli k;
  cin >> n >> k;
  vi a(n);
  fr(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int x = 0;
  vi s;
  vb used(n, false);
  while (!used[x]) {
    used[x] = true;
    s.pb(x);
    x = a[x];
  }
  int f = 0;
  while (s[f] != x) {
    f++;
  }
  if (k > f) {
    k -= f;
    cout << s[f + k % (s.size() - f)] + 1;
  } else
    cout << s[k] + 1;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int lli;
typedef unsigned long long int ulli;
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vlli vector<lli>
#define vulli vector<ulli>
#define vchar vector<char>
#define vstr vector<string>
#define vb vector<bool>
#define fst first
#define sec second
#define srt(a) sort(a.begin(), a.end())
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define vplli vector<pair<lli, lli>>
#define vpulli vector<pair<ulli, ulli>>
#define vvi vector<vector<int>>
#define vvpii vector<vector<pair<int, int>>>
#define vvb vector<vector<bool>>
#define fr(i, n) for (int i = 0; i < (n); i++)
#define frbig(i, n) for (lli i = 0; i < (n); i++)
#define fra(i, a, n) for (lli i = a; i < (n); i++)
#define run(c, s) for (auto c : s)
#define read(n)                                                                \\
  int n;                                                                       \\
  cin >> n;
#define read2(x, y)                                                            \\
  int x, y;                                                                    \\
  cin >> x >> y;
#define read2ll(x, y)                                                          \\
  long long int x, y;                                                          \\
  cin >> x >> y;
#define readll(n)                                                              \\
  lli n;                                                                       \\
  cin >> n;
#define watch(x) cout << (#x) << " is " << (x) << endl
#define print_array(x) fr(i, x.size()) cout << x[i] << " ";
using namespace std;
const lli MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vi a(n);
  fr(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int x = 0;
  while (k--) {
    x = a[x];
  }
  cout << x + 1;
  return 0;
}