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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1000000007;
const int INF = 1 << 30;
// const ll INF = (ll)1e18 + 1;

const int D = 60;
const int MAX_N = 200005;
int to[D][MAX_N];

int main() {
  int n;
  ll k;
  cin >> n >> k;
  rep(i, n) {
    cin >> to[0][i];
    to[0][i]--;
  }
  rep(i, D - 1) {
    rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; }
  }

  int v = 0;
  for (int i = D - 1; i >= 0; --i) {
    ll l = 1LL << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }
  cout << v + 1 << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1000000007;
const int INF = 1 << 30;
// const ll INF = (ll)1e18 + 1;

using Graph = vector<vector<int>>;
vector<bool> seen, finished;

// サイクル復元
ll pos = -1;
stack<ll> hist;

void dfs(const Graph &G, ll v, ll p) {
  seen[v] = true;
  hist.push(v);
  for (auto nv : G[v]) {
    if (nv == p)
      continue;
    if (finished[nv])
      continue;
    if (seen[nv] && !finished[nv]) {
      pos = nv;
      return;
    }
    dfs(G, nv, v);
    if (pos != -1)
      return;
  }
  hist.pop();
  finished[v] = true;
}
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> a(N);

  rep(i, N) {
    cin >> a[i];
    a[i]--;
  }

  Graph G(N);
  rep(i, N) { G[i].push_back(a[i]); }

  seen.assign(N, false), finished.assign(N, false);
  pos = -1;
  dfs(G, 0, -1);

  set<ll> cycle;
  while (!hist.empty()) {
    ll t = hist.top();
    cycle.insert(t);
    hist.pop();
    if (t == pos)
      break;
  }
  /*
  for (auto e: cycle){
    cout << e << " ";
  }
  cout << endl;*/

  // dist計算
  vector<ll> dist(N, -1);
  queue<ll> que;
  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    ll v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      if (dist[nv] != -1)
        continue;
      dist[nv] = dist[v] + 1;
      que.push(nv);
    }
  }

  vector<ll> cyc;
  for (auto e : cycle) {
    cyc.push_back(e);
  }
  ll n_cyc = cyc.size();
  ll d_min = INF;
  rep(i, n_cyc) d_min = min(d_min, dist[cyc[i]]);
  ll num_cyc = (K - d_min) % n_cyc;

  vector<P> cyc2;
  rep(i, n_cyc) cyc2.push_back(make_pair(dist[cyc[i]], cyc[i]));
  sort(cyc2.begin(), cyc2.end());
  // rep(i, cyc.size()) cout << cyc2[i].first << " " << cyc2[i].second << endl;
  cout << cyc2[num_cyc].second + 1 << endl;
}