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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " = " << x << '\\n'
#define debug_arr(a, n)                                                        \\
  for (ll i = 0; i < n; i++)                                                   \\
  cout << a[i] << " "
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<ll>
#define vll vector<ll>
#define inf 1000000000
#define mod 1000000007

const ll max_n = 2e5 + 9;

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
ll power(ll a, ll b) {
  ll prod = 1;
  while (b) {
    if (b & 1)
      prod = (prod * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return prod;
}
ll n, k;
ll arr[max_n];
vi a[max_n];
ll loop_index;
ll visited[max_n];
vi ans;
ll flag;
map<ll, ll> m;
void dfs(ll s, ll cnt = 0) {
  if (visited[s]) {
    loop_index = m[s];
    flag = 1;
  }
  visited[s] = 1;
  if (flag)
    return;
  ans.pb(s);
  m[s] = ans.size() - 1;
  for (auto it : a[s]) {
    dfs(it, cnt + 1);
  }
}
int main() {
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    a[i + 1].pb(arr[i]);
  }

  dfs(1);
  if (k < ans.size()) {
    cout << ans[k] << endl;
    return 0;
  } else {
    k -= loop_index;
    ll num = ans.size() - loop_index;
    k %= num;
    cout << ans[loop_index + k] << endl;
    return 0;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " = " << x << '\\n'
#define debug_arr(a, n)                                                        \\
  for (ll i = 0; i < n; i++)                                                   \\
  cout << a[i] << " "
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<ll>
#define vll vector<ll>
#define inf 1000000000
#define mod 1000000007

const ll max_n = 1e5 + 9;

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
ll power(ll a, ll b) {
  ll prod = 1;
  while (b) {
    if (b & 1)
      prod = (prod * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return prod;
}
ll n, k;
ll arr[max_n];
vi a[max_n];
ll loop_index;
ll visited[max_n];
vi ans;
ll flag;
map<ll, ll> m;
void dfs(ll s, ll cnt = 0) {
  if (visited[s]) {
    loop_index = m[s];
    flag = 1;
  }
  visited[s] = 1;
  if (flag)
    return;
  ans.pb(s);
  m[s] = ans.size() - 1;
  for (auto it : a[s]) {
    dfs(it, cnt + 1);
  }
}
int main() {
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    a[i + 1].pb(arr[i]);
  }

  dfs(1);
  if (k <= ans.size()) {
    cout << ans[k - 1] << endl;
    return 0;
  } else {
    k -= loop_index;
    ll num = ans.size() - loop_index;
    k %= num;
    cout << ans[loop_index + k] << endl;
    return 0;
  }
  return 0;
}