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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<pair<int, pair<int, int>>> viii;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<vii> vvii;

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define MEM(a, b) memset(a, (b), sizeof(a))
#define all(a) a.begin(), a.end()
#define loop(x, a, b) for (int(x) = (a); (x) < (b); (x)++)
#define rep(x, n) for (int(x) = 0; (x) < (n); (x)++)
#define tr(c, it)                                                              \\
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define ain(a, n)                                                              \\
  int((a)[(n)]);                                                               \\
  for (int i = 0; i < (n); i++)                                                \\
  cin >> ((a)[i])
#define md 1000000007
#define MAXN 200005

int main() {
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  ll vis[n + 1];
  MEM(vis, 0);
  loop(i, 1, n + 1) { cin >> a[i]; }
  ll cycle = 1;
  ll next = a[1], start;
  vis[1] = 1;
  vll v;
  v.pb(1);
  while (1) {
    // cout << "ll: " << next << endl;
    if (vis[next]) {
      start = next;
      break;
    }
    vis[next] = 1;
    v.pb(next);
    next = a[next];
    cycle++;
  }
  int f = 0, sI;
  vll newV;

  for (int i = 0; i < v.size(); i++) {
    // cout << v[i] << "gg" << endl;
    if (v[i] == start) {
      f = 1;
      sI = i + 1;
    }
    if (f == 1) {
      newV.pb(v[i]);
    }
  }
  // cout << "si : " << sI << endl;
  if (k > sI) {
    k -= (sI - 1);
    cout << newV[k % (newV.size())] << endl;
  } else {
    cout << v[k] << endl;
    ;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<pair<int, pair<int, int>>> viii;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<vii> vvii;

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define MEM(a, b) memset(a, (b), sizeof(a))
#define all(a) a.begin(), a.end()
#define loop(x, a, b) for (int(x) = (a); (x) < (b); (x)++)
#define rep(x, n) for (int(x) = 0; (x) < (n); (x)++)
#define tr(c, it)                                                              \\
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define ain(a, n)                                                              \\
  int((a)[(n)]);                                                               \\
  for (int i = 0; i < (n); i++)                                                \\
  cin >> ((a)[i])
#define md 1000000007
#define MAXN 200005

int main() {
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  ll a[n + 1], ind;
  loop(i, 1, n + 1) {
    cin >> a[i];
    if (a[i] == 1)
      ind = i;
  }
  ll cycle = 1;
  ll next = a[a[ind]];
  vll v;
  v.pb(a[ind]);
  while (next != a[ind]) {
    v.pb(next);
    next = a[next];
    cycle++;
  }
  // for (auto x: v)
  // 	cout << x << endl;
  // cout << k%cycle << endl;
  cout << v[k % cycle] << endl;
  return 0;
}