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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const char wall = '#';
vector<bool> used;
vector<vector<ll>> dist;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {1, 0, -1, 0};

vector<ll> A;
// vectorのリサイズ
// A.resize(N);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
vector<ll> history;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll ans = 0;
  ll n, k;
  vector<vector<ll>> telepo;
  cin >> n >> k;
  telepo.resize(n);
  used.resize(n, false);
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  rep(i, n) { telepo[i].push_back(a[i]); }
  ll index = 0;
  used[index] = true;
  history.push_back(0);
  ll point = 0;
  ll loopLen = 0;
  rep(i, n) {
    ll next = telepo[index][0];
    if (!used[next]) {
      used[next] = true;
      history.push_back(next);
    } else {
      // rep(i,history.size()) {
      //   printf("%lld%c", history[i], (i == history.size()-1)?'\\n':' ');
      // }
      rep(j, int(history.size())) {
        if (history[j] == next) {
          point = j;
          loopLen = history.size() - j;
          break;
        }
      }
      // cout << "point: " << point << ", loopLen: " << loopLen<< endl;
      break;
    }
    index = next;
  }
  if (k - point < 0) {
    cout << history[k] + 1 << endl;
    return 0;
  }
  k -= point;
  ll ans_index = k % loopLen;
  // cout << "point: " << point << ", ans_index: " << ans_index<< endl;
  ans = history[point + ans_index] + 1;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const char wall = '#';
vector<bool> used;
vector<vector<ll>> dist;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {1, 0, -1, 0};

vector<ll> A;
// vectorのリサイズ
// A.resize(N);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
vector<ll> history;
ll ans = 0;
ll n, k;
vector<vector<ll>> telepo;
void bfs() {
  vector<vector<ll>> v = telepo;
  ll index = 0;
  used[index] = true;
  history.push_back(0);
  ll point = 0;
  ll loopLen = 0;
  rep(i, n) {
    ll next = v[index][0];
    if (!used[next]) {
      used[next] = true;
      history.push_back(next);
    } else {
      // rep(i,history.size()) {
      //   printf("%lld%c", history[i], (i == history.size()-1)?'\\n':' ');
      // }
      rep(j, history.size()) {
        if (history[j] == next) {
          point = j;
          loopLen = history.size() - j;
          break;
        }
      }
      // cout << "point: " << point << ", loopLen: " << loopLen<< endl;
      break;
    }
    index = next;
  }
  k -= point;
  ll ans_index = k % loopLen;
  // cout << "point: " << point << ", ans_index: " << ans_index<< endl;
  ans = history[point + ans_index] + 1;
  if (k <= point) {
    ans = history[k];
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  telepo.resize(n);
  used.resize(n, false);
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  rep(i, n) { telepo[i].push_back(a[i]); }
  bfs();
  cout << ans << endl;
  return 0;
}