alexjercan/bugnet huggingface dataset:
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

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

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

  int now = 0;
  int ls = -1;
  map<int, int> m;
  int tc = 0;
  for (int i = 0; i < N; i++) {
    if (m.count(now) == 0)
      m[now] = 1;
    else
      m[now]++;
    if (m[now] == 2) {
      ls = now;
      break;
    }
    if (i == K) {
      cout << now + 1 << endl;
      return 0;
    }
    now = A[now];
    tc++;
  }

  now = ls;
  vector<int> loop;
  do {
    loop.push_back(now);
    now = A[now];
  } while (ls != now);
  int cnt = loop.size();

  K = K - tc;
  ll pos = K % cnt;
  cout << loop[pos] + 1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

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

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

  map<int, int> m;
  int rs = -1;
  m[0] = 1;
  for (int i = 0; i < N; i++) {
    if (m.count(A[i]) == 0)
      m[A[i]] = 1;
    else
      m[A[i]]++;
    if (m[A[i]] == 2) {
      rs = A[i];
      break;
    }
  }

  int now = rs;
  int cnt = 0;
  bool fl = false;
  vector<int> loop;
  do {
    if (rs == -1)
      break;
    loop.push_back(now);
    now = A[now];
  } while (now != rs);

  cnt = loop.size();

  now = 0;
  int tc = 0;
  while (now != rs) {
    now = A[now];
    tc++;
    if (K == tc) {
      cout << now + 1 << endl;
      return 0;
    }
  }

  K = K - tc;
  ll pos = K % cnt;
  cout << loop[pos] + 1 << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
st;*/

#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")

#define pb push_back
#define ld long double
#define int long long

mt19937 rnd(51);

const int N = 2e5 + 10;
vector<int> g[N];
vector<bool> used(N);

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    g[i].pb(a);
  }
  vector<int> v;
  int now = 1;
  bool x = 0;
  while (k > 0) {
    k--;
    used[now] = 1;
    v.pb(now);
    now = g[now][0];
    if (used[now] && !x) {
      x = 1;
      int dl = 0;
      for (int i = v.size() - 1; i > -1; i--) {
        if (v[i] == now) {
          dl = v.size() - i;
          break;
        }
      }
      k %= dl;
    }
  }
  cout << now << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
st;*/

#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")

#define pb push_back
#define ld long double
#define int long long

mt19937 rnd(51);

const int N = 2e5 + 10;
vector<int> g[N];

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    g[i].pb(a);
  }
  int now = 1;
  for (int i = 0; i < k; i++) {
    now = g[now][0];
  }
  cout << now << 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 SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define MAX 100005
using ll = long long;
using namespace std;

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

  vector<int> c(n + 1, -1);
  ll b[200000] = {};
  ll i = 0;
  ll v = 1;
  while (c[v] == -1) {
    c[v] = i;
    b[i] = v;
    v = a[v - 1];
    i++;
  }
  ll m = i - c[v];
  ll l = c[v];
  ll ans;
  if (k < l) {
    ans = b[k];
  } else {
    ans = b[l + ((k - l) % m)];
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define MAX 100005
using ll = long long;
using namespace std;

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

  ll b[200000] = {};
  int c[200001] = {};
  ll i = 1;
  ll v = 1;
  while (c[v] == 0) {
    c[v] = i;
    b[i] = v;
    v = a[v - 1];
    i++;
  }
  i--;
  ll m = i - c[v];
  ll l = c[v];
  ll ans;
  if (k < l) {
    ans = b[k];
  } else {
    ans = b[l + ((k - l) % m)];
  }
  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long N, K;
vector<long> A, visited;

int main(void) {

  cin >> N >> K;
  A.resize(N + 1);
  visited.resize(N + 1, false);
  for (long i = 1; i <= N; i++)
    cin >> A[i];

  long x = 1;
  long s = 0, t = 0;
  while (!visited[x]) {
    visited[x] = true;
    x = A[x];
    s++;
  }

  long y = x;
  do {
    y = A[y];
    t++;
  } while (x != y);

  long u = 0;
  if (K <= s) {
    u = K;
  } else {
    u = s + (K - s) % t;
  }

  long ans = 1;
  for (long i = 0; i < u; i++)
    ans = A[ans];

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

long N, K;
vector<long> A, visited;

int main(void) {

  cin >> N >> K;
  A.resize(N);
  visited.resize(N, false);
  for (long i = 1; i <= N; i++)
    cin >> A[i];

  long x = 1;
  long s = 0, t = 0;
  while (!visited[x]) {
    visited[x] = true;
    x = A[x];
    s++;
  }

  long y = x;
  do {
    y = A[y];
    t++;
  } while (x != y);

  long u = 0;
  if (K <= s) {
    u = K;
  } else {
    u = s + (K - s) % t;
  }

  long ans = 1;
  for (long i = 0; i < u; i++)
    ans = A[ans];

  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
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];
  vector<int> V;
  V.push_back(1);
  int loop_return;
  bool visited[N];
  for (int i = 0; i < N; i++)
    visited[i] = false;
  visited[0] = true;
  while (true) {
    int next = A[V.back() - 1];
    if (visited[next - 1]) {
      loop_return = distance(V.begin(), find(V.begin(), V.end(), next));
      break;
    } else {
      V.push_back(next);
      visited[next - 1] = true;
    }
  }
  if (K < V.size())
    cout << V[K] << endl;
  else
    cout << V[loop_return + (K - V.size()) % (V.size() - loop_return)] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
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];
  vector<int> V;
  V.push_back(1);
  int loop_return;
  while (true) {
    int next = A[V.back() - 1];
    auto it = lower_bound(V.begin(), V.end(), next);
    if (it != V.end() && *it == next) {
      loop_return = distance(V.begin(), it);
      break;
    } else {
      V.push_back(next);
    }
  }
  if (K < V.size())
    cout << V[K] << endl;
  else
    cout << V[loop_return + (K - V.size()) % (V.size() - loop_return)];
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
typedef long long ll;

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll before = 1;
  ll now = 1;
  map<ll, ll> mp;
  mp[1]++;
  for (ll i = 0; i < n; i++) {
    now = a[before - 1];
    before = now;
    if (mp[now] == 1) {
      break;
    }
    mp[now]++;
  }
  // cout << now << endl;

  ll count = 0;
  ll count1 = -1;
  ll count2 = 0;
  ll before2 = 1;
  ll now2 = 1;
  for (ll i = 0; i < n; i++) {
    if (now2 == now && count1 == -1) {
      count1 = count;
      count = 0;
    } else if (now2 == now && count1 != -1) {
      count2 = count;
      break;
    }
    count += 1;
    now2 = a[before2 - 1];
    before2 = now2;
  }
  // cout << count1 << " " << count2 << endl;

  ll kisoku = k - count1 + 1;
  ll amari = kisoku % count2;
  if (amari == 0) {
    amari = count2;
  }

  ll before3 = now;
  ll now3 = now;
  if (count2 != 0 && n < k) {
    for (ll i = 0; i < amari - 1; i++) {
      now3 = a[before3 - 1];
      before3 = now3;
      // cout << now3 << endl;
    }
  } else {
    before3 = 1;
    now3 = 1;
    for (ll i = 0; i < k; i++) {
      now3 = a[before3 - 1];
      before3 = now3;
      // cout << now3 << endl;
    }
  }
  cout << now3 << endl;
  return 0;
}

Error: -8

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
typedef long long ll;

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll before = 1;
  ll now = 1;
  map<ll, ll> mp;
  mp[1]++;
  for (ll i = 0; i < n - 1; i++) {
    now = a[before - 1];
    before = now;
    if (mp[now] == 1) {
      break;
    }
    mp[now]++;
  }
  // cout << now << endl;

  ll count = 0;
  ll count1 = -1;
  ll count2 = 0;
  ll before2 = 1;
  ll now2 = 1;
  for (ll i = 0; i < n - 1; i++) {
    if (now2 == now && count1 == -1) {
      count1 = count;
      count = 0;
    } else if (now2 == now && count1 != -1) {
      count2 = count;
      break;
    }
    count += 1;
    now2 = a[before2 - 1];
    before2 = now2;
  }
  // cout << count1 << " " << count2 << endl;

  ll kisoku = k - count1 + 1;
  ll amari = kisoku % count2;
  if (amari == 0) {
    amari = count2;
  }

  ll before3 = now;
  ll now3 = now;
  if (count2 != 0) {
    for (ll i = 0; i < amari - 1; i++) {
      now3 = a[before3 - 1];
      before3 = now3;
      // cout << now3 << endl;
    }
  } else {
    before3 = 1;
    now3 = 1;
    for (ll i = 0; i < k; i++) {
      now3 = a[before3 - 1];
      before3 = now3;
      // cout << now3 << endl;
    }
  }
  cout << now3 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
int main() {
  long long int n, k, f = 0, fn = 0;
  cin >> n >> k;
  vector<int> a(10000000);
  vector<int> b(10000000);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < k; i++) {
    if (b[f] == 0) {
      b[f] = 1;
      f = a[f] - 1;
    } else {
      fn = f;
      for (int j = 0; j < n; j++) {
        fn = a[fn] - 1;
        if (fn == f) {
          fn = j + 1;
          break;
        }
      }
      for (int j = 0; j < (k - i) % fn; j++) {
        f = a[f] - 1;
      }
      break;
    }
  }
  cout << f + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

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

long long int gcd(long long int a, long long int b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
long long int fac(long long int n) {
  if (n == 0) {
    return 1;
  } else {
    return n * (fac(n - 1)) % 1000000007;
  }
}

int main() {
  long long int n, k, ans = 0, q = 0, w = 0, x = 0, t = 0, r = 0;
  cin >> n >> k;
  vector<long long int> a(100000);
  vector<long long int> b(1000000);
  vector<long long int> c(1000000);
  vector<long long int> d(1000000);
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  b[0] = 0;
  c[0]++;
  d[0] = 0;
  for (long long int i = 0; i <= 3 * n; i++) {
    b[i + 1] = a[b[i]] - 1;
    c[b[i + 1]]++;
    d[b[i + 1]] = i + 1;
    if (c[b[i + 1]] > 2) {
      q = b[i + 1];
      break;
    }
  }
  for (long long int i = 0; i <= 3 * n; i++) {
    if (b[i] == q && t == 0) {
      r = i;
      t++;
    } else if (b[i] == q && t == 1) {
      w = i;
      t++;
    } else if (b[i] == q && t == 2) {
      x = i;
      break;
    }
  }
  if (k - 1 < r) {
    ans = b[k - 1];
  } else {
    ans = b[r + (k - r) % (x - w)];
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
int main() {
  ll n, k;
  cin >> n >> k;
  unordered_map<ll, ll> map;
  ll temp;
  for (ll i = 1; i <= n; i++) {
    cin >> temp;
    map[i] = temp;
  }
  vector<ll> cpath;
  cpath.push_back(1);
  unordered_map<ll, ll> vmap;
  vmap[1] = 0;
  int cpos = 1;
  int i = 1;
  while (vmap.find(map[cpos]) == vmap.end()) {
    vmap[map[cpos]] = i++;
    cpath.push_back(map[cpos]);
    cpos = map[cpos];
  }
  if (k > cpath.size() - 1) {
    ll plength = (cpath.size() - 1) - vmap[map[cpos]] + 1;
    k = (k - cpath.size()) % plength;
    k = k + vmap[map[cpos]];
    cout << cpath[k] << endl;
  } else {
    cout << cpath[k] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
void comp(ll cpos, ll k, unordered_map<ll, ll> &map) {
  if (k == 0) {
    cout << cpos << endl;
    return;
  }
  comp(map[cpos], k - 1, map);
}
int main() {
  ll n, k;
  cin >> n >> k;
  unordered_map<ll, ll> map;
  ll temp;
  for (ll i = 1; i <= n; i++) {
    cin >> temp;
    map[i] = temp;
  }
  comp(1, k, map);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <memory.h>
#include <vector>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()

int const N = 500001;
int n, v[N], vs[N], f;
vector<int> g[N], sec;
ll k;

void go(int i) {
  vs[i] = 1;
  sec.push_back(i);
  for (auto u : g[i]) {
    if (vs[u] == 2) {
      continue;
    }
    if (vs[u] == 1) {
      f = u;
      return;
    }
    if (!vs[u])
      go(u);
  }
  vs[i] = 2;
}

int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", v + i);
    g[i].push_back(v[i]);
  }
  go(1);
  int rem = 0, f0 = 0;
  vector<int> al;
  for (auto i : sec) {
    if (i == f) {
      f0 = 1;
    }
    if (!f0)
      ++rem;
    if (f0)
      al.push_back(i);
  }
  if (k < rem) {
    printf("%d\\n", sec[k]);
    return 0;
  }
  k -= rem;
  k %= al.size();
  printf("%d\\n", al[k]);
}


Error: 0

Fixed submission:

#include <iostream>
#include <memory.h>
#include <vector>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()

int const N = 200001;
int n, v[N], vs[N], f;
vector<int> g[N], sec;
ll k;

void go(int i) {
  vs[i] = 1;
  sec.push_back(i);
  for (auto u : g[i]) {
    if (!vs[u])
      go(u);
    if (vs[u] == 2) {
      f = u;
      return;
    }
  }
  vs[i] = 2;
}

int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", v + i);
    g[i].push_back(v[i]);
  }
  go(1);
  int rem = 0;
  for (auto i : sec) {
    if (i == f)
      break;
    ++rem;
  }
  k -= rem;
  printf("%d\\n", sec[k % (sec.size() - rem) + rem]);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <atcoder/all>

#define INF 1e9
#define rep(i, n) for (long long i = 0; (i) < (long long)(n); i++)
#define REP(i, a, b)                                                           \\
  for (long long i = (long long)(a); (i) <= (long long)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()
#define pb push_back

using namespace std;
// using namespace atcoder;
using ll = long long;
using P = pair<ll, ll>;
// using mint = modint1000000007;

ll cnt[20][20];
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  int mx = 62;
  vector<vector<ll>> next(mx + 1, vector<ll>(n + 1, -1));
  rep(i, n) next[0][i + 1] = a[i];
  rep(i, mx) { REP(j, 1, n) next[i + 1][j] = next[i][next[i][j]]; }
  int now = 1;
  for (int i = mx; i >= 0; i--) {
    if (((k >> i) & 1)) {
      now = next[i][now];
    }
  }
  cout << now << endl;
  // rep(i, 10){rep(j, n + 1) cout << next[i][j] << " "; cout << endl;}
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
// #include <atcoder/all>

#define INF 1e9
#define rep(i, n) for (long long i = 0; (i) < (long long)(n); i++)
#define REP(i, a, b)                                                           \\
  for (long long i = (long long)(a); (i) <= (long long)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()
#define pb push_back

using namespace std;
// using namespace atcoder;
using ll = long long;
using P = pair<ll, ll>;
// using mint = modint1000000007;

ll cnt[20][20];
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  int mx = 100;
  vector<vector<ll>> next(mx + 1, vector<ll>(n + 1, -1));
  rep(i, n) next[0][i + 1] = a[i];
  rep(i, mx + 1) { REP(j, 1, n) next[i + 1][j] = next[i][next[i][j]]; }
  int now = 1;
  for (int i = mx; i >= 0; i--) {
    if (((k >> i) & 1)) {
      now = next[i][now];
    }
  }
  cout << now << endl;
  // rep(i, 10){rep(j, n + 1) cout << next[i][j] << " "; cout << endl;}
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define INF (1 << 31) - 1

int main() {
  ll N;
  cin >> N;
  ll K;
  cin >> K;
  int A[N];
  rep(i, N) { cin >> A[i]; }
  int city[2 * N + 1];
  city[0] = 1;
  // city[N]はN秒後にいる場所
  for (ll i = 1; i <= 2 * N; i++) {
    city[i] = A[city[i - 1] - 1];
  }
  ll cycle = 0;
  for (ll i = N - 1; i >= 0; i--) {
    if (city[i] == city[N]) {
      cycle = N - i;
      break;
    }
  }
  if (K <= 2 * N)
    cout << city[K] << endl;
  else {
    ll r = (K - N) % cycle;
    cout << city[N + r] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define INF (1 << 31) - 1

int main() {
  int N;
  cin >> N;
  ll K;
  cin >> K;
  int A[N];
  rep(i, N) { cin >> A[i]; }
  int city = 1;
  for (ll i = 0; i < K; i++) {
    city = A[city - 1];
  }
  cout << city << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  int up[70][n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> up[0][i];
  }
  for (int i = 1; i < 70; i++) {
    for (int node = 1; node <= n; node++) {
      up[i][node] = up[i - 1][up[i - 1][node]];
    }
  }
  int pow[70];
  pow[0] = 1;
  for (int i = 1; i < 70; i++) {
    pow[i] = pow[i - 1] * 2;
  }
  int u = 1;
  while (k != 0) {
    int log = 0;
    while (pow[log + 1] < k) {
      log++;
    }
    u = up[log][u];
    k -= pow[log];
  }
  cout << u << endl;
}
/*

 */

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  int up[40][n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> up[0][i];
  }
  for (int i = 1; i < 40; i++) {
    for (int node = 1; node <= n; node++) {
      up[i][node] = up[i - 1][up[i - 1][node]];
    }
  }
  int pow[40];
  pow[0] = 1;
  for (int i = 1; i < 40; i++) {
    pow[i] = pow[i - 1] * 2;
  }
  int u = 1;
  while (k != 0) {
    int log = 0;
    while (pow[log + 1] < k) {
      log++;
    }
    u = up[log][u];
    k -= pow[log];
  }
  cout << u << endl;
}
/*

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

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  int move = 0;
  vector<int> move_point_count(n, 0);
  vector<int> obserb;
  vector<int> obserb2;

  while (true) {
    move_point_count.at(move)++;
    if (move_point_count.at(move) == 2) {
      break;
    }
    obserb.push_back(move);
    move = a.at(move) - 1;
  }
  int move_memo = move;
  while (true) {
    obserb2.push_back(move);
    move = a.at(move) - 1;
    if (move_memo == move)
      break;
  }

  int obserber = obserb.size() - obserb2.size();
  if (obserber == 0)
    cout << obserb.at(k % obserb.size()) + 1 << endl;
  else if (k <= obserber)
    cout << obserb.at(k) + 1 << endl;
  else {
    k -= obserber;
    cout << obserb2.at(k % obserb2.size()) + 1 << endl;
  }
  return 0;
}

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


Fixed submission:

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);

  ll move = 0;
  vector<ll> move_point_count(n, 0);
  vector<ll> obserb;
  vector<ll> obserb2;
  ll x = 0;
  ll obserb_size = 0, obserb2_size = 0;

  while (true) {
    move_point_count.at(move)++;
    if (move_point_count.at(move) == 2) {
      break;
    }
    obserb.at(x) = move;
    obserb_size++;
    x++;
    move = a.at(move) - 1;
  }
  ll move_memo = move;
  x = 0;
  while (true) {

    obserb2.at(x) = move;
    x++;
    obserb2_size++;
    move = a.at(move) - 1;
    if (move_memo == move)
      break;
  }
  ll obserber = obserb_size - obserb2_size;
  if (obserber == 0)
    cout << obserb.at(k % obserb_size) << endl;
  else if (k <= obserber)
    cout << obserb.at(obserber) << endl;
  else {
    k -= obserber;
    cout << obserb2.at(k % obserb2_size) << endl;
  }
  return 0;
}