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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define debug(name) DEBUG(#name, (name))
template <typename T> void DEBUG(string label, T value) {
  cerr << "[" << label << " = " << value << "]\\n";
}

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> arr(n);
  for (auto &i : arr) {
    cin >> i;
    i--;
  }

  vector<bool> vis(n);
  vector<int> path;
  int cur = 0;

  int last = -1;
  while (1) {
    if (vis[cur]) {
      last = arr[cur];
      break;
    }
    vis[cur] = true;
    cur = arr[cur];
    path.push_back(cur);
  }
  // for(auto i : path){
  // cout << i+1 << " ";
  //}
  // cout << endl;
  assert(last != -1);

  int start = 0;
  for (int i = path.size() - 2; i >= 0; --i) {
    if (path[i] == last) {
      start = i;
      break;
    }
  }

  ll sz = path.size();
  k--;
  if (k < sz) {
    cout << path[k % path.size()] + 1 << endl;
  } else {

    ll ns = sz - (ll)start;
    k -= start;
    ll idx = k % ns + (ll)start;

    cout << path[idx] + 1 << endl;
  }
}
int main(int argv, char **argc) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}


Error: WA

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define debug(name) DEBUG(#name, (name))
template <typename T> void DEBUG(string label, T value) {
  cerr << "[" << label << " = " << value << "]\\n";
}

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> arr(n);
  for (auto &i : arr) {
    cin >> i;
    i--;
  }

  vector<bool> vis(n);
  vector<int> path;
  int cur = 0;

  int last = -1;
  while (1) {
    if (vis[cur]) {
      last = arr[cur];
      break;
    }
    vis[cur] = true;

    cur = arr[cur];
    path.push_back(cur);
  }
  assert(last != -1);
  int start = -1;
  for (int i = path.size() - 2; i >= 0; --i) {
    if (path[i] == last) {
      start = i;
      break;
    }
  }
  // vector<int> ans = {6, 2, 5, 3, 2, 5, 3, 2, 5, 3, 2, 5, 3, 2, 5, 3, 2};
  ll sz = path.size();
  // for(int query = 1; query <= ans.size(); ++query){
  // k = query;
  // int ac_ans = ans[k-1];
  k--;
  // int test;
  if (k < sz) {
    cout << path[k % path.size()] + 1 << endl;
    // test = path[k % sz]+1 ;
  } else {
    assert(start != -1);
    for (auto i : path) {
      cout << i + 1 << " ";
    }
    cout << endl;
    // debug(start);

    ll ns = sz - (ll)start;
    k -= start;
    ll idx = k % ns + (ll)start;

    cout << path[idx] + 1 << endl;
    // test = path[idx]+1;
  }
  // debug(test);
  // debug(ac_ans);

  // assert(test == ac_ans);
  //}
}
int main(int argv, char **argc) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}