alexjercan/bugnet huggingface dataset:
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define pb push_back
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int MOD = 1000000007;
const int INF = 1e9;
#define PI 3.14159265369;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int roop, pre;
  vector<int> v(n + 1, -1);
  v[1] = 0;
  vector<int> r;
  r.push_back(1);
  int now = 1;
  int cnt = 0;
  while (1) {
    cnt++;
    int next = a[now - 1];
    if (v[next] == -1) {
      v[next] = cnt;
      r.push_back(next);
      now = next;
    } else {
      roop = cnt - v[next];
      pre = v[next];
      break;
    }
  }
  if (k < pre)
    cout << r[k] << endl;
  else {
    int ans = (k - pre) % roop + pre;
    cout << r[ans] << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define pb push_back
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int MOD = 1000000007;
const int INF = 1e9;
#define PI 3.14159265369;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  if (a[0] == 0) {
    cout << 1 << endl;
    return 0;
  }
  ll now = 0;
  vector<bool> visited(n, false);
  visited[0] = true;
  ll last;
  vector<ll> v;
  while (1) {
    int next = a[now];
    if (!visited[next]) {
      v.push_back(next);
      now = next;
      visited[next] = true;
    } else {
      last = next;
      break;
    }
  }
  ll r;
  for (ll i = 0; i < (ll)v.size(); i++) {
    if (v[i] == last) {
      r = i;
    }
  }
  if (k < (ll)v.size()) {
    cout << v[k] << endl;
    return 0;
  } else {
    if (a[(int)v[v.size() - 1]] == (int)v[v.size() - 1]) {
      cout << v[v.size() - 1] << endl;
      return 0;
    }
    ll ans = (k - r) % (v.size() - r) + r - 1;
    if (ans == r - 1)
      ans = v.size() - 1;
    cout << v[ans] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t n, k;
  cin >> n >> k;

  vector<int> A(n);

  for (int p = 0; p < n; p++) {
    cin >> A.at(p);
  }

  vector<int> B(n);
  B.at(0) = 1;

  set<int> S;
  S.insert(1);

  int q = 0;
  int i = 1;
  while (q == 0) {
    B.at(i) = A.at(B.at(i - 1) - 1);
    if (S.count(B.at(i))) {
      q++;
    }
    S.insert(B.at(i));
    i++;
  }

  int j = 0;
  int m = 0;
  while (m == 0) {
    if (B.at(j) == B.at(i - 1)) {
      m++;
    }
    j++;
  }

  if (k >= i) {
    cout << B.at(j + ((k - j) % (i - j))) << endl;
  }

  else {
    cout << B.at(k) << endl;
  }
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t n, k;
  cin >> n >> k;

  vector<int> A(n);

  for (int p = 0; p < n; p++) {
    cin >> A.at(p);
  }

  vector<int> B(n);
  B.at(0) = 1;

  set<int> S;
  S.insert(1);

  int q = 0;
  int i = 1;
  while (q == 0) {
    B.at(i) = A.at(B.at(i - 1) - 1);
    if (S.count(B.at(i))) {
      q++;
    }
    S.insert(B.at(i));
    i++;
  }

  cout << B.at(i) << endl;

  int j = 0;
  int m = 0;
  while (m == 0) {
    if (B.at(j) == B.at(i)) {
      m++;
    }
    j++;
  }

  if (k >= i) {
    cout << B.at(j + ((k - j) % (i - j))) << endl;
  }

  else {
    cout << B.at(k) << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  long long n, k;
  cin >> n >> k;
  int a[n];
  rep(i, n) cin >> a[i];
  vector<int> s;
  vector<int> ord(n + 1, -1);
  int v = 1, d = 1, e = 0; // d周期、e残り
  while (ord[v] == -1) {
    s.push_back(v);
    ord[v] = s.size();
    v = a[v - 1];
  }
  e = ord[v] - 1;
  d = s.size() - e;
  if (k < e)
    cout << s[k] << endl;
  else
    cout << s[(k - e) % d + e] << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  long n, k;
  cin >> n >> k;
  long a[n];
  rep(i, n) cin >> a[i];
  long now = 1;
  vector<int> d;
  for (int i = 0; i < n; i++) {
    d.push_back(now);
    int c = now;
    now = a[c - 1];
    if (now == 1)
      break;
  }
  if (now != 1) {
    int g = now;
    for (int i = 0; i < n; i++) {
      d.push_back(now);
      int c = now;
      now = a[c - 1];
      if (now == g)
        break;
    }
    int l = (k - n) % (d.size() - n);
    cout << d[l] << endl;
    return 0;
  }
  int m = k % (d.size() - 1);
  cout << d[m] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int maxlog = 62, maxn = 200005;
int n;
long long k, pow2[maxlog + 1] = {1};
int d[maxn][maxlog + 1];

int main() {
  for (int i = 1; i <= maxlog; i++)
    pow2[i] = pow2[i - 1] + pow2[i - 1];
  scanf("%d%lld", &n, &k);
  int q = 1;
  for (int i = 1; i <= n; i++)
    scanf("%d", &d[i][0]);
  for (int i = 1; i <= maxlog; i++)
    for (int j = 1; j <= n; j++)
      d[j][i] = d[d[j][i - 1]][i - 1];
  while (q--) {
    int p = maxlog, pos = 1;
    while (k > 0) {
      while (pow2[p] > k)
        p--;
      pos = d[pos][p];
      k -= pow2[p];
    }
    printf("%d\\n", pos);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, pos, tot = 0;
long long k;
vector<int> v;
int a[200005];
int vis[200005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  pos = 1;
  while (true) {
    v.push_back(pos);
    tot++;
    vis[pos] = tot;
    pos = a[pos];
    if (vis[pos])
      break;
  }
  int id = vis[pos] - 1;
  v.erase(v.begin(), v.begin() + id);
  int ind = (k - (long long)id) % (long long)(v.size());
  cout << v[ind] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;

ll k;
int main() {

  cin >> n >> k;
  vector<int> a(n);
  vector<int> m;
  vector<bool> seen(n, false);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  int now = 0;
  ll b;
  ll MOD;
  while (m.size() < n + 1) {
    if (m.size() == n) {
      b = 0;
      MOD = n;
    }
    if (seen[now]) {
      for (ll i = 0; i < m.size(); i++) {
        if (m[i] == now) {
          b = i;
          MOD = m.size() - i;
          break;
        }
      }
      break;
    }
    seen[now] = true;
    m.push_back(now);
    now = a[now];
  }
  int ans;
  if (k < b) {
    ans = m[k] + 1;
  } else {
    k = (k - b) % MOD;
    ans = m[b + k] + 1;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;

ll k;
int main() {

  cin >> n >> k;
  vector<int> a(n);
  vector<int> m;
  vector<bool> seen(n, false);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  int now = 0;
  ll b;
  ll MOD;
  while (true) {
    if (seen[now]) {
      for (int i = 0; i < m.size(); i++) {
        if (m[i] == now) {
          b = m.size() - i - 1;
          MOD = m.size() - i;
          break;
        }
      }

      break;
    }
    seen[now] = true;
    m.push_back(now);
    now = a[now];
  }
  int ans;
  if (k < b) {
    ans = m[k] + 1;
  } else {
    k = (k - b) % MOD;
    ans = m[b + k] + 1;
  }
  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  if (k < n) {
    long long ki = 0, p = 0;
    while (ki < k) {
      p = a[p];
      ki++;
    }
    cout << p + 1 << endl;
    return 0;
  }

  set<int> go;
  long long ki = 0, p = 0;
  go.insert(0);
  while (ki < k) {
    p = a[p];
    ki++;
    if (go.count(p)) {
      break;
    }
    go.insert(p);
  }

  long long pp = p;
  ki = 0;
  p = 0;
  long long sr = 0;
  if (pp != p) {
    while (ki < k) {
      p = a[p];
      ki++;
      if (p == pp) {
        sr = ki;
        break;
      }
    }
  }

  k -= sr;

  ki = 0;
  p = pp;
  long long r = 0;
  while (ki < k) {
    p = a[p];
    ki++;
    if (p == pp) {
      r = ki;
      break;
    }
  }

  long long er = k % r;
  ki = 0;
  p = pp;
  while (ki < er) {
    p = a[p];
    ki++;
  }

  cout << p + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  set<int> go;
  long long ki = 0, p = 0;
  go.insert(0);
  while (ki < k) {
    p = a[p];
    ki++;
    if (go.count(p)) {
      break;
    }
    go.insert(p);
  }

  long long pp = p;
  ki = 0;
  p = 0;
  long long sr = 0;
  if (pp != p) {
    while (ki < k) {
      p = a[p];
      ki++;
      if (p == pp) {
        sr = ki;
        break;
      }
    }
  }
  if (k - sr < 0) {
    long long kj = 0;
    while (kj < k) {
      p = a[p];
      kj++;
    }
    cout << p + 1 << endl;
    return 0;
  }

  k -= sr;

  ki = 0;
  p = pp;
  long long r = 0;
  while (ki < k) {
    p = a[p];
    ki++;
    if (p == pp) {
      r = ki;
      break;
    }
  }

  long long er = k % r;
  ki = 0;
  p = pp;
  while (ki < er) {
    p = a[p];
    ki++;
  }

  cout << p + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<ll> b(n, -1);
  vector<ll> c;
  b[0] = 0;
  ll now = 0;
  ll t = 0;
  ll s = 0;
  while (true) {
    c.push_back(now);
    if (b[a[now]] >= 0) {
      s = b[a[now]];
      t = b[now] - b[a[now]] + 1;
      break;
    }
    b[a[now]] = b[now] + 1;
    now = a[now];
  }

  if (k < c.size()) {
    cout << c[k] + 1 << endl;
    return 0;
  }
  cout << c[(k - s) % t + s] + 1 << endl;

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<ll> b(n, -1);
  vector<ll> c;
  b[0] = 0;
  ll now = 0;
  ll t = 0;
  ll s = 0;
  while (true) {
    c.push_back(now);
    if (b[a[now]] >= 0) {
      s = b[a[now]];
      t = b[now] - b[a[now]] + 1;
      break;
    }
    b[a[now]] = b[now] + 1;
    now = a[now];
  }

  if (k < t) {
    cout << c[k] << endl;
    return 0;
  }
  k -= s;
  cout << c[k % (t - s) + s] + 1 << endl;

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;

ll n, k;
ll a[200005];
ll b[200005];

int main() {
  scanf("%lld%lld", &n, &k);
  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  ll cnt = 1;
  ll i;
  for (i = 1; b[i] == 0; i = a[i], cnt++) {
    b[i] = cnt;
    k--;
    if (k < 0) {
      printf("%lld\\n", i);
      return 0;
    }
  }
  ll x = cnt - b[i];
  k %= x;
  while (k > 0) {
    i = a[i];
    k--;
  }
  printf("%lld\\n", i);
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;

ll n, k;
ll a[100005];
ll b[100005];

int main() {
  scanf("%lld%lld", &n, &k);
  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  ll cnt = 1;
  ll i;
  for (i = 1; b[i] == 0; i = a[i], cnt++) {
    b[i] = cnt;
    k--;
    if (k <= 0) {
      printf("%lld\\n", i);
      return 0;
    }
  }
  ll x = cnt - b[i];
  k %= x;
  while (k > 0) {
    i = a[i];
    k--;
  }
  printf("%lld\\n", i);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
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;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> vec(N + 1);
  REP(i, N + 1) vec[i] = 0;
  vector<ll> path;
  ll tmp = 1;
  vec[0] = 1;
  ll ans = 0;
  path.push_back(vec[0]);
  ll cnt = 0;
  for (ll i = 0; i < K; ++i) {
    if (vec[tmp] == 0) {
      vec[tmp] = A[tmp - 1];
      tmp = A[tmp - 1];
      path.push_back(tmp);
      cnt++;
    } else if (vec[tmp] != 0) {
      // なんらかの処理
      ans = tmp;
      break;
    }
  }

  vector<ll> b;
  bool flag = false;
  REP(i, path.size()) {
    if (path[i] == ans)
      flag = !flag;
    if (flag == true)
      b.push_back(path[i]);
  }

  if (K - (cnt - b.size()) <= 0) {
    cout << path[K] << endl;
  } else {
    ll c = (K - (cnt - b.size())) % b.size();
    cout << b[c] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
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;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> vec(N + 1);
  REP(i, N + 1) vec[i] = 0;
  vector<ll> path;
  ll tmp = 1;
  vec[0] = 1;
  ll ans = 0;
  path.push_back(vec[0]);
  ll cnt = 0;
  for (ll i = 0; i < K; ++i) {
    if (vec[tmp] == 0) {
      vec[tmp] = A[tmp - 1];
      tmp = A[tmp - 1];
      path.push_back(tmp);
      cnt++;
    } else if (vec[tmp] != 0) {
      // なんらかの処理
      ans = tmp;
      break;
    }
  }

  vector<ll> b;
  bool flag = false;
  REP(i, path.size()) {
    if (path[i] == ans)
      flag = !flag;
    if (flag == true)
      b.push_back(path[i]);
  }
  cout << b[(K - (cnt - b.size())) % b.size()] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  long K;
  cin >> N >> K;

  int A[N];
  int visited[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
    visited[i] = -1;
  }

  int presentPoint = 0;
  int nextPoint = -1;
  vector<int> loop = {0};
  int loopstart = -1;
  int loopsize = -1;
  for (int i = 0; i < K; i++) {
    nextPoint = A[presentPoint];
    if (visited[nextPoint] != -1) {
      loopstart = visited[nextPoint];
      loopsize = i - visited[nextPoint];
      break;
    }
    presentPoint = A[presentPoint];
    visited[presentPoint] = i;
    loop.emplace_back(presentPoint);
  }

  int remaincount = (K - loopstart) % loopsize;
  if (remaincount != 0) {
    presentPoint = loop[loopstart + remaincount];
  }

  cout << presentPoint + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <vector>
#define fastIO ios::sync_with_stdio(0), cin.tie(0)

using namespace std;

int main() {
  int N;
  long K;
  cin >> N >> K;

  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int presentPoint = 1;
  int nextPoint = -1;
  vector<int> loop = {1};
  int loopstart = -1;
  int loopsize = -1;
  for (int i = 0; i < K; i++) {
    nextPoint = A[presentPoint - 1];
    for (int j = 0; j < loop.size() - 1; j++) {
      if (loop[j] == nextPoint) {
        loopstart = j;
        loopsize = i - j + 1;
        break;
      }
    }
    presentPoint = A[presentPoint - 1];
    loop.emplace_back(presentPoint);
    if (loopstart != -1 && loopsize != -1) {
      break;
    }
  }
  int remaincount = (K - loopstart) % loopsize;
  if (remaincount != 0) {
    presentPoint = loop[loopstart + remaincount];
  }

  cout << presentPoint << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// cand <genesis?>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll base = 7;
const ll N = 3e5 + 9;
const ll inf = 1000100009;
typedef pair<ll, ll> LL;

ll n, k, a[N], i, chk[N];
vector<ll> pos;
ll getpos(ll x) {
  for (ll i = 0; i < pos.size(); i++)
    if (pos[i] == x)
      return i;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  // freopen("test.INP", "r", stdin);
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  i = 1;
  while (chk[i] != 1) {
    // cout<<i<<"x\\n";
    chk[i] = 1;
    pos.push_back(i);
    i = a[i];
  }
  ll p = getpos(i);
  // cout<<i<<"\\n";
  if (k <= p)
    cout << pos[k];
  else {
    k -= p;
    k %= (pos.size() - p);
    cout << pos[p + k];
  }
}


Error: WA

Fixed submission:

// cand <genesis?>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll base = 7;
const ll N = 1e5 + 9;
const ll inf = 1000100009;
typedef pair<ll, ll> LL;

ll n, k, a[N], i, chk[N];
vector<ll> pos;
ll getpos(ll x) {
  for (ll i = 0; i < pos.size(); i++)
    if (pos[i] == x)
      return i;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  // freopen("test.INP", "r", stdin);
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  i = 1;
  while (chk[i] != 1) {
    // cout<<i<<"x\\n";
    chk[i] = 1;
    pos.push_back(i);
    i = a[i];
  }
  ll p = getpos(i);
  // cout<<i<<"\\n";
  if (k <= p)
    cout << pos[k];
  else {
    k -= p;
    k %= (pos.size() - p);
    cout << pos[p + k];
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  vector<ll> loop(N + 1);
  loop[0] = 0;
  for (int i = 0; i < N; i++) {
    loop[i + 1] = A[loop[i]];
  }
  if (N >= K) {
    cout << loop[K] + 1 << "\\n";
    return 0;
  }
  vector<ll> stop(N, -1);
  stop[0] = 0;
  ll start, end;
  for (int i = 0; i <= N; i++) {
    if (stop[loop[i]] == -1) {
      stop[loop[i]] = i + 1;
    } else {
      start = stop[loop[i]];
      end = i + 1;
    }
  }
  K -= start;
  ll res = K % (end - start);
  ll ans = loop[start + res] + 1;
  cout << ans << "\\n";
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  map::at


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define FOR(i, n) for (int i = 0; i < n; i++)

int main() {
  float n, k;
  cin >> n >> k;
  int ans = 0;
  map<int, int> a;

  for (int i = 0; i < n; i++) {
    int z;
    cin >> z;
    a[i + 1] = z;
  }
  int s = a[1];
  float cnt = 0;
  float g = 0;

  for (int i = 1; i < n + 1; i++) {
    cnt = 0;
    for (int j = 0; j < i; j++) {
      if (i != j) {
        if (a.at(s) == a.at(j)) {
          g = i - j;
          break;
        }
      }
      cnt++;
    }
    if (cnt != i) {
      break;
    }
  }
  s = a[1];
  if (g == 0) {
    for (int i = 0; i < n; i++) {
      s = a.at(s);
    }

  } else {
    k = k - cnt;
    k = fmod(k, g);
    s = a[cnt + k];
  }

  cout << s << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long unsigned int n;
  long long k;
  cin >> n >> k;
  int r1 = 0, r2 = 0, r3 = 0, t = 0;
  bool shit = 0;
  set<int> s;
  vector<int> v(n);
  vector<int> r(n);
  for (long unsigned int i = 0; i < n; i++)
    cin >> v[i];
  for (long unsigned int i = 0; i < n; i++) {
    if (i)
      t = v[t - 1];
    else
      t = v[0];
    s.insert(t);
    if (s.size() == i) {
      r1 = i;
      r2 = t;
      shit = 1;
      break;
    }
    r[i] = t;
  }
  if (shit)
    for (long unsigned int i = 0; i < n; i++)
      if (r[i] == r2)
        r3 = i;
  if (!shit) {
    if (k % n)
      cout << r[k % n - 1];
    else
      cout << r[n - 1];
    return 0;
  }
  if (k <= r3)
    cout << r[k - 1];
  else
    cout << r[(k - r3 - 1) % (r1 - r3) + r3];
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long unsigned int n;
  long long k;
  cin >> n >> k;
  int r1 = 0, r2 = 0, r3 = 0;
  bool shit = 0;
  set<int> s;
  vector<int> v(n);
  vector<int> r(n);
  for (long unsigned int i = 0; i < n; i++)
    cin >> v[i];
  for (long unsigned int i = 0; i < n; i++) {
    int t;
    if (i)
      t = v[t - 1];
    else
      t = v[0];
    s.insert(t);
    if (s.size() == i) {
      r1 = i;
      r2 = t;
      shit = 1;
      break;
    }
    r[i] = t;
  }
  if (shit)
    for (long unsigned int i = 0; i < n; i++)
      if (r[i] == r2)
        r3 = i;
  if (!shit) {
    if (k % n)
      cout << r[k % n - 1];
    else
      cout << r[n - 1];
    return 0;
  }
  if (k <= r3)
    cout << r[k - 1];
  else
    cout << r[(k - r3 - 1) % (r1 - r3) + r3];
  return 0;
}