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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF 1001001001
using ll = long long;
using dd = long double;

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;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF 1001001001
using ll = long long;
using dd = long double;

const int D = 60;
const int MAX_N = 20005;
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;

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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> data(N + 1);
  for (int i = 1; i < N + 1; i++)
    cin >> data.at(i);
  int a = 1, b = 1; // 循環を検出するための変数
  ll i = 0, ans = 1;
  int num = 0; // 循環がスタートするまでの数と循環してる個数
  while (a != b || i == 0) { // 追いつくところを計算
    if (i > K) {
      for (i = 0; i < K; i++)
        ans = data[ans];
      cout << ans << endl;
      return 0;
    }
    a = data[a];
    b = data[data[b]];
    i++;
  }
  b = 1;
  i = 0;           // 早い方を最初の位置に戻して
  while (a != b) { // ループ開始地点を算出
    a = data[a];
    b = data[b];
    i++; // ループまでの余計な移動数
  }
  K -= i;  // ループまでの余計な移動分減らす
  ans = a; // ループの開始地点からスタートさせる
  while (a != b || num == 0) { // ループ個数を算出
    a = data[a];
    b = data[data[b]];
    num++;
  }
  for (i = 0; i < K % num; i++) { // ループ回数で割った分だけ回す
    ans = data[ans];
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> data(N + 1);
  for (int i = 1; i < N + 1; i++)
    cin >> data.at(i);
  int a = 1, b = 1; // 循環を検出するための変数
  ll i = 0, ans;
  int num = 0; // 循環がスタートするまでの数と循環してる個数
  while (a != b || i == 0) { // 追いつくところを計算
    a = data[a];
    b = data[data[b]];
    i++;
    if (i > K) {
      for (i = 0; i < K; i++)
        ans = data[ans];
      cout << ans << endl;
      return 0;
    }
  }
  b = 1;
  i = 0;           // 早い方を最初の位置に戻して
  while (a != b) { // ループ開始地点を算出
    a = data[a];
    b = data[b];
    i++; // ループまでの余計な移動数
  }
  K -= i;  // ループまでの余計な移動分減らす
  ans = a; // ループの開始地点からスタートさせる
  while (a != b || num == 0) { // ループ個数を算出
    a = data[a];
    b = data[data[b]];
    num++;
  }
  cout << num << endl;
  for (i = 0; i < K % num; i++) { // ループ回数で割った分だけ回す
    ans = data[ans];
    cout << i << endl;
  }
  cout << i << " " << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef long long LL;
typedef long double LD;

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

  LL n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> road;
  vector<int> seen(n + 1, -1);
  int c = 1, l = 0;
  int pos = 1;
  while (seen[pos] == -1) {
    seen[pos] = road.size();
    road.push_back(pos);
    pos = a[pos - 1];
  }
  c = road.size() - seen[pos];
  l = seen[pos];
  if (k < l)
    cout << road[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << road[l + k] << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef long long LL;
typedef long double LD;

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

  LL n, k;
  cin >> n >> k;
  int a[100005];
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  vector<int> a_vis(n + 1, 0), loop(n);
  int pos = 1;
  int i = 0;
  int block;
  while (1) {
    loop[i] = pos;
    if (i == n) {
      cout << pos << endl;
      return 0;
    }
    if (a_vis[pos] == 1) {
      auto itr = find(loop.begin(), loop.end(), pos);
      block = distance(itr, loop.end());
      break;
    }
    a_vis[pos] = 1;
    pos = a[pos];
    i++;
  }
  k -= i + block;
  k %= block;
  cout << loop[k] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, b, n) for (int i = b; i < n; ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;

void solve(long N, ll K, vector<long> A) {
  long current = 0;
  long loop_length = 0;
  long loop_start = 0;
  vector<long> reachable;
  unordered_map<long, bool> already_reach;
  REP(i, N) {
    reachable.emplace_back(current);
    already_reach[current] = true;
    long next = A[current] - 1;
    // cout << current << " -> " << next << endl;
    if (already_reach[next]) {
      auto itr = find(reachable.cbegin(), reachable.cend(), next);
      loop_start = itr - reachable.cbegin();
      loop_length = i + 1 - loop_start;
      break;
    }
    current = next;
  }
  // cout << "loop_start" << loop_start << endl;
  // cout << "loop_length"<<loop_length << endl;

  long num = 0;
  if (K < loop_start)
    num = K;
  else
    num = loop_start + (K - loop_start) % loop_length;

  // current = 0;
  // REP(i, num) {
  //   long next = A[current] - 1;
  //   // cout << current << " -> " << next << endl;
  //   current = next;
  // }

  cout << reachable[num] + 1 << endl;
}

int main() {
  // int 3E4  long 2E9  ll 9E18
  cin.tie(0);
  ios::sync_with_stdio(false);

  long N;
  ll K;
  cin >> N >> K;

  vector<long> A(N);
  REP(i, N)
  cin >> A[i];

  solve(N, K, move(A));

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, b, n) for (int i = b; i < n; ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;

void solve(long N, ll K, vector<long> A) {
  long current = 0;
  long loop_length = 0;
  long loop_start = 0;
  vector<long> reachable;
  REP(i, N) {
    reachable.emplace_back(current);
    long next = A[current] - 1;
    // cout << current << " -> " << next << endl;
    loop_length = i + 1;
    auto itr = find(reachable.cbegin(), reachable.cend(), next);
    if (itr != reachable.cend()) {
      loop_start = itr - reachable.cbegin();
      break;
    }
    current = next;
  }
  loop_length -= loop_start;
  // cout << "loop_start" << loop_start << endl;
  // cout << "loop_length"<<loop_length << endl;

  long num = 0;
  if (K < loop_start)
    num = K;
  else
    num = loop_start + (K - loop_start) % loop_length;

  // current = 0;
  // REP(i, num) {
  //   long next = A[current] - 1;
  //   // cout << current << " -> " << next << endl;
  //   current = next;
  // }

  cout << reachable[num] + 1 << endl;
}

int main() {
  // int 3E4  long 2E9  ll 9E18
  cin.tie(0);
  ios::sync_with_stdio(false);

  long N;
  ll K;
  cin >> N >> K;

  vector<long> A(N);
  REP(i, N)
  cin >> A[i];

  solve(N, K, move(A));

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

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<vector<int>> next(60, vector<int>(N));
  for (int j = 0; j < N; j++) {
    cin >> next.at(0).at(j);
    next.at(0).at(j)--;
  }
  for (int i = 0; i < 59; i++) {
    for (int j = 0; j < N; j++) {
      next.at(i + 1).at(j) = next.at(i).at(next.at(i).at(j));
    }
  }
  int j = 0;
  for (int i = 0; i < 60; i++) {
    if (K & (1LL << i)) {
      j = next.at(i).at(j);
    }
  }
  cout << j + 1 << 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() {
  int N;
  long long K;
  cin >> N >> K;
  vector<vector<int>> next(60, vector<int>(N));
  for (int j = 0; j < N; j++) {
    cin >> next.at(0).at(j);
    next.at(0).at(j)--;
  }
  for (int i = 0; i < 59; i++) {
    for (int j = 0; j < N; j++) {
      next.at(i + 1).at(j) = next.at(i).at(next.at(i).at(j));
    }
  }
  int j = 0;
  for (int i = 0; j < 60; j++) {
    if (K & (1LL << i)) {
      j = next.at(i).at(j);
    }
  }
  cout << j + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  long int n, k;
  cin >> n >> k;
  vector<long int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  long int next = 1;
  vector<int> visited(n + 1, -1);
  vector<int> q;
  long int loopsize;
  for (int i = 0; i < k + 1; i++) {
    if (visited[next] != -1) {
      break;
    }
    visited[next] = q.size();
    q.push_back(next);
    next = a[next - 1];
  }

  if (k + 1 == q.size()) {
    cout << q[k];
  } else {
    loopsize = q.size() - visited[next];
    cout << q[visited[next] + (k - visited[next]) % loopsize];
  }

  // cout<<next;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  long int n, k;
  cin >> n >> k;
  vector<long int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  bool flag = false;
  long int next = 1;
  vector<int> q;
  q.push_back(next);
  long int loopsize;
  vector<long int> num;
  for (int i = 0; i < k; i++) {
    if (flag)
      break;
    next = a[next - 1];
    if (!q.empty()) {
      for (int j = 0; j < (int)q.size(); j++) {
        if (next == q[j]) {
          loopsize = q.size() - j;
          k -= j;
          for (int l = 0; l < loopsize; l++) {
            // cout<<q[j+l]<<endl;
            num.push_back(q[j + l]);
          }
          flag = true;
        }
      }
    }
    q.push_back(next);
  }

  cout << num[k % loopsize];

  // cout<<next;

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> A(n);
  vector<int> B(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < n; i++) {
    B.at(i + 1) = A.at(B.at(i)) - 1;
  }
  int terminate = B.at(n);
  int loop_start, loop;
  for (int i = 1; i < n + 1; i++) {
    if (B.at(n - i) == terminate) {
      loop_start = n - i;
      loop = i;
      break;
    }
  }
  if (k <= n) {
    cout << B.at(k) + 1 << "\\n";
  } else {
    cout << B.at((k - loop_start) % loop + loop_start) + 1 << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> A(n);
  vector<int> B(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  int start = -1, loop;
  for (int i = 0; i < n; i++) {
    B.at(i + 1) = A.at(B.at(i)) - 1;
    for (int j = 0; j < i + 1; j++) {
      if (B.at(j) == B.at(i + 1)) {
        start = j;
        loop = i + 1 - j;
        break;
      }
    }
    if (start != -1) {
      break;
    }
  }
  if (k <= start) {
    cout << B.at(k) + 1 << "\\n";
  } else {
    cout << B.at((k - start) % loop + start) + 1 << "\\n";
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long long n, k, now = 1, cnt, a[210000], pre[210000];

int main() {
  int i, j;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (i = 1, j = 1; !pre[i] && k; i = a[i], j++, k--)
    pre[i] = j;
  k = k % (j - pre[i]);
  for (; k; i = a[i], k--)
    ;
  cout << i << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int MAXN = 2 * 1e5 + 5;
int main() {
  long long N, K;
  int A[MAXN] = {0};
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<int> v;
  int now = 1;
  v.push_back(1);
  while (K > 0) {
    if (find(v.begin(), v.end(), A[now]) == v.end()) {
      v.push_back(A[now]);
      K--;
      now = A[now];
    } else {
      vector<int>::iterator it = find(v.begin(), v.end(), A[now]);
      int length = v.end() - it;
      if (K % length == 0)
        cout << *(it + length - 1);
      else
        cout << *(it + (K % length) - 1);
      return 0;
    }
  }
  cout << *(v.end() - 1);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int64_t N, K;
  cin >> N >> K;
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int64_t> memo(N, N + 1);
  int64_t X = 0;
  int64_t Y;
  int64_t Z;
  memo.at(0) = 0;
  for (int64_t i = 0; i < N; i++) {
    if (memo.at(A.at(X) - 1) != N + 1) {
      Y = i - memo.at(A.at(X) - 1) + 1;
      Z = memo.at(A.at(X) - 1);
      break;
    } else {
      memo.at(A.at(X) - 1) = i + 1;
      X = A.at(X) - 1;
    }
  }
  if (K >= Z) {
    for (int64_t i = 0; i < N; i++) {
      if (memo.at(i) == (K - Z) % Y + Z) {
        cout << i + 1 << endl;
        break;
      }
    }
  } else {
    int ans = 0;
    for (int i = 0; i < K; i++) {
      ans = A.at(ans) - 1;
    }
    cout << ans + 1 << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int ans = 0;
  for (int i = 0; i < K; i++) {
    ans = A.at(ans) - 1;
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
#define multi_test true and false
#define all(a) (a).begin(), (a).end()
using namespace std;

void dfs(int v, vector<bool> &bani, vector<int> &path, int arr[]) {
  if (bani[v]) {
    path.push_back(v);
    return;
  }
  bani[v] = true;
  path.push_back(v);
  dfs(arr[v], bani, path, arr);
}

void solve() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int &i : arr)
    cin >> i;

  for (int &i : arr)
    i--;

  vector<int> path;
  vector<bool> bani(n, false);
  dfs(0, bani, path, arr);

  int index = -1;
  for (int i = 0; i < (int)path.size(); i++) {
    if (path[i] == path.back()) {
      index = i;
      break;
    }
  }
  if (k < (int)path.size()) {
    cout << path[k] + 1 << endl;
    return;
  }
  vector<int> new_path(path.begin() + index, path.end());
  // for(int i : new_path) cout << i << ' ';
  // cout << endl ;
  k -= index;
  n = (int)new_path.size() - 1;
  k = k % n;
  //    cout << k << ' ' << n << endl ;
  cout << new_path[k] + 1 << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int test = 1;
  if (multi_test)
    cin >> test;
  while (test--)
    solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define multi_test true and false
#define all(a) (a).begin(), (a).end()
using namespace std;

void dfs(int v, vector<bool> &bani, vector<int> &path, int arr[]) {
  if (bani[v]) {
    path.push_back(v);
    return;
  }
  bani[v] = true;
  path.push_back(v);
  dfs(arr[v], bani, path, arr);
}

void solve() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int &i : arr)
    cin >> i;

  for (int &i : arr)
    i--;

  vector<int> path;
  vector<bool> bani(n, false);
  dfs(0, bani, path, arr);

  int index = -1;
  for (int i = 0; i < (int)path.size(); i++) {
    if (path[i] == path.back()) {
      index = i;
      break;
    }
  }
  vector<int> new_path(path.begin() + index, path.end());
  // for(int i : new_path) cout << i << ' ';
  // cout << endl ;
  k -= index;
  n = (int)new_path.size() - 1;
  k = k % n;
  //    cout << k << ' ' << n << endl ;
  cout << new_path[k] + 1 << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int test = 1;
  if (multi_test)
    cin >> test;
  while (test--)
    solve();

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int j, m = 0, n;
long long k, cnt;
int main() {
  cin >> n >> k;
  int a[n + 1], a1[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a1[i] = 0;
  }
  j = 1;
  while (a1[j] == 0) {
    a1[j] = m + 1;
    if (m == k) {
      cout << j;
      return 0;
    }
    j = a[j];
    m++;
  }
  cnt = (k - a1[j] + 1) % (m + 1 - a1[j]);
  while (cnt--) {
    j = a[j];
  }
  cout << j;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k, loc;
int main() {
  cin >> n >> k;
  int a[n];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int i = 0;
  loc = a[1];
  while (i < k - 1) {
    loc = a[loc];
    i++;
  }
  cout << loc;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  long long k;
  cin >> n >> k;
  vector<int> town(n + 1);
  vector<bool> visit(n + 1, false);
  for (int i = 1; i <= n; i++) {
    int to;
    cin >> to;
    town[i] = to;
  }
  int now = town[1];
  visit[now] = true;
  vector<int> loop;
  visit[1] = true;
  k--;
  while (k != 0) {
    now = town[now];

    if (!visit[now]) {
      visit[now] = true;
    } else {
      if (find(loop.begin(), loop.end(), now) != loop.end()) {
        k--;
        break;
      }
      loop.push_back(now);
    }
    k--;
  }
  if (k > 0) {
    long long pos = k % loop.size();
    // cout << "pos = " << k << " % " << loop.size() << "= " << pos << endl;
    // cout << loop[pos] << endl;
    // for (auto &i : loop)
    // {
    //     cout << i << " ";
    // }
    cout << loop[pos];
  } else
    cout << now;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  long long k;

  cin >> n >> k;
  long long tk = k;
  vector<int> town(n + 1);
  vector<bool> visit(n + 1, false);
  for (int i = 1; i <= n; i++) {
    int to;
    cin >> to;
    town[i] = to;
  }
  int now = town[1];
  vector<int> loop;
  visit[1] = true;
  k--;
  while (k != 0) {
    visit[now] = true;
    now = town[now];

    if (!visit[now]) {
      visit[now] = true;
    } else {
      if (find(loop.begin(), loop.end(), now) != loop.end()) {
        k--;
        break;
      }
      loop.push_back(now);
    }

    k--;
  }
  if (k > 0) {
    long long pos = k % loop.size();
    // cout << "pos = " << k << " % " << loop.size() << "= " << pos << endl;
    // cout << loop[pos] << endl;
    // for (auto &i : loop)
    // {
    //     cout << i << " ";
    // }
    cout << loop[pos];
  } else
    cout << now;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int N = 2e5 + 7;
const int K = 62;

int up[K][N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, nd;
  cin >> n >> nd;
  for (int i = 0; i < n; i++) {
    cin >> up[0][i];
    up[0][i]--;
  }
  for (int i = 0; i + 1 < K; i++) {
    for (int j = 0; j < n; j++) {
      up[i + 1][j] = up[i][up[i][j]];
    }
  }
  int p = 0;
  for (int i = 0; i < K; i++) {
    if ((nd >> i) & 1)
      p = up[i][p];
  }
  cout << p + 1 << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int N = 1e5 + 7;
const int K = 62;

int up[K][N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, nd;
  cin >> n >> nd;
  for (int i = 0; i < n; i++) {
    cin >> up[0][i];
    up[0][i]--;
  }
  for (int i = 0; i + 1 < K; i++) {
    for (int j = 0; j < n; j++) {
      up[i + 1][j] = up[i][up[i][j]];
    }
  }
  int p = 0;
  for (int i = 0; i < K; i++) {
    if ((nd >> i) & 1)
      p = up[i][p];
  }
  cout << p + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define pb push_back
#define COUT(x) cout << (x) << endl
#define ENDL cout << endl
#define debug(x) cout << "[debug] " << #x << ": " << x << endl
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1000000000;

int main() {
  ll N, K;
  cin >> N >> K;
  int a[N];

  rep(i, N) {
    int aa;
    cin >> aa;
    a[i] = aa;
  }

  map<int, int> m;
  rep(i, N) { m[i] = INF; }
  vector<int> v;

  m[0] = 0;
  int from = 0;
  int i = 0;
  v.pb(0);
  while (1) {
    int to = a[from] - 1;
    v.pb(to);
    if (m[to] == INF) {
      m[to] = i + 1;
    } else {
      int amari = m[to];
      int roop = i + 1 - amari;
      if (amari >= K) {
        COUT(v[K] + 1);
      } else {
        ll roop_index = (K - amari) % roop;
        COUT(v[amari + roop_index] + 1);
      }
      break;
    }
    from = to;
    i++;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define pb push_back
#define COUT(x) cout << (x) << endl
#define ENDL cout << endl
#define debug(x) cout << "[debug] " << #x << ": " << x << endl
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1000000000;

int main() {
  int N, K;
  cin >> N >> K;
  int a[N];

  rep(i, N) {
    int aa;
    cin >> aa;
    a[i] = aa;
  }

  int from = 0;
  rep(i, K) { from = a[from - 1]; }
  COUT(from + 1);
  return 0;
}