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

#include <algorithm>
#include <cstdbool>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

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

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<ll, ll> P;
ll ma = 1000000000 + 7;
ll mx = 1000003;
ll h, n, w, m, t;
string s;
ll R, C;
ll sy, sx, gy, gx;
char maze[510][510];
ll dis[20][20];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
/*void comb(vector<vector <ll> >& v) {
        for (ll i = 0; i < v.size(); i++) {
                v[i][0] = 1;
                v[i][i] = 1;
        }
        for (ll k = 1; k < v.size(); k++) {
                for (int j = 1; j < k; j++) {
                        v[k][j] = (v[k - 1][j - 1] + v[k - 1][j])%ma;
                }
        }
}*/

ll GetDigit(ll num) { return log10(num) + 1; }
ll Combination(int n, int r) {
  if (n - r < r)
    r = n - r;
  if (r == 0)
    return 1;
  if (r == 1)
    return n;

  vector<ll> numerator(r);
  vector<ll> denominator(r);

  for (int k = 0; k < r; k++) {
    numerator[k] = n - r + k + 1;
    denominator[k] = k + 1;
  }

  for (int p = 2; p <= r; p++) {
    int pivot = denominator[p - 1];
    if (pivot > 1) {
      int offset = (n - r) % p;
      for (int k = p - 1; k < r; k += p) {
        numerator[k - offset] /= pivot;
        denominator[k] /= pivot;
      }
    }
  }

  ll result = 1;
  for (int k = 0; k < r; k++) {
    if (numerator[k] > 1)
      result *= numerator[k];
    result %= ma;
  }

  return result;
}

ll bfs() {
  queue<P> que;
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++)
      dis[i][j] = INF;
  }
  que.push(P(sy - 1, sx - 1));
  dis[sy - 1][sx - 1] = 0;

  while (que.size() > 0) {
    P p = que.front();
    que.pop();
    if (p.first == gy - 1 && p.second == gx - 1)
      break;
    for (ll i = 0; i < 4; i++) {
      ll ny = p.first + dy[i];
      ll nx = p.second + dx[i];
      if (0 <= ny && ny < R && 0 <= nx && nx < C && maze[ny][nx] == '.' &&
          dis[ny][nx] == INF) {
        que.push(P(ny, nx));
        dis[ny][nx] = dis[p.first][p.second] + 1;
      }
    }
  }
  return dis[gy - 1][gx - 1];
}

int main() {
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> vis(n, 0);
  ll p = 0;
  rep(i, n) {
    cin >> a[i];
    --a[i];
  }
  if (k <= 1000000000) {
    ll tt = 0;
    rep(i, k) { tt = a[tt]; }
    cout << tt + 1 << endl;
    return 0;
  }
  vis[0] = 1;
  ll cnt = 0;
  ll roop = 0;
  vector<ll> rop(2 * n);
  ll i = 0;
  ll tmp = 0;
  while (1) {
    tmp = p;
    p = a[p];
    if (roop > 0 && vis[p] == -1) {
      break;
    }
    if (vis[p] == 0) {
      vis[p] = 1;
    } else {
      ++roop;
      vis[p] = -1;
      rop[i] = p + 1;
      ++i;
    }
  }
  rep(i, n) {
    if (vis[i] == 1) {
      ++cnt;
    }
  }
  k -= cnt;
  ll res = rop[k % roop];
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdbool>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

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

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<ll, ll> P;
ll ma = 1000000000 + 7;
ll mx = 1000003;
ll h, n, w, m, t;
string s;
ll R, C;
ll sy, sx, gy, gx;
char maze[510][510];
ll dis[20][20];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
void comb(vector<vector<ll>> &v) {
  for (ll i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (ll k = 1; k < v.size(); k++) {
    for (int j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % ma;
    }
  }
}

ll GetDigit(ll num) { return log10(num) + 1; }
ll Combination(int n, int r) {
  if (n - r < r)
    r = n - r;
  if (r == 0)
    return 1;
  if (r == 1)
    return n;

  vector<ll> numerator(r);
  vector<ll> denominator(r);

  for (int k = 0; k < r; k++) {
    numerator[k] = n - r + k + 1;
    denominator[k] = k + 1;
  }

  for (int p = 2; p <= r; p++) {
    int pivot = denominator[p - 1];
    if (pivot > 1) {
      int offset = (n - r) % p;
      for (int k = p - 1; k < r; k += p) {
        numerator[k - offset] /= pivot;
        denominator[k] /= pivot;
      }
    }
  }

  ll result = 1;
  for (int k = 0; k < r; k++) {
    if (numerator[k] > 1)
      result *= numerator[k];
    result %= ma;
  }

  return result;
}

ll bfs() {
  queue<P> que;
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++)
      dis[i][j] = INF;
  }
  que.push(P(sy - 1, sx - 1));
  dis[sy - 1][sx - 1] = 0;

  while (que.size() > 0) {
    P p = que.front();
    que.pop();
    if (p.first == gy - 1 && p.second == gx - 1)
      break;
    for (ll i = 0; i < 4; i++) {
      ll ny = p.first + dy[i];
      ll nx = p.second + dx[i];
      if (0 <= ny && ny < R && 0 <= nx && nx < C && maze[ny][nx] == '.' &&
          dis[ny][nx] == INF) {
        que.push(P(ny, nx));
        dis[ny][nx] = dis[p.first][p.second] + 1;
      }
    }
  }
  return dis[gy - 1][gx - 1];
}

int main() {
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> vis(n, 0);
  ll p = 0;
  rep(i, n) {
    cin >> a[i];
    --a[i];
  }
  vis[0] = 1;
  ll cnt = 0;
  ll roop = 0;
  vector<ll> rop(2 * n);
  ll i = 0;
  ll tmp = 1;
  while (1) {
    p = a[p];
    if (roop > 0 && vis[p] == -1) {
      break;
    }
    if (vis[p] == 0) {
      vis[p] = 1;
      ++tmp;
    } else {
      ++roop;
      vis[p] = -1;
      rop[i] = p + 1;
      ++i;
    }
  }
  rep(i, n) {
    if (vis[i] == 1) {
      ++cnt;
    }
  }
  k -= cnt;
  ll res = rop[k % roop];
  cout << res << endl;
}