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

#include "bits/stdc++.h"
using namespace std;
// #define int long long
#define repo(i, begin, end)                                                    \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ll long long
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << "  ";
#define fastr                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (int i = (a - 1); i >= b; i--)
#define fi first
#define se second
#define tit(v, i, c) get<c>(v[i])
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define mp make_pair
double jk = 0.004;
#define power(x, y) pow(x, y) + jk
#define endl "\\n"
const long long mod = 1e9 + 7;
const long long N = 1000000000;
const ll val = 1000005;
int in() {
  int n;
  cin >> n;
  return n;
}
bool visited[val];
void _do_() {
  memset(visited, false, sizeof(visited));
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  rep(i, 1, n + 1) cin >> a[i];
  ll i = 1;
  vector<ll> v, s;
  s.pb(0);

  while (visited[i] == false && k > 0) {
    visited[i] = true;
    v.pb(i);
    k--;
    i = a[i];
  }
  if (!k)
    cout << i << endl;
  else {
    memset(visited, false, sizeof(visited));
    // visited[i]=true;
    while (!visited[i]) {
      visited[i] = true;
      s.pb(i);
      i = a[i];
    }
    ll z = k + 1;
    ll t = s.size() - 1;
    z = z % t;
    if (z == 0)
      z = t;
    // if(z==0)z=t;
    // dbg(z);
    cout << s[z] << endl;
  }
}
signed main() {
  fastr
      // int tc=in();
      int tc = 1;
  while (tc--)
    _do_();
  cerr << "Time elapsed:";
  cerr << 1000 * clock() / CLOCKS_PER_SEC << " ms ";
  return 0;
}

Error: 0
55: z = 3  Time elapsed:29 ms 

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
// #define int long long
#define repo(i, begin, end)                                                    \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ll long long
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << "  ";
#define fastr                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (int i = (a - 1); i >= b; i--)
#define fi first
#define se second
#define tit(v, i, c) get<c>(v[i])
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define mp make_pair
double jk = 0.004;
#define power(x, y) pow(x, y) + jk
#define endl "\\n"
const long long mod = 1e9 + 7;
const long long N = 1000000000;
const ll val = 1000005;
int in() {
  int n;
  cin >> n;
  return n;
}
bool visited[val];
void _do_() {
  memset(visited, false, sizeof(visited));
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  rep(i, 1, n + 1) cin >> a[i];
  ll i = 1;
  vector<ll> v, s;
  s.pb(0);

  while (visited[i] == false) {
    visited[i] = true;
    v.pb(i);
    i = a[i];
  }
  memset(visited, false, sizeof(visited));
  // visited[i]=true;
  while (!visited[i]) {
    visited[i] = true;
    s.pb(i);
    i = a[i];
  }
  ll z = k - v.size() + 1;
  ll t = s.size() - 1;
  z = z % t;
  if (z == 0)
    z = t;
  dbg(z);
  cout << s[z] << endl;
}
signed main() {
  fastr
      // int tc=in();
      int tc = 1;
  while (tc--)
    _do_();
  cerr << "Time elapsed:";
  cerr << 1000 * clock() / CLOCKS_PER_SEC << " ms ";
  return 0;
}