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

#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long

const int N = 2e5 + 5;

int n, k;
int a[N];
bool vis[N];

int32_t main() {
  IOS;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int take = min(k, 200000LL);
  k -= take;
  int cur = 1;
  while (take > 0) {
    cur = a[cur];
    take--;
  }
  if (k == 0) {
    cout << cur;
    return 0;
  }
  int len = 0;
  while (!vis[cur]) {
    vis[cur] = 1;
    len++;
    cur = a[cur];
  }
  k %= len;
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  cout << cur;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
#define trace(x) cerr << #x << ": " << x << " " << endl
#define tarce(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 trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << 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 pb push_back
#define inf 2e18
#define scan()                                                                 \\
  int n;                                                                       \\
  cin >> n;                                                                    \\
  int a[n + 1];                                                                \\
  for (int i = 1; i <= n; i++)                                                 \\
  cin >> a[i]
#define scan2()                                                                \\
  int n, k;                                                                    \\
  cin >> n >> k;                                                               \\
  int a[n + 1];                                                                \\
  for (int i = 1; i <= n; i++)                                                 \\
  cin >> a[i]
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << a[i] << ' '
#define mem(a, v) memset(a, v, sizeof(a))
#define pii pair<int, int>
#define mkp make_pair
using namespace std;
#define N 200005

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;
  int i, a[n];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int curr = 1;

  int cycle;
  int v[n + 10];
  mem(v, 0);
  int ans[n + 10];
  int count = 0;
  int start;
  while (v[curr] == 0) {
    count++;
    ans[count] = curr;
    v[curr] = 1;
    curr = a[curr];
    if (v[curr] == 1) {
      start = curr;
      break;
    }
  }

  curr = 1;
  int c = 0;
  while (curr != start) {
    curr = a[curr];
    c++;
  }
  curr = a[start];
  vector<int> va;
  va.pb(start);
  while (curr != start) {
    va.pb(curr);
    curr = a[curr];
  }
  int m = va.size();

  //  trace2(c,m);
  //   for(auto i :va)trace(i);
  if (k <= c) {
    curr = 1;
    while (k--) {
      curr = a[curr];
    }
    cout << curr;
  } else {
    int r = k - c;
    int y = r % m;
    k = y + c;
    curr = 1;
    while (k--) {
      curr = a[curr];
    }
    cout << curr;
  }
  // cycle=count;
}