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

#include <bits/stdc++.h>
#include <string>
#include <vector>
using namespace std;
#define l long long
#define pb push_back
#define mem(a, n) memset(a, n, sizeof(a))
#define find(v, n) find(v.begin(), v.end(), n)
#define sort1(v) sort(v.begin(), v.end())
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define mod 998244353
#define reverse(s) reverse(s.begin(), s.end())
// #define pi pair<l,l>
const double pi = 3.141592653589793;
bool isprime(l n) {
  for (l i = 2; i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
bool fact(l n) {
  vector<l> v;
  for (l i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if (i % 2 == 1 || (n / i) % 2 == 1) {
        return true;
      }
    }
  }
  return false;
}
l power(l a, l b) {
  l ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    b >>= 1;
    a = (a % mod * a % mod) % mod;
  }
  return ans;
} // prefixsums
void frooty() {
  l n, k;
  cin >> n >> k;
  vector<l> v;
  v.pb(0LL);
  for (l i = 0; i < n; i++) {
    l a;
    cin >> a;
    v.pb(a);
  }
  bool vis[n + 1];
  mem(vis, 0);
  vis[1] = true;
  l temp = 1;
  l x = 0;
  l ret = 0;
  for (l i = 0; i < n; i++) {
    if (x == k) {
      cout << temp << "\\n";
      return;
    }
    temp = v[temp];
    x++;
    if (vis[temp] == false) {
      vis[temp] = true;
    } else {
      ret = temp;
      break;
    }
  }
  l co = 1;
  mem(vis, 0);
  temp = 1;
  x = 0;
  while (vis[ret] != true) {
    vis[temp] = true;
    x++;
    temp = v[temp];
  }
  // cout<<ret<<" "<<x-1<<"\\n";
  temp = ret;
  vector<l> ans((l)(1e6), 0);
  mem(vis, 0);
  ans[0] = temp;
  while (vis[ret] != true) {
    temp = v[temp];
    vis[temp] = true;
    ans[co] = temp;
    co++;
  }
  //	k++;
  co--;
  k -= (x - 1);
  if (co == 0) {
    cout << ret << "\\n";
    return;
  }
  l r = k % co;
  //	cout<<co<<"\\n";
  cout << ans[r] << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  //	cin>>q;
  while (q--) {
    frooty();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <string>
#include <vector>
using namespace std;
#define l long long
#define pb push_back
#define mem(a, n) memset(a, n, sizeof(a))
#define find(v, n) find(v.begin(), v.end(), n)
#define sort1(v) sort(v.begin(), v.end())
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define mod 998244353
#define reverse(s) reverse(s.begin(), s.end())
// #define pi pair<l,l>
const double pi = 3.141592653589793;
bool isprime(l n) {
  for (l i = 2; i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
bool fact(l n) {
  vector<l> v;
  for (l i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if (i % 2 == 1 || (n / i) % 2 == 1) {
        return true;
      }
    }
  }
  return false;
}
l power(l a, l b) {
  l ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    b >>= 1;
    a = (a % mod * a % mod) % mod;
  }
  return ans;
} // prefixsums
void frooty() {
  l n, k;
  cin >> n >> k;
  vector<l> v;
  v.pb(0LL);
  for (l i = 0; i < n; i++) {
    l a;
    cin >> a;
    v.pb(a);
  }
  bool vis[n + 1];
  mem(vis, 0);
  vis[1] = true;
  l temp = 1;
  l x = 0;
  l ret = 0;
  for (l i = 0; i < n; i++) {
    //	if(x==k)
    //	{
    //		cout<<temp<<"\\n";
    //		return ;
    //	}
    temp = v[temp];
    x++;
    if (vis[temp] == false) {
      vis[temp] = true;
    } else {
      ret = temp;
      break;
    }
  }
  l co = 1;
  mem(vis, 0);
  temp = 1;
  x = 0;
  while (vis[ret] != true) {
    vis[temp] = true;
    x++;
    temp = v[temp];
  }
  // cout<<ret<<" "<<x-1<<"\\n";
  temp = ret;
  vector<l> ans((l)(1e6), 0);
  mem(vis, 0);
  ans[0] = temp;
  while (vis[ret] != true) {
    temp = v[temp];
    vis[temp] = true;
    ans[co] = temp;
    co++;
  }
  //	k++;
  co--;
  k -= (x - 1);
  l r = k % co;
  //	cout<<co<<"\\n";
  cout << ans[r] << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  //	cin>>q;
  while (q--) {
    frooty();
  }
}