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

#include <bits/stdc++.h>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
using ll = long long;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1);
  for (ll i = 1; i <= N; i++) {
    cin >> A.at(i);
  }
  ll num = 1;
  ll sum = 0;
  vector<ll> tele(N + 1, -1), kako(N + 1, -1);
  tele.at(0) = 1;
  kako.at(1) = 0;
  num = A.at(num);
  bool a = true;
  while (kako.at(num) < 0) {
    kako.at(num) = sum + 1;
    tele.at(sum) = num;
    num = A.at(num);
    sum = sum + 1;
    if (sum == K) {
      a = false;
      break;
    }
  }
  if (a) {
    tele.at(sum) = num;
    ll c = kako.at(num);
    ll d = sum + 1 - c;
    ll e = K - c;
    ll f = e % d;
    if (f == 0)
      f = d;
    ll amari = f + c - 1;
    cout << tele.at(amari) << endl;
  } else {
    tele.at(sum) = num;
    cout << tele.at(sum - 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
using ll = long long;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1);
  for (ll i = 1; i <= N; i++) {
    cin >> A.at(i);
  }
  ll num = 1;
  ll sum = 0;
  vector<ll> tele(N + 1, -1), kako(N + 1, -1);
  tele.at(0) = 1;
  kako.at(1) = 0;
  num = A.at(num);
  while (kako.at(num) < 0) {
    kako.at(num) = sum + 1;
    tele.at(sum) = num;
    num = A.at(num);
    sum = sum + 1;
  }
  tele.at(sum) = num;
  ll c = kako.at(num);
  ll d = sum + 1 - c;
  ll e = K - c;
  ll f = e % d;
  ll amari = f + c - 1;
  cout << tele.at(amari) << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define enld '\\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[6] = {1, 0, -1, 0, 1, 1};
const int dy[6] = {0, 1, 0, -1, 1, -1};
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;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<ll> dist(N + 1, -1);
  ll now = A[1];
  dist[now] = 1;
  K--;
  if (K == 1) {
    cout << now << enld;
    return 0;
  }
  while (1) {
    if (dist[A[now]] != -1) {
      ll m = dist[now] + 1 - dist[A[now]];
      K %= m;
      while (K > 0) {
        now = A[now];
        K--;
      }
      break;
    } else {
      dist[A[now]] = dist[now] + 1;
      if (K == 0) {
        break;
      }
      now = A[now];
      K--;
    }
  }
  cout << now << enld;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define enld '\\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[6] = {1, 0, -1, 0, 1, 1};
const int dy[6] = {0, 1, 0, -1, 1, -1};
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;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<ll> dist(N + 1, -1);
  ll now = A[1];
  dist[now] = 1;
  K--;
  if (K == 1) {
    cout << now << enld;
    return 0;
  }
  while (1) {
    if (dist[A[now]] != -1) {
      ll m = dist[now] + 1 - dist[A[now]];
      K %= m;
      while (K > 0) {
        now = A[now];
        K--;
      }
      break;
    } else {
      dist[A[now]] = dist[now] + 1;
      if (K == 0) {
        break;
      }
      now = A[now];
      K--;
    }
  }
  cout << now << enld;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
// static const ll MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  llint n, k;
  cin >> n >> k;

  vector<int> a(n);
  for (int in = 0; in < n; in++) {
    cin >> a[in];
    a[in]--;
  }

  vector<int> visited(n);
  vector<int> path;
  int now = 0;
  while (visited[now] == 0) {
    visited[now] = 1;
    path.push_back(now);
    now = a[now];
  }

  int s = find(path.begin(), path.end(), now) - path.begin();
  int r = (int)path.size() - s;
  if (k < s)
    cout << path[k] + 1 << endl;
  else
    cout << path[(k - s) % r + s] + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
// static const ll MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  llint n, k;
  cin >> n >> k;

  llint atmp;
  vector<llint> a(n);
  for (int in = 0; in < n; in++) {
    cin >> atmp;
    atmp--;
    a.at(in) = atmp;
  }

  vector<llint> used(n);
  queue<llint> q;
  llint tmp = 0;
  llint p = 0, b = 0;
  for (int in = 0; in < n; in++) {
    if (used.at(tmp) == 0) {
      q.push(tmp);
      used.at(tmp) = 1;
      tmp = a.at(tmp);

    } else {
      break;
    }
  }
  for (int in = 0; in < n; in++) {
    if (q.front() == tmp) {
      break;
    } else {
      q.pop();
      p++;
    }
  }

  vector<llint> loop;
  b = q.size();
  for (int iq = 0; iq < b; iq++) {
    loop.push_back(q.front());
    q.pop();
  }

  llint ians = (k - p) % b;
  if (ians == 0)
    ians = b - 1;
  if (p != 0) {
    ians++;
    if (ians == b)
      ians = 0;
  }
  llint ans = loop.at(ians) + 1;
  cout << ans << endl;

  // out << p << " " << b << endl;

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

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N + 1, 0);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i + 1);
  }
  vector<int> number(N + 1, -1);
  number[1] = 0;
  int idou = 0;
  int now = 1;
  for (long long i = 0; i < K; i++) {
    if (number[A[now]] == -1) {
      now = A[now];
      idou++;
      number[now] = idou;
    } else {
      idou++;
      int cheat = (K - number[A[now]]) % (idou - number[A[now]]);
      rep(j, cheat + 1) { now = A[now]; }
      break;
    }
  }
  cout << now << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int now = 1;
  for (long long i = 0; i < K; i++) {
    now = A.at(now - 1);
  }
  cout << now << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long N, K, cnt = 0LL, m;
int main() {
  cin >> N >> K;
  long long A[N], visited[N];
  memset(visited, -1, sizeof visited);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= 1;
  }
  vector<long long> r = {0};
  visited[0] = K;
  while (K != 0LL) {
    long long n = A[r.back()];
    // cout<<"K:"<<K-1<<endl;
    // cout<<n+1<<endl;
    if (visited[n] != -1) {
      K--;
      m = visited[n] - K;
      // cout<<"m"<<m<<endl;
      // cout<<"cnt"<<cnt<<endl;
      K %= m;
      if (K == 0) {
        cout << n + 1 << endl;
        return 0;
      }
      // cout<<"K"<<K<<endl;
      K++;
      memset(visited, -1, sizeof visited);
    }
    visited[n] = --K;
    r.push_back(n);
  }
  // for(auto x:r)cout<<x<<endl;
  cout << r.back() + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long N, K, cnt = 0LL, m;
int main() {
  cin >> N >> K;
  long long A[N], visited[N];
  memset(visited, -1, sizeof visited);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= 1;
  }
  vector<long long> r = {0};
  while (cnt != K) {
    long long n = A[r.back()];
    // cout<<"cnt:"<<cnt<<endl;
    // cout<<n+1<<endl;
    if (visited[n] != -1) {
      m = cnt + 1 - visited[n];
      // cout<<"m"<<m<<endl;
      // cout<<"cnt"<<cnt<<endl;
      K -= cnt;
      K %= m;
      K += cnt;
      // cout<<"K"<<K<<endl;
      memset(visited, -1, sizeof visited);
    }
    visited[n] = ++cnt;
    r.push_back(n);
  }
  // for(auto x:r)cout<<x<<endl;
  cout << r.back() + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long int K;
  int N;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int n = 1; n <= N; n++) {
    cin >> A[n];
  }

  vector<int> route;             // i回目の移動で訪れる場所の番号
  vector<int> spot_n(N + 1, -1); // 町n(1<=n<=N)を何番目に訪れたか
  int period = 0;                // 周期
  int initial_p = 0;             // 周期に入る前の移動回数
  route.push_back(1);
  route[0] = 1;
  spot_n[1] = 0;
  for (int i = 1; i < N; i++) {
    int temp_n = A[route[i - 1]];
    route.push_back(temp_n);
    if (spot_n[route[i]] != -1) {
      int former = spot_n[route[i]];
      int latter = i;
      period = latter - former;
      initial_p = spot_n[route[i]];
      break;
    }
    spot_n[route[i]] = i;
  }

  if (K <= initial_p) {
    cout << route[K] << endl;
  } else {
    int temp = (K - initial_p) % period + initial_p;
    cout << route[temp] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
// #define MAX 200000

int main() {
  int N;
  long long int K;
  cin >> N >> K;
  vector<int> A(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // 周期を計算
  vector<int> C(N + 1);
  int L = 0, init = 0, zero = 0;
  int next_pos = 0;
  C[0] = 1;
  bool can_find = false;
  for (long long int i = 0; i < K; i++) {
    C[i + 1] = A[next_pos];
    next_pos = A[next_pos] - 1;
    for (long long int j = 0; j < i + 1; j++) {
      if (C[j] == C[i + 1]) {
        zero = j;
        init = C[j] - 1;
        L = i + 1 - j;
        can_find = true;
        break;
      }
    }
    if (can_find) {
      break;
    }
    if (i == K - 1) {
      cout << C[i + 1];
      return 0;
    }
  }
  // 周期に入る前に終わる場合
  // if (K - zero <= 0)
  //{
  //	long long int temp = 0;
  //	for (long long int i = 0; i < K; i++) {
  //		temp = A[temp] -1;
  //	}
  //	cout << temp + 1;
  //}
  // else
  {
    long long int K_real = (K - zero) % L;
    long long int temp = init;
    for (long long int i = 0; i < K_real; i++) {
      temp = A[temp] - 1;
    }
    cout << temp + 1;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int k;
  long long int n, x = 1, y = 1, z = 0;
  cin >> n >> k;
  vector<long long int> a(n + 1);
  vector<long long int> b(n + 1, 0);
  for (long long int i = 1; i < n + 1; i++) {
    cin >> a[i];
  }

  for (long long int i = 1; i < k + 1; i++) {

    if (b[x] == 0) {
      b[x] = i;
    } else {
      y = i - b[x];
      z = i;
      x = a[x];
      break;
    }
    x = a[x];
  }
  if (y != 0 || z != 0) {
    for (long long int i = 0; i < (k - z) % y; i++) {
      x = a[x];
    }
  }
  cout << x << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int k;
  int n, x = 1, y, z;
  cin >> n >> k;
  vector<int> a(n + 1);
  vector<int> b(n + 1, 0);
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < k; i++) {
    x = a[x];
    if (b[i] == 0) {
      b[i] = i;
    } else {
      y = i - b[i];
      z = i;
      break;
    }
  }

  for (int i = z + 1; i < (z + 1) % y; i++) {
    x = a[x];
  }

  cout << x << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // sort, 二分探索(lower_bound)など
#include <cmath>     // pow, logなど
#include <complex>   // 複素数
#include <deque>     // 両端アクセスのキュー
#include <iostream>  // 入出力
#include <iterator>
#include <map>
#include <numeric> // iota(整数列の生成), gcd, lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map> // 順序保持しないmap
#include <unordered_set> // 順序保持しないset
#include <utility>       // pair
#include <vector>

#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, a, b) for (int i = (a); i >= (b); i--)
#define ALL(container) (container).begin(), (container).end()
#define MAX(x) *max_element(ALL(x)) // 最大値を求める
#define MIN(x) *min_element(ALL(x)) // 最小値を求める

using namespace std;
typedef long long ll;

constexpr int INF = 1e9;
constexpr int MOD = 1000000007;

int main() {
  size_t N, K;
  cin >> N >> K;
  vector<size_t> A(N);
  for (size_t i = 0; i < N; ++i) {
    cin >> A[i];
  }
  vector<size_t> visited;
  vector<int> ord(N + 1, -1); // 訪問順序(何番目に訪れたか)
  size_t cur = 1;
  while (ord[cur] == -1) {
    ord[cur] = visited.size();
    visited.push_back(cur);
    cur = A[cur - 1];
  }
  size_t period = visited.size() - ord[cur]; // 周期
  size_t rem = ord[cur]; // 周期に入らない例外部分

  if (K < rem) {
    cout << visited[K] << endl;
  } else {
    cout << visited[rem + ((K - rem) % period)] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm> // sort, 二分探索(lower_bound)など
#include <cmath>     // pow, logなど
#include <complex>   // 複素数
#include <deque>     // 両端アクセスのキュー
#include <iostream>  // 入出力
#include <iterator>
#include <map>
#include <numeric> // iota(整数列の生成), gcd, lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map> // 順序保持しないmap
#include <unordered_set> // 順序保持しないset
#include <utility>       // pair
#include <vector>

#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, a, b) for (int i = (a); i >= (b); i--)
#define ALL(container) (container).begin(), (container).end()
#define MAX(x) *max_element(ALL(x)) // 最大値を求める
#define MIN(x) *min_element(ALL(x)) // 最小値を求める

using namespace std;
typedef long long ll;

constexpr int INF = 1e9;
constexpr int MOD = 1000000007;

int main() {
  size_t N;
  ll K;
  cin >> N >> K;
  vector<size_t> A(N);
  for (size_t i = 0; i < N; ++i) {
    cin >> A[i];
  }
  vector<size_t> v;
  size_t arrived = 1, next = 1;
  size_t idx, gap;
  for (ll i = 0; i < K; ++i) {
    arrived = next;
    if (find(ALL(v), arrived) != v.end()) {
      idx = distance(v.begin(), find(ALL(v), arrived));
      gap = i - idx;
      vector<size_t> repeat(gap);
      copy(v.begin() + idx, v.begin() + i, repeat.begin());
      next = repeat[(K - idx) % gap];
      break;
    }
    v.push_back(arrived);
    next = A[arrived - 1];
  }

  cout << next << endl;

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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>

int N;
unsigned long long K;

int main() {
  scanf("%d %lld", &N, &K);
  int *A = (int *)malloc(sizeof(int) * N);
  int *visitedFlag = (int *)malloc(sizeof(int) * N);

  int i = 0;
  for (; i < N; i++) {
    scanf("%d", &A[i]);
    visitedFlag[i] = 0;
  }
  i = 0;
  while (!visitedFlag[i]) {
    visitedFlag[i] = 1;
    i = A[i] - 1;
  }
  int circle = i;
  i = 0;
  int stepNums[2] = {0, 1};
  while (i != circle) {
    stepNums[0]++;
    i = A[i] - 1;
  }
  i = A[i] - 1;
  while (i != circle) {
    stepNums[1]++;
    i = A[i] - 1;
  }
  if (K > stepNums[0]) {
    K = (K - stepNums[0]) % stepNums[1];
    i = circle;
    while (K != 0) {
      i = A[i] - 1;
      K--;
    }
  } else {
    i = 0;
    while (K != 0) {
      i = A[i] - 1;
      K--;
    }
  }
  printf("%d", i + 1);
  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>

int N;
unsigned long long K;

int main() {
  scanf("%d %lld", &N, &K);
  int *A = (int *)malloc(sizeof(int) * N);
  int i = 0;
  for (; i < N; i++)
    scanf("%d", &A[i]);
  unsigned long long nums = 0;
  i = 0;
  while (nums < K) {
    i = A[i] - 1;
    nums++;
  }
  printf("%d", i + 1);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}

ll mod(ll a, ll b) { return (a % b + b) % b; }

ll powerm(ll base, ll exp, ll mod) {
  ll ans = 1;
  while (exp) {
    if (exp & 1)
      ans = (ans * base) % mod;
    exp >>= 1, base = (base * base) % mod;
  }
  return ans;
}

using namespace std;
ll mods(string num, ll a) {
  // Initialize result
  ll res = 0;
  // One by one process all digits of 'num'
  for (ll i = 0; i < num.length(); i++)
    res = (res * 10 + (ll)num[i] - '0') % a;
  return res;
}
int main() {
  fast;
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  ll visited[n + 3];
  memset(visited, 0, sizeof(visited));
  ll arr[n + 1];
  for (ll i = 1; i <= n; i++)
    cin >> arr[i];
  ll timer = 0;
  ll x = 1;
  while (visited[x] == 0) {
    visited[x] = timer;
    timer++;
    x = arr[x];
  }
  ll t = timer - visited[x];
  x = 1;
  memset(visited, 0, sizeof(visited));
  timer = 0;
  while (k--) {
    if (visited[x] != 0) {
      k = k % t;
    }
    visited[x] = timer;
    timer++;
    x = arr[x];
  }
  cout << x << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}

ll mod(ll a, ll b) { return (a % b + b) % b; }

ll powerm(ll base, ll exp, ll mod) {
  ll ans = 1;
  while (exp) {
    if (exp & 1)
      ans = (ans * base) % mod;
    exp >>= 1, base = (base * base) % mod;
  }
  return ans;
}

using namespace std;

int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  ll arr[n + 1];
  for (ll i = 1; i <= n; i++)
    cin >> arr[i];
  ll x = 1;
  while (k--) {
    x = arr[x];
  }
  cout << x << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

/*"Success isn't permanent, failure isn't fatal,
                             it's the courage to continue that counts"*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\\n")
#define priln(n) printf("%d\\n", n)
#define lpriln(n) printf("%lld\\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
const int mod = 1e9 + 7;
const int N = 2e5 + 4;
const int lim = 61;

int dp[N][64];

int main() {
  int n;
  ll k;

  scn(n);
  lscn(k);

  rep(i, 1, n + 1) scn(dp[i][0]);

  rep(i, 1, lim) {
    rep(node, 1, n + 1) dp[node][i] = dp[dp[node][i - 1]][i - 1];
  }

  int cur = 1;

  rep(i, 0, lim) if (k & (1LL << i)) cur = dp[cur][i];

  pri(cur);

  return 0;
}


Error: WA

Fixed submission:

/*"Success isn't permanent, failure isn't fatal,
                             it's the courage to continue that counts"*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\\n")
#define priln(n) printf("%d\\n", n)
#define lpriln(n) printf("%lld\\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
const int mod = 1e9 + 7;
const int N = 1e5 + 4;
const int lim = 61;

int dp[N][64];

int main() {
  int n;
  ll k;

  scn(n);
  lscn(k);

  rep(i, 1, n + 1) scn(dp[i][0]);

  rep(i, 1, lim) {
    rep(node, 1, n + 1) dp[node][i] = dp[dp[node][i - 1]][i - 1];
  }

  int cur = 1;

  rep(i, 0, lim) if (k & (1LL << i)) cur = dp[cur][i];

  pri(cur);

  return 0;
}