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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pll pair<ll, ll>
#define pb push_back
#define mp(x, y) make_pair((x), (y))
#define F first
#define S second
#define I insert
#define vll vector<ll>
#define vbl vector<bool>
#define vvll vector<vll>
#define vld vector<ld>
#define vb vector<bool>
#define vc vector<char>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << " ]";
}
template <class T> ostream &operator<<(ostream &os, set<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << " ]";
}
template <class T, class R> ostream &operator<<(ostream &os, map<T, R> V) {
  os << "[ ";
  for (auto v : V)
    os << "(" << v.first << ":" << v.second << ")";
  return os << " ]";
}
template <class T, class R> ostream &operator<<(ostream &os, pair<T, R> V) {
  return os << "(" << V.first << "," << V.second << ")";
}

#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
clock_t clk = clock();
#define checktime()                                                            \\
  cout << "\\nTime Elapsed : " << float(clock() - clk) / CLOCKS_PER_SEC << endl
#else
#define trace(...) ;
#define checktime() ;
#endif

const ll Mod = 1e9 + 7;
// const ll Mod = 998244353;
const ll MAXN = 1e6;

void solve() {
  ll n, k;
  cin >> n >> k;
  vll a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  set<ll> se;
  vll v;
  ll i = 1, val;
  while (1) {
    se.I(i);
    v.pb(i);
    i = a[i - 1];
    if (se.find(i) != se.end()) {
      val = i;
      break;
    }
  }
  ll l = 0;
  for (ll pp : v) {
    if (pp == val)
      break;
    l++;
  }
  // trace(k,l,v,val);
  if (k <= l) {
    // trace("HI");
    cout << v[k] << "\\n";
  } else {
    // 1 2 3 4
    k -= l;
    ll nn = v.size() - l;
    cout << v[l + k % nn] << "\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(25);

  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pll pair<ll, ll>
#define pb push_back
#define mp(x, y) make_pair((x), (y))
#define F first
#define S second
#define I insert
#define vll vector<ll>
#define vbl vector<bool>
#define vvll vector<vll>
#define vld vector<ld>
#define vb vector<bool>
#define vc vector<char>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << " ]";
}
template <class T> ostream &operator<<(ostream &os, set<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << " ]";
}
template <class T, class R> ostream &operator<<(ostream &os, map<T, R> V) {
  os << "[ ";
  for (auto v : V)
    os << "(" << v.first << ":" << v.second << ")";
  return os << " ]";
}
template <class T, class R> ostream &operator<<(ostream &os, pair<T, R> V) {
  return os << "(" << V.first << "," << V.second << ")";
}

#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
clock_t clk = clock();
#define checktime()                                                            \\
  cout << "\\nTime Elapsed : " << float(clock() - clk) / CLOCKS_PER_SEC << endl
#else
#define trace(...) ;
#define checktime() ;
#endif

const ll Mod = 1e9 + 7;
// const ll Mod = 998244353;
const ll MAXN = 1e6;

void solve() {
  ll n, k;
  cin >> n >> k;
  vll a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  set<ll> se;
  vll v;
  ll i = 1, val;
  while (1) {
    se.I(i);
    v.pb(i);
    i = a[i - 1];
    if (se.find(i) != se.end()) {
      val = i;
      break;
    }
  }
  ll l = 0;
  for (ll pp : v) {
    if (pp == val)
      break;
    l++;
  }
  if (k <= l) {
    cout << v[i] << "\\n";
  } else {
    k -= l;
    ll nn = v.size() - l;
    cout << v[l + k % nn] << "\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(25);

  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}