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

/* include c++ libraries */
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

/* include c-lang libraries */
#include <climits>
#include <cmath>

/* include my libraries */
// #include <show_vec.hh>

#define DEBUG if (false)

using namespace std;

using ll = long long;
#define LINE printf("LINE : %d\\n", __LINE__);
#define loop(i, n) for (i = 0; i < n; i++)
template <class T> using vec = vector<T>;

const int MOD = 1000000007;

int main() {

  int N;
  cin >> N;
  ll K;
  cin >> K;
  vec<int> Next(N, 0), visited(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> Next[i];
    Next[i]--;
  }
  int curr = 0;
  visited[0] = 1;
  int loop_first, loop_end;
  int count = 0;
  while (true) {
    int next = Next[curr];
    if (visited[next] == 1) {
      loop_first = next;
      loop_end = curr;
      break;
    } else {
      visited[next] = 1;
      curr = next;
      count++;
    }
  }
  int count_ = 0;
  curr = 0;
  for (int i = 0; i < count; i++) {

    if (curr == loop_first) {
      break;
    } else {
      count_++;
      curr = Next[curr];
    }
  }

  int step = 0;
  if (K <= count) {
    step = K;
  } else {
    step = ((K - count_) % (count - count_ + 1)) + count_;
  }
  DEBUG printf("count : %d\\n", count);
  DEBUG printf("count_ : %d\\n", count_);
  int moved;
  curr = 0;
  DEBUG printf("step : %d\\n", step);
  for (int i = 0; i < step; i++) {
    curr = Next[curr];
  }
  cout << curr + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

/* include c++ libraries */
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

/* include c-lang libraries */
#include <climits>
#include <cmath>

/* include my libraries */
// #include <show_vec.hh>

#define DEBUG if (false)

using namespace std;

using ll = long long;
#define LINE printf("LINE : %d\\n", __LINE__);
#define loop(i, n) for (i = 0; i < n; i++)
template <class T> using vec = vector<T>;

const int MOD = 1000000007;

int main() {

  int N;
  ll K;
  cin >> N >> K;
  vec<pair<int, int>> A;

  int n = 0;
  for (int i = 0; i < N; i++) {
    int next;
    cin >> next;
    A.push_back(make_pair(i, next - 1));
  }
  vec<int> move;
  int curr = 0;
  while (true) {
    int next = -1;
    for (int i = 0; i < A.size(); i++) {
      if (A[i].first == curr) {
        next = A[i].second;
        A.erase(A.begin() + i);
        break;
      }
    }
    move.push_back(curr);
    DEBUG LINE;
    if (next == -1) {
      break;
    } else {
      curr = next;
    }
  }
  DEBUG LINE;
  int loop_first_val = move[move.size() - 1]; //.second;
  DEBUG cout << "loop_first_val : " << loop_first_val << endl;
  move.resize(move.size() - 1);
  int loop_first_point;
  for (int i = 0; i < move.size(); i++) {
    if (move[i] == loop_first_val) {
      loop_first_point = i;
      break;
    }
  }
  DEBUG cout << "loop_first_point : " << loop_first_point << endl;

  if (K < move.size()) {
    cout << move[K] + 1 << endl;
  } else {
    int sift = (K - loop_first_point) % (move.size() - loop_first_point);
    cout << move[loop_first_point + sift] + 1 << endl;
  }

  return 0;
}