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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(i) i.begin(), i.end()
template <typename T = ll> using v = vector<T>;
template <typename T = ll> using vv = vector<v<T>>;
template <typename T = ll, typename U = ll> using p = pair<T, U>;
const ll INF = (ll)1e9;
const ll MOD = INF + 7;
template <typename T> T chmin(T &a, T b) { a = min(a, b); }
template <typename T> T chmax(T &a, T b) { a = max(a, b); }

void a() {
  string s, t;
  cin >> s >> t;
  if (s == t.substr(0, t.size() - 1)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

void b() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll count = 0;

  rep(i, a) {
    if (k <= 0)
      break;
    k--;
    count++;
  }

  rep(i, b) {
    if (k <= 0)
      break;
    k--;
  }

  rep(i, c) {
    if (k <= 0)
      break;
    k--;
    count--;
  }

  cout << count << endl;
}

void c() {
  ll N, M, X;
  cin >> N >> M >> X;
  auto C = v<>(N, 0);
  auto A = vv<>(N, v<>(M, 0));
  rep(i, N) {
    cin >> C[i];
    rep(j, M) cin >> A[i][j];
  }
  ll _min = INF;
  rep(i, 1 << N) {
    cout << bitset<3>(i) << ":";
    for (ll ii = 0; (1 << ii) < i; i++) {
      if ((i >> ii) & 1)
        cout << ii << " ";
    }
    cout << endl;
  }
  cout << _min << endl;
}

void d() {
  ll N, K;
  cin >> N >> K;
  auto A = v<>(N, 0);
  auto B = v<bool>(N, false);
  auto C = v<p<>>();
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  auto s = p<>(0, A[0]);
  ll i = 0;
  while (K) {
    if (B[s.first])
      break;
    B[s.first] = true;
    C.push_back(s);
    s.first = A[s.first];
    s.second = A[s.first];
    K--;
    i++;
  }
  if (!K)
    cout << s.first + 1 << endl;
  else {
    auto it = find(C.begin(), C.end(), s);
    auto off = it - C.begin();
    cout << C[off + K % (C.size() - off)].first + 1 << endl;
  }
}

int main() {
  d();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(i) i.begin(), i.end()
template <typename T = ll> using v = vector<T>;
template <typename T = ll> using vv = vector<v<T>>;
template <typename T = ll, typename U = ll> using p = pair<T, U>;
const ll INF = (ll)1e9;
const ll MOD = INF + 7;
template <typename T> T chmin(T &a, T b) { a = min(a, b); }
template <typename T> T chmax(T &a, T b) { a = max(a, b); }

void a() {
  string s, t;
  cin >> s >> t;
  if (s == t.substr(0, t.size() - 1)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

void b() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll count = 0;

  rep(i, a) {
    if (k <= 0)
      break;
    k--;
    count++;
  }

  rep(i, b) {
    if (k <= 0)
      break;
    k--;
  }

  rep(i, c) {
    if (k <= 0)
      break;
    k--;
    count--;
  }

  cout << count << endl;
}

void c() {
  ll N, M, X;
  cin >> N >> M >> X;
  auto C = v<>(N, 0);
  auto A = vv<>(N, v<>(M, 0));
  rep(i, N) {
    cin >> C[i];
    rep(j, M) cin >> A[i][j];
  }
  ll _min = INF;
  rep(i, 1 << N) {
    cout << bitset<3>(i) << ":";
    for (ll ii = 0; (1 << ii) < i; i++) {
      if ((i >> ii) & 1)
        cout << ii << " ";
    }
    cout << endl;
  }
  cout << _min << endl;
}

void d() {
  ll N, K;
  cin >> N >> K;
  auto A = v<>(N, 0);
  auto B = v<bool>(N, false);
  auto C = v<>();
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  ll s = 0;
  while (K) {
    if (B[s])
      break;
    B[s] = true;
    C.push_back(s);
    s = A[s];
    K--;
  }
  if (!K)
    cout << s + 1 << endl;
  else
    cout << C[s + K % C.size()] + 1 << endl;
}

int main() {
  d();
  return 0;
}