Problem p02684 (C++) — buggy submission, verdict: Runtime Error

/*
AUTHOR: Aman Raj
LANG: C++
*/

using namespace std;

typedef long long ll;
#include <bits/stdc++.h>

// setbase - cout << setbase (16); cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints
// xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define inf 1e9
// #define mod 3
#define flush fflush(stdout)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef cc_hash_table<int, ll, hash<ll>> ht;

// find_by_order()  // order_of_key
typedef tree<int, null_type, less<long long int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll power1(ll a, ll b) // a raised to power b
{
  if (b <= 0)
    return 1;
  if (b == 1)
    return a;
  ll c = power1(a, b / 2);
  c *= c;
  c = c % mod;
  if (b % 2 == 1)
    c *= a;
  c = c % mod;
  return c;
}
/* ll mi(ll a,ll m)
 { ll c= power1(a,m-2,mod);

   return c;
 }
*/

int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (auto &x : v)
    cin >> x;
  vector<ll> visited(n, -1);
  ll t = 0, times = 0;
  visited[0] = 0;
  while (k > 0) {
    t = v[t] - 1;
    times++;
    k--;
    if (visited[t] >= 0) {
      break;
    }

    visited[t] = times;
  }
  if (k <= 0) {
    cout << t + 1 << endl;
    return 0;
  }
  times = times - visited[t];
  times = k % times;
  while (times--) {
    t = v[t] - 1;
  }
  cout << t + 1 << endl;

  return 0;
}


Error: 0

Fixed submission:

/*
AUTHOR: Aman Raj
LANG: C++
*/

using namespace std;

typedef long long ll;
#include <bits/stdc++.h>

// setbase - cout << setbase (16); cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints
// xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define inf 1e9
// #define mod 3
#define flush fflush(stdout)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef cc_hash_table<int, ll, hash<ll>> ht;

// find_by_order()  // order_of_key
typedef tree<int, null_type, less<long long int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll power1(ll a, ll b) // a raised to power b
{
  if (b <= 0)
    return 1;
  if (b == 1)
    return a;
  ll c = power1(a, b / 2);
  c *= c;
  c = c % mod;
  if (b % 2 == 1)
    c *= a;
  c = c % mod;
  return c;
}
/* ll mi(ll a,ll m)
 { ll c= power1(a,m-2,mod);

   return c;
 }
*/

int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (auto &x : v)
    cin >> x;
  vector<ll> visited(n, -1);
  ll t = 0, times = 0;
  visited[0] = 0;
  while (k > 0) {
    t = v[t] - 1;
    times++;
    k--;
    if (visited[t] >= 0) {
      break;
    }

    visited[t] = times;
  }
  k = max(k, 0ll);
  times = times - visited[t];
  times = k % times;
  while (times--) {
    t = v[t] - 1;
  }
  cout << t + 1 << endl;

  return 0;
}