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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cstring>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>

typedef long long int lli;

using namespace std;

int main() {

  lli N, K;
  cin >> N >> K;
  lli A[N];
  int city = 0;
  int T[N], F[N], FF[N];
  for (int i = 0; i < N; i++) {
    cin >> city;
    A[i] = city - 1;
    T[i] = 0;
    F[i] = 0;
    FF[i] = 0;
  }

  int pos = 0;
  int cc = 0;
  int rem = 0;
  for (long long int i = 0; i < K; i++) {

    if (pos == A[pos]) {
      break;
    } else {
      if (F[pos] == 0) {
        F[pos] = 1;
        FF[pos] = i;
      } else if (F[pos] == 1) {
        F[pos] = 2;
        T[pos] = i - FF[pos];
      } else if (F[pos] == 2 && cc == 0) {
        rem = (K - i) % T[pos];
        break;
      }
    } // fi
    // cout << i << " "<< pos << " " << T[pos] << " " << F[pos] << " " <<
    // FF[pos]<< endl;
    pos = A[pos];
  }
  for (long long int i = 0; i < rem; i++) {
    pos = A[pos];
  }
  cout << pos + 1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cstring>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>

typedef long long int lli;

using namespace std;

int main() {

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

  int pos = 0;
  for (long long int i = 0; i < K; i++) {
    if (pos == A[pos]) {
      break;
    } else {
      pos = A[pos];
    }
  }
  cout << pos + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
using ll = long long;

int main() {
  ll N, K, A[200010], visited[200010];
  cin >> N >> K;
  rep(i, N) {
    int a;
    cin >> a;
    A[i] = a - 1;
    visited[i] = -1;
  }

  ll time = 0, next = A[0];
  visited[0] = time;
  while (visited[next] == -1) {
    time++;
    visited[next] = time;
    if (time == K) {
      cout << next + 1 << endl;
      return 0;
    }
    next = A[next];
  }
  time++;

  ll r = K - time;
  ll q = time - visited[next];
  ll ans = next;
  rep(i, r % q) { ans = A[ans]; }
  cout << ans + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int N;
unsigned long long K;

int main() {
  cin >> N >> K;
  vector<int> A;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A.push_back(a - 1);
  }

  vector<int> B;
  map<int, int> Flag;
  Flag[0] = 1;
  int next = A[0];
  while (Flag[next] != 1) {
    Flag[next] = 1;
    B.push_back(next);
    next = A[next];
  }
  int n = 0;
  for (auto b : B) {
    if (b == next) {
      break;
    }
    n++;
  }
  long long l;
  if (next == 0) {
    l = B.size() + 1;
    n = 0;
    cout << B[K % l - 1] + 1 << endl;
  } else {
    l = B.size() - n;
    cout << B[n + (K - n - 1) % l] + 1 << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  ll i;
  ll n, k;
  cin >> n >> k;
  vector<ll> dest(n);
  int curr;
  ll loopsize = -1;
  unordered_map<int, ll> visited;
  for (i = 0; i < n; i++) {
    cin >> dest[i];
    dest[i]--;
  }

  curr = 0;
  visited[curr] = 0;
  for (i = 1; i <= k; i++) {
    // cout << curr << "->" << dest[curr] << endl;
    curr = dest[curr];
    if (loopsize == -1) {
      if (visited.find(curr) == visited.end())
        visited[curr] = i;
      else {
        loopsize = i - visited[curr];
        i = k - (k - i) % loopsize;
      }
    }
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  ll i;
  ll n, k;
  cin >> n >> k;
  vector<ll> dest(n);
  int curr = 0;
  for (i = 0; i < n; i++) {
    cin >> dest[i];
    dest[i]--;
  }

  for (i = 0; i < k; i++)
    curr = dest[curr];

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

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  if (k <= 200000) {
    int q = 0;
    while (k) {
      q = a[q];
      k--;
    }
    cout << q + 1 << endl;
    return 0;
  }
  vector<int> visited(n, 0);
  vector<int> last(n, 0);
  int p = 0, c = 0;
  for (int i = 0; i < last.size(); i++) {
    visited[p]++;
    if (visited[p] == 2) {
      c = i - last[p];
      break;
    }
    last[p] = i;
    p = a[p];
  }
  // cout << c << endl;
  k -= last[p];
  k %= c;
  while (k) {
    p = a[p];
    k--;
  }
  cout << p + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  if (k <= 200000) {
    int q = 0;
    while (k) {
      q = a[q];
      k--;
    }
    cout << q + 1 << endl;
    return 0;
  }
  vector<int> visited(n, -1);
  vector<int> last(n, -1);
  int p = 0, c = 0;
  for (int i = 0; i < last.size(); i++) {
    visited[p]++;
    if (visited[p] == 2) {
      c = i - last[p];
      break;
    }
    last[p] = i;
    p = a[p];
  }
  // cout << c << endl;
  k -= last[p];
  k %= c;
  while (k) {
    p = a[p];
    k--;
  }
  cout << p + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<ll> v(n, 0);
  for (ll i = 0; i < n; ++i) {
    cin >> v.at(i);
    --v[i];
  }

  set<int> s_used;
  vector<int> v_path;

  int now = 0;

  while (s_used.count(now) == 0) {
    v_path.push_back(now);
    s_used.insert(now);
    now = v[now];
  }

  // ???
  int s = find(v_path.begin(), v_path.end(), now) - v_path.begin();
  int r = (int)v_path.size() - s;

  // cout << s << " " << r << "\\n";

  if (s >= k)
    cout << v_path[k] + 1 << "\\n";
  else {
    cout << v_path[(k - s) % r + s] + 1 << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k;
  cin >> n >> k;

  vector<ll> v(n + 1), par(n + 1, -1), move(n, -1), cycle;
  for (ll i = 1; i <= n; ++i) {
    cin >> v[i];
  }
  ll next = v[1], cycle_s = 0;
  par[1] = 0;
  move[1] = 0;
  par[next] = 1;
  bool flag = false;
  for (ll i = 2; i <= k; ++i) {
    ll curr = next;
    next = v[curr];
    if (par[next] != -1) {
      flag = true;
      cycle_s = i - move[next];
      break;
    }
    par[next] = curr;
    move[next] = i;
  }

  if (flag == false) {
    cout << next << "\\n";
  }

  else {

    ll start = next;

    ll j = 0;
    k -= move[start];
    cycle.push_back(start);
    next = v[start];
    while (next != start) {
      cycle.push_back(next);
      ll curr = next;
      // cout << " a " << next;
      next = v[curr];
    }
    // cout << endl;
    // for(auto a : cycle){
    //     cout << a << " ";
    // }
    // cout << endl;

    // cycle.pop();
    ll pos = k % (cycle_s);
    // cout << cycle_s << " " << start << " " << pos << "\\n";
    cout << cycle[pos] << "\\n";
  }

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

#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using pii = pair<int, int>;

#define rep(i, k, n) for (int i = (k); i != int(n); ++i)
#define sz(x) x.size()
#define all(x) x.begin(), x.end()

#define dbg(x) cout << #x << '=' << x << '\\n'
#define dbg2(x, y) cout << #x << '=' << x << ' ' << #y << '=' << y << '\\n'

#define input() (*istream_iterator<lli>(cin))
#define strin() (*istream_iterator<string>(cin))
#define output(x) cout << x << '\\n'

const int mxn = 2e5 + 10;
lli arr[mxn];
bool is_visit[mxn];
int find_loop(int cur = 1) {
  is_visit[cur] = true;
  int nx = arr[cur];
  if (is_visit[nx])
    return nx;
  return find_loop(nx);
}
int main() {
  ios::sync_with_stdio(false);
  lli n = input(), k = input();
  vector<int> cycle;
  int cur = 1, dist = 0;
  for (int i = 1; i <= n; ++i)
    arr[i] = input();
  int loop_town = find_loop();
  // dbg(loop_town);
  while (cur != loop_town) {
    cur = arr[cur];
    dist++;
    if (dist == k) {
      cout << cur;
      return 0;
    }
  }
  // dbg(dist);
  k -= dist;
  cur = arr[loop_town];
  cycle.push_back(loop_town);
  while (cur != loop_town) {
    cycle.push_back(cur);
    cur = arr[cur];
  }
  // for(int cur: cycle) cout << cur;
  k %= cycle.size();
  cout << cycle[k];
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using pii = pair<int, int>;

#define rep(i, k, n) for (int i = (k); i != int(n); ++i)
#define sz(x) x.size()
#define all(x) x.begin(), x.end()

#define dbg(x) cout << #x << '=' << x << '\\n'
#define dbg2(x, y) cout << #x << '=' << x << ' ' << #y << '=' << y << '\\n'

#define input() (*istream_iterator<lli>(cin))
#define strin() (*istream_iterator<string>(cin))
#define output(x) cout << x << '\\n'

const int mxn = 2e5 + 10;
lli arr[mxn];
bool is_visit[mxn];
int find_loop(int cur = 1) {
  is_visit[cur] = true;
  int nx = arr[cur];
  if (is_visit[nx])
    return nx;
  find_loop(nx);
}
int main() {
  ios::sync_with_stdio(false);
  lli n = input(), k = input();
  vector<int> cycle;
  int cur = 1, dist = 0;
  for (int i = 1; i <= n; ++i)
    arr[i] = input();
  int loop_town = find_loop();
  // dbg(loop_town);
  while (cur != loop_town) {
    cur = arr[cur];
    dist++;
    if (dist == k) {
      cout << cur;
      return 0;
    }
  }
  // dbg(dist);
  k -= dist;
  cur = arr[loop_town];
  cycle.push_back(loop_town);
  while (cur != loop_town) {
    cycle.push_back(cur);
    cur = arr[cur];
  }
  // for(int cur: cycle) cout << cur;
  k %= cycle.size();
  cout << cycle[k];
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define int long long
using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vector<int> mark(n + 1, 0);
  int pos = 1;

  bool upto = false;

  for (int i = 1; i <= k; i++) {
    if (mark[pos] > 0) {
      k -= (mark[pos] - 1);
      k = k % (i - mark[pos]);
      upto = true;
      break;
    }
    mark[pos] = i;
    pos = a[pos];
  }
  // pos = 1;
  if (!upto) {
    pos = 1;
  }
  while (k--) {
    pos = a[pos];
  }
  cout << pos;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long
using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int tt = 0;
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int pos = 1;
  while (k--) {
    pos = a[pos];
  }
  cout << pos;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, k;
  vector<ll> v(1000000), c(1000000);
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
  }
  ll now = 0;
  bool can = true;
  for (ll i = 1; i <= k; i++) {
    if (c[now] && can) {
      can = false;
      ll tmp = abs(c[now] - i);
      i += tmp * ((k - i) / tmp);
    }
    c[now] = i;
    now = v[now];
  }
  cout << now + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ll n, k;
  vector<ll> v(10000), c(10000);
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
  }
  ll now = 0;
  bool can = true;
  for (ll i = 1; i <= k; i++) {
    if (c[now] && can) {
      can = false;
      ll tmp = abs(c[now] - i);
      i += tmp * ((k - i) / tmp);
    }
    c[now] = i;
    now = v[now];
  }
  cout << now + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

/* 大文字を小文字に変換 */
char tolower(char c) { return (c + 0x20); }
/* 小文字を大文字に変換 */
char toupr(char c) { return (c - 0x20); }

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 200100;

int P[65][MAX_N]; // 10**18 == 2*60だから

int main() {
  ll N, K;
  cin >> N >> K;
  int A[N];
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  rep(i, 65) {
    if (i == 0) {
      rep(j, N) P[i][j] = A[j];
    } else {
      rep(j, N) P[i][j] = P[i - 1][P[i - 1][j]];
    }
  }
  bitset<65> Kbit(K);
  int cur = 0;
  for (int i = 64; i >= 0; i--) {
    if (Kbit.test(i)) {
      cur = P[i][cur];
    }
  }
  cur++;
  cout << cur << endl;
}

Error: 0

Fixed submission:

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

/* 大文字を小文字に変換 */
char tolower(char c) { return (c + 0x20); }
/* 小文字を大文字に変換 */
char toupr(char c) { return (c - 0x20); }

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 100100;

int P[65][MAX_N]; // 10**18 == 2*60だから

int main() {
  ll N, K;
  cin >> N >> K;
  int A[N];
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  rep(i, 65) {
    if (i == 0) {
      rep(j, N) P[i][j] = A[j];
    } else {
      rep(j, N) P[i][j] = P[i - 1][P[i - 1][j]];
    }
  }
  bitset<65> Kbit(K);
  int cur = 0;
  for (int i = 64; i >= 0; i--) {
    if (Kbit.test(i)) {
      cur = P[i][cur];
    }
  }
  cur++;
  cout << cur << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int> order(n + 1, -1);
  vector<int> city;
  int c = 1, l = 0;
  {
    int p = 1;
    while (order.at(p) == -1) {
      order.at(p) = city.size();
      city.push_back(p);
      p = a.at(p - 1);
    }
    c = city.size() - order.at(p);
    l = order.at(p);
  }

  if (k < l)
    cout << city.at(k) << endl;
  else {
    k -= l;
    k %= c;
    cout << city.at(l + k) << endl;
  }

  return 0;
}

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


Fixed submission:

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

const int MAX_N = 200005;
int n;
ll k;

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

  int p = 0;
  vector<int> order(n + 1, -1);
  vector<int> city;
  while (order.at(p) == -1) {
    order.at(p) = city.size();
    city.push_back(p);
    p = a.at(p - 1);
  }
  int first = order.at(p);
  int second = city.size();
  int c = second - first;
  if (k < c)
    cout << city.at(k) + 1 << endl;
  else {
    k -= first;
    k %= c;
    cout << city.at(first + k) + 1 << endl;
  }

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

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

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> G(0);
  vector<int> kai(N, 0);
  int now = 0, fin = 0, saki = 0, sta = 0;
  G.push_back(0);
  while (G.size() <= N) {
    G.push_back(A[now]);
    if (kai[A[now]] == 0) {
      kai[A[now]]++;
      now = A[now];
      continue;
    } else {
      fin = G.size();
      saki = A[now];
      G.erase(G.end() - 1);
      for (int i = 0; i < fin - 1; i++) {
        if (G[i] == saki) {
          sta = i;
          break;
        }
      }
      fin -= 1;
      break;
    }
  }
  int ans;
  if (K < fin)
    ans = G[K];
  else {
    int mod = fin - sta;
    ans = G[sta + (K - sta) % mod];
  }
  cout << ans + 1 << endl;
}

Error: -8

Fixed submission:

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

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> G(0);
  vector<int> kai(N, 0);
  int now = 0, fin = 0, saki = 0, sta = 0;
  G.push_back(0);
  while (G.size() < N) {
    G.push_back(A[now]);
    if (kai[A[now]] == 0) {
      kai[A[now]]++;
      now = A[now];
      continue;
    } else {
      fin = G.size();
      saki = A[now];
      G.erase(G.end() - 1);
      for (int i = 0; i < fin - 1; i++) {
        if (G[i] == saki) {
          sta = i;
          break;
        }
      }
      fin -= 1;
      break;
    }
  }
  int ans;
  if (K < fin)
    ans = G[K];
  else {
    int mod = fin - sta;
    ans = G[sta + (K - sta) % mod];
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  deque<pair<ll, ll>> q;
  ll ans;
  set<ll> visited;
  q.push_back({0, 0});
  bool flag = true;
  ll memo[N];
  for (int i = 0; i < N; i++) {
    memo[i] = -1;
  }
  while (!q.empty()) {
    ll cur_i = q.front().first;
    ll cur_num = q.front().second;
    q.pop_front();
    if (flag && memo[cur_i] != -1) {
      flag = false;
      ll cnt = cur_num - memo[cur_i];
      cur_num += ((K - cur_num) / cnt) * cnt;
    }
    if (cur_num == K) {
      ans = cur_i + 1;
      break;
    }
    q.push_back({A[cur_i], cur_num + 1});
    memo[cur_i] = cur_num;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  deque<pair<ll, ll>> q;
  ll ans;
  set<ll> visited;
  q.push_back({0, 0});
  bool flag = true;
  ll memo[N];
  for (int i = 0; i < N; i++) {
    memo[i] = -1;
  }
  while (!q.empty()) {
    ll cur_i = q.front().first;
    ll cur_num = q.front().second;
    q.pop_front();
    if (flag && memo[cur_i] != -1) {
      flag = false;
      ll cnt = cur_num - memo[cur_i];
      cur_num += (K / cnt - 1) * cnt;
    }
    if (cur_num == K) {
      ans = cur_i + 1;
      break;
    }
    visited.insert(cur_i);
    q.push_back({A[cur_i], cur_num + 1});
    memo[cur_i] = cur_num;
  }
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  ll k;
  cin >> n >> k;

  int targets[n + 1];

  rep(i, n) {
    int a;
    cin >> a;
    targets[i + 1] = a;
  }

  int location = 1;

  vector<ll> visited(n + 1, -1);
  visited[1] = 0;
  ll loop_size = 0;

  for (ll i = 0; k > 0; i++) {
    k--;
    location = targets[location];

    if (visited[location] > -1) {
      loop_size = i + 1 - visited[location];
      break;
    }
    visited[location] = i + 1;
  }

  if (k == 0) {
    cout << location << endl;
    return 0;
  }

  ll remaining = k > loop_size ? k % loop_size : k;

  rep(i, remaining) { location = targets[location]; }

  cout << location << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  ll k;
  cin >> n >> k;

  int targets[n + 1];

  rep(i, n) {
    int a;
    cin >> a;
    targets[i + 1] = a;
  }

  int location = 1;

  vector<ll> visited(n + 1, -1);
  visited[1] = 0;
  ll loop_size = 0;

  for (ll i = 0; k > 0; i++) {
    k--;
    location = targets[location];

    if (visited[location] > -1) {
      loop_size = i - visited[location];
      break;
    }
    visited[location] = i + 1;
  }

  if (k == 0) {
    cout << location << endl;
  }

  ll remaining = k > loop_size ? k % loop_size : k;

  rep(i, remaining) { location = targets[location]; }

  cout << location << endl;
}