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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define INF 1LL << 60

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

pair<ll, ll> dfs(ll from, vector<ll> &edge, int d, vector<ll> &dist) {
  if (dist[from] == -1) {
    dist[from] = d;
  }
  if (dist[edge[from]] >= 0) {
    return make_pair(edge[from], dist[from] + 1 - dist[edge[from]]);
  }

  return dfs(edge[from], edge, dist[from] + 1, dist);
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) {
    cin >> A[i];
    A[i]--;
  }
  vector<ll> dist(N, -1);
  // map<ll, ll> dist_to_v;
  vector<ll> dist_to_v(2 * N);
  dist[0] = 0;

  pair<ll, ll> loop_start = dfs(0, A, 0, dist);
  for (ll i = 0; i < N; i++) {
    if (dist[i] >= 0)
      dist_to_v[dist[i]] = i;
  }
  // cerr << loop_start.first << " " << loop_start.second  << " " << dist[1] <<
  // endl;
  ll ans = 0;
  if (K < dist[loop_start.first]) {
    cout << dist_to_v[K] + 1 << endl;
    return 0;
  }
  if (loop_start.first == 0) {
    K %= loop_start.second;
    cout << dist_to_v[K] + 1 << endl;
  } else {
    K -= dist[loop_start.first];
    K %= loop_start.second;
    // cerr << "dist: " << K + dist[loop_start.first] << endl;
    cout << dist_to_v[K + dist[loop_start.first]] + 1 << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define INF 1LL << 60

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

pair<ll, ll> dfs(ll from, vector<ll> &edge, int d, vector<ll> &dist) {
  if (dist[from] == -1) {
    dist[from] = d;
  } else if (dist[edge[from]] >= 0) {
    return make_pair(edge[from], dist[from] + 1 - dist[edge[from]]);
  }

  return dfs(edge[from], edge, dist[from] + 1, dist);
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) {
    cin >> A[i];
    A[i]--;
  }
  vector<ll> dist(N, -1);
  // map<ll, ll> dist_to_v;
  vector<ll> dist_to_v(2 * N);
  dist[0] = 0;

  pair<ll, ll> loop_start = dfs(0, A, 0, dist);
  for (ll i = 0; i < N; i++) {
    if (dist[i] >= 0)
      dist_to_v[dist[i]] = i;
  }
  // cerr << loop_start.first << " " << loop_start.second  << " " << dist[1] <<
  // endl;
  ll ans = 0;
  if (loop_start.first == 0) {
    K %= loop_start.second;
    cout << dist_to_v[K] + 1 << endl;
  } else {
    K -= dist[loop_start.first];
    K %= loop_start.second;
    cout << dist_to_v[K + dist[loop_start.first]] + 1 << endl;
  }
  return 0;
}