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

#include <bits/stdc++.h>
// __builtin_ctz : no of trailing zeros
#define int long long
#define ll long long
#define pp pair<int, int>
#define mod 1000000007
#define trace(x) cerr << #x << ": " << x << " " << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define sa(a, n)                                                               \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define scan()                                                                 \\
  int n;                                                                       \\
  cin >> n;                                                                    \\
  int a[n];                                                                    \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << a[i] << ' '
using namespace std;
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
void _(int &a, int b) {
  a = (a % mod + b % mod) % mod;
  a = (a + mod) % mod;
}
ll cl(ll a, ll b) { return (a / b) + (!(!(a % b))); }
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  ll b[n];
  bool vis[n];
  memset(vis, 0, sizeof(vis));
  ll d = 0;
  ll cnt = 1;
  while (!vis[d] and k) {
    b[d] = cnt;
    cnt++;
    vis[d] = 1;
    d = a[d];
    k--;
  }
  // trace(d);
  if (k == 0)
    cout << d + 1;
  else {

    ll g = cnt - b[d];
    k %= g;
    while (k--)
      d = a[d];
    cout << d + 1;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// __builtin_ctz : no of trailing zeros
#define int long long
#define ll long long
#define pp pair<int, int>
#define mod 1000000007
#define trace(x) cerr << #x << ": " << x << " " << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define sa(a, n)                                                               \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define scan()                                                                 \\
  int n;                                                                       \\
  cin >> n;                                                                    \\
  int a[n];                                                                    \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << a[i] << ' '
using namespace std;
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
void _(int &a, int b) {
  a = (a % mod + b % mod) % mod;
  a = (a + mod) % mod;
}
ll cl(ll a, ll b) { return (a / b) + (!(!(a % b))); }
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  ll b[n];
  bool vis[n];
  memset(vis, 0, sizeof(vis));
  ll d = 0;
  ll cnt = 1;
  while (!vis[d] and k--) {
    b[d] = cnt;
    cnt++;
    vis[d] = 1;
    d = a[d];
  }
  // trace(d);
  if (k == 0)
    cout << d + 1;
  else {

    ll g = cnt - b[d];
    k %= g;
    while (k--)
      d = a[d];
    cout << d + 1;
  }
}