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

#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 <cstdlib>
#include <deque> // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#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
#define rep(i, x) for (ll i = 0; i < x; i++)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ld = long double;
using ll = long long;
const ll INF = 10010010010;
typedef pair<ll, ll> P;
using graph = vector<vector<ll>>;
const ll mod = 1000000007;
ll gcd(ll a, ll b) { // a>b
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
} /*フィボナッチテンプレ
 ll memo[50];
 ll fibo(ll n) {
     if (memo[n] != 0)return memo[n];
     if (n <= 1)return memo[n] = n;
     else return memo[n] = fibo(n - 1) + fibo(n - 2);
 }
 */
bool isp(ll n) {
  bool res = true;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      res = false;
      break;
    }
  }
  return res;
}
ll ctoi(char c) {
  if (c == '0')
    return 0;
  else if (c == '1')
    return 1;
  else if (c == '2')
    return 2;
  else if (c == '3')
    return 3;
  else if (c == '4')
    return 4;
  else if (c == '5')
    return 5;
  else if (c == '6')
    return 6;
  else if (c == '7')
    return 7;
  else if (c == '8')
    return 8;
  else if (c == '9')
    return 9;
  else
    return -1;
}
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> tele(N);
  vector<ll> visit(N);
  rep(i, N) cin >> tele[i];
  ll now = 0;
  ll cnt = 0;
  ll shote = 0;
  ll start = 0;
  bool roop = false;
  while (true) {
    if (visit[now] == 1) {
      roop = true;
      start = tele[now] - 1;
    }
    if (visit[now] == 2) {
      break;
    }
    if (roop)
      cnt++;
    else
      shote++;
    visit[now]++;
    now = tele[now] - 1;
  }
  shote -= cnt;
  now = 0;
  if (K < shote) {
    rep(i, K) { now = tele[now] - 1; }
    cout << now + 1 << endl;
  } else {
    K -= shote;
    K %= cnt;
    now = start;
    //       cout << now << " " << K << endl;
    rep(i, K) { now = tele[now] - 1; }
    cout << now + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#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
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> tel(N);
  for (int i = 0; i < N; i++) {
    cin >> tel[i];
  }
  int count = 0;
  int now = 1;
  while (count < K) {
    now -= 1;
    now = tel[now];
    count++;
  }
  cout << now << endl;
}