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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <signal.h>
using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define resz resize
#define ini ll_MAX
#define inl LLONG_MAX
#define MOD 1000000007
#define sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define F0R(i, a) for (ll i = 0; i < (a); i++)
#define FORR(i, a, b) for (ll i = a; i >= (b); i--)
#define F0RR(i, a) for (ll i = a; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define pbds                                                                   \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;

using pii = pair<ll, ll>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;

ll mod(ll a, ll b) { return (b + a % b) % b; }

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll po(ll a, ll b) {
  ll ans = 1;
  while (b--)
    ans *= a;
  return ans;
}
ll pom(ll a, ll b, ll mod) {
  if (b == 0)
    return 1;
  ll k = pom(a, b / 2, mod);
  if (b % 2 == 0)
    return (k * k) % mod;
  else
    return ((k * k) * mod * a) % mod;
}

ll sqdist(ll a, ll b, ll c, ll d) {
  return (a - c) * (a - c) + (b - d) * (b - d);
}
ll fact(ll n) {
  if (n == 0)
    return 1;
  ll ans = 1;
  FOR(i, 1, n + 1) ans *= i;
  return ans;
}

ll visited[200005], dist[200005], vec[200005];
ll p, q;
vector<ll> st;
void dfs() {

  // cout<<p<<' '<<q<<endl;
  if (visited[q])
    return;
  st.pb(q);
  visited[q]++;
  dist[q] = dist[p] + 1;
  p = q;
  q = vec[q];
  dfs();
}

void solve() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
    vec[i]--;
  }
  p = 0, q = vec[p];
  st.pb(p);
  dist[p] = 0;
  dfs();
  // FOR(i,0,n)cout<<dist[i]<<' ';cout<<endl;
  // for(auto i:st)cout<<i<<' ';cout<<endl;
  if (k < dist[q])
    cout << st[k] + 1;
  else {
    k -= dist[q];
    k %= (dist[p] - dist[q] + 1);
    cout << st[dist[q] + k] + 1;
  }
}
// 4 5 2 3 4 1
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <signal.h>
using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define resz resize
#define ini ll_MAX
#define inl LLONG_MAX
#define MOD 1000000007
#define sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define F0R(i, a) for (ll i = 0; i < (a); i++)
#define FORR(i, a, b) for (ll i = a; i >= (b); i--)
#define F0RR(i, a) for (ll i = a; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define pbds                                                                   \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;

using pii = pair<ll, ll>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;

ll mod(ll a, ll b) { return (b + a % b) % b; }

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll po(ll a, ll b) {
  ll ans = 1;
  while (b--)
    ans *= a;
  return ans;
}
ll pom(ll a, ll b, ll mod) {
  if (b == 0)
    return 1;
  ll k = pom(a, b / 2, mod);
  if (b % 2 == 0)
    return (k * k) % mod;
  else
    return ((k * k) * mod * a) % mod;
}

ll sqdist(ll a, ll b, ll c, ll d) {
  return (a - c) * (a - c) + (b - d) * (b - d);
}
ll fact(ll n) {
  if (n == 0)
    return 1;
  ll ans = 1;
  FOR(i, 1, n + 1) ans *= i;
  return ans;
}
void exitfunc(ll sig) { exit(0); }
void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> vec(n, 0);
  for (auto &i : vec) {
    cin >> i;
    i--;
  }
  vector<ll> dist(n, 0), stack, visited(n, 0);
  ll curr = 0;
  visited[0]++;
  ll p, q;
  stack.push_back(0);
  curr = 0;
  ll c = 10;
  while (1) {
    c--;
    if (!c)
      break;
    p = curr, q = vec[curr];
    if (visited[q])
      break;
    visited[curr]++;
    dist[q] = dist[p] + 1;
    stack.push_back(q);
    curr = q;
  }

  if (k < dist[q])
    cout << stack[k] << endl;
  else {
    k -= dist[q];
    k %= (dist[p] - dist[q] + 1);
    if (dist[q] + k > stack.size())
      cout << "H" << endl;
    else
      cout << stack[dist[q] + k] + 1 << endl;
  }
}
// 4 5 2 3 4 1
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}