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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#define DEBUG(x) cout << ">> " << #x << ": " << x << endl;
#define _                                                                      \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int MOD = 1000000007;
typedef long long ll;
typedef long double ld;

int main() {
  _ ll n, k;
  cin >> n >> k;
  vector<ll> grid(n);
  for (ll i = 0; i < n; i++)
    cin >> grid[i];
  map<ll, ll> mapp;
  ll pos = 0, cs, dist = 0, inn;
  while (true) {
    if (mapp.find(pos) != mapp.end()) {
      cs = dist - mapp[pos];
      inn = mapp[pos];
      break;
    }
    if (dist == k) {
      cout << pos + 1 << endl;
      return 0;
    }
    mapp[pos] = dist++;
    pos = grid[pos] - 1;
  }
  vector<ll> path;
  for (ll i = 0; i < cs; i++) {
    path.push_back(pos);
    pos = grid[pos] - 1;
  }
  cout << path[((k - inn) % cs + cs) % cs] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#define DEBUG(x) cout << ">> " << #x << ": " << x << endl;
#define _                                                                      \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int MOD = 1000000007;
typedef long long ll;
typedef long double ld;

int main() {
  _ ll n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> grid(n);
  for (ll i = 0; i < n; i++)
    cin >> grid[i];
  map<ll, ll> mapp;
  vector<bool> visited(n, false);
  ll pos = 0, cs, dist = 0, inn;
  while (true) {
    if (visited[pos]) {
      cs = dist - mapp[pos];
      inn = mapp[pos];
      break;
    }
    visited[pos] = true;
    mapp[pos] = dist++;
    pos = grid[pos] - 1;
  }
  vector<ll> path;
  for (ll i = 0; i < cs; i++) {
    path.push_back(pos);
    pos = grid[pos] - 1;
  }
  cout << path[(k - inn) % cs] + 1 << endl;
  return 0;
}