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

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

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

#define mp make_pair
#define pb push_back
#define pf push_front
#define ss second
#define ff first
#define sz(x) (int)x.size()
#define newl "\\n"
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vl vector<ll>
#define pll pair<ll, ll>
#define vll vector<pll>
#define mem(x, val) memset(x, val, sizeof(x))
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define all(v) (v).begin(), (v).end()

const ld pi = 3.14159265359;
ll INF = 1e18 + 10;
ll MOD = 998244353;
ll mod = 1e9 + 7;

inline ll add(ll a, ll b, ll m) {
  if ((a + b) >= m)
    return (a + b) % m;
  return a + b;
}

inline ll mul(ll a, ll b, ll m) {
  if ((a * b) < m)
    return a * b;
  return (a * b) % m;
}

ll power(ll x, ll y, ll m) {
  ll res = 1;
  x = x % m;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vl v(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int i = 0, nxt = 0;
  while (i < 2 * n) {
    a[v[nxt] - 1]++;
    nxt = v[nxt] - 1;
    i++;
  }
  set<int> s;
  int cnt = 0;
  for (int j = 0; j < n; j++) {
    if (a[j] > 1) {
      s.insert(j);
      cnt++;
    }
  }
  i = 0;
  nxt = 0;
  vi u;
  ll stage = -1;
  while (i < n) {
    if (s.count(nxt)) {
      u.pb(nxt);
      if (stage == -1) {
        stage = (ll)i;
      }
    }
    nxt = v[nxt] - 1;
    i++;
  }
  int len = sz(u);
  if (k < stage) {
    nxt = 0, i = 0;
    while (i < k) {
      nxt = v[nxt] - 1;
      i++;
    }
    cout << nxt + 1;
    return;
  }
  k -= stage;
  ll pos = k % cnt;
  cout << u[pos] + 1;
}

int main() {
  fastio;
  int t;

  t = 1;
  // cin >> t;
  // int test=t;
  while (t-- > 0) {
    // cout<<"Case #"<<(test-t)<<": ";
    solve();
    cout << newl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

#define mp make_pair
#define pb push_back
#define pf push_front
#define ss second
#define ff first
#define sz(x) (int)x.size()
#define newl "\\n"
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vl vector<ll>
#define pll pair<ll, ll>
#define vll vector<pll>
#define mem(x, val) memset(x, val, sizeof(x))
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define all(v) (v).begin(), (v).end()

const ld pi = 3.14159265359;
ll INF = 1e18 + 10;
ll MOD = 998244353;
ll mod = 1e9 + 7;

inline ll add(ll a, ll b, ll m) {
  if ((a + b) >= m)
    return (a + b) % m;
  return a + b;
}

inline ll mul(ll a, ll b, ll m) {
  if ((a * b) < m)
    return a * b;
  return (a * b) % m;
}

ll power(ll x, ll y, ll m) {
  ll res = 1;
  x = x % m;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vl v(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int i = 0, nxt = 0;
  while (i < 64 * n) {
    a[v[nxt] - 1]++;
    nxt = v[nxt] - 1;
    i++;
  }
  set<int> s;
  int cnt = 0;
  for (int j = 0; j < n; j++) {
    if (a[j] > 1) {
      s.insert(j);
      cnt++;
    }
  }
  i = 0;
  nxt = 0;
  vi u;
  ll stage = -1;
  while (i < n) {
    if (s.count(nxt)) {
      u.pb(nxt);
      if (stage == -1) {
        stage = (ll)i;
      }
    }
    nxt = v[nxt] - 1;
    i++;
  }
  int len = sz(u);
  k -= stage;
  ll pos = k % cnt;
  cout << u[pos] + 1;
}

int main() {
  fastio;
  int t;

  t = 1;
  // cin >> t;
  // int test=t;
  while (t-- > 0) {
    // cout<<"Case #"<<(test-t)<<": ";
    solve();
    cout << newl;
  }

  return 0;
}