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

#include <bits/stdc++.h>

typedef long long int ll;
#define cases                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define scan(n) scanf("%d", &n)
#define pb push_back
#define mod 1000000007
#define maxsz 200001
#define int ll

using namespace std;

signed main() {
  int n, k;
  cin >> n >> k;
  int ar[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  set<int> st;
  vector<int> cyc;
  int cycst;
  int ind = 1;
  while (1) {
    cyc.push_back(ar[ind]);
    st.insert(ar[ind]);
    ind = ar[ind];
    if (st.find(ar[ind]) != st.end()) {
      cycst = ar[ind];
      break;
    }
  }
  int cycind;
  for (int i = 0; i < (int)cyc.size(); i++) {
    if (cyc[i] == cycst) {
      cycind = i;
      break;
    }
  }
  int sz = cyc.size() - cycind;

  //    for(int i=0;i<cyc.size();i++){
  //        cout<<cyc[i]<<" ";
  //    }cout<<endl;
  //    cout<<"cycind:"<<cycind<<endl;
  //    cout<<"sz:"<<sz<<endl;

  int ans;
  if (k <= cycind) {
    ans = k - 1;
    //        cout<<"ans:"<<ans<<endl;
    cout << cyc[ans] << endl;
  } else {
    k -= cycind;
    ans = (k % sz);
    if (ans == 0)
      ans = sz;
    ans--;
    //        cout<<"ans:"<<ans+cycind<<endl;
    cout << cyc[ans + cycind];
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

typedef long long int ll;
#define cases                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define scan(n) scanf("%d", &n)
#define pb push_back
#define mod 1000000007
#define maxsz 200001
#define int ll

using namespace std;

signed main() {
  int n, k;
  cin >> n >> k;
  int ar[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  set<int> st;
  vector<int> cyc;
  int cycst;
  int ind = 1;
  while (1) {
    cyc.push_back(ar[ind]);
    st.insert(ar[ind]);
    ind = ar[ind];
    if (st.find(ar[ind]) != st.end()) {
      cycst = ar[ind];
      break;
    }
  }
  int cycind;
  for (int i = 0; i < cyc.size(); i++) {
    if (cyc[i] == cycst) {
      cycind = i;
      break;
    }
  }
  int sz = cyc.size() - cycind;

  //    for(int i=0;i<cyc.size();i++){
  //        cout<<cyc[i]<<" ";
  //    }cout<<endl;
  //    cout<<"cycind:"<<cycind<<endl;
  //    cout<<"sz:"<<sz<<endl;

  int ans;
  if (k <= cycind) {
    ans = k - 1;
  } else {
    k -= cycind;
    ans = (k % sz);
    if (ans == 0)
      ans = sz;
    ans--;
  }
  //    cout<<"ans:"<<ans+cycind<<endl;
  cout << cyc[ans + cycind];
}