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

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

void solve() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; ++i) {
    /* code */
    cin >> a[i];
  }
  vector<ll> s;

  ll t = n + 1;
  ll i = 1;
  ll l = 0;
  bool flag = true;
  set<ll> r;

  while (t--) {
    if (r.find(i) != r.end()) {
      auto it = find(s.begin(), s.end(), i);
      // cout<<it-s.begin()<<endl;
      if (k < (ll)(it - s.begin())) {
        cout << s[k] << endl;
        break;
      }
      k -= (ll)(it - s.begin());
      ll temp = s.end() - it;
      flag = false;
      ll y = (k % temp + temp) % temp;
      cout << *(it + y) << endl;
      break;
    } else {
      r.insert(i);
      s.push_back(i);
      i = a[i - 1];
    }
  }
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: WA

Fixed submission:

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

void solve() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; ++i) {
    /* code */
    cin >> a[i];
  }
  vector<ll> s;

  ll t = n + 1;
  ll i = 1;
  ll l = 0;
  bool flag = true;
  set<ll> r;

  while (t--) {
    if (r.find(i) != r.end()) {
      auto it = find(s.begin(), s.end(), i);
      if (k < (it - s.begin()))
        ;
      { cout << s[k] << endl; }
      k -= (ll)(it - s.begin());
      ll temp = s.end() - it;
      flag = false;
      ll y = (k % temp + temp) % temp;
      cout << *(it + y) << endl;
      break;
    } else {
      r.insert(i);
      s.push_back(i);
      i = a[i - 1];
    }
  }
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
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;

const int D = 60;
const int MAX_N = 200005;
int to[D][MAX_N];

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c = 1, l = 0;
  {
    int v = 1;
    while (ord[v] == -1) {
      ord[v] = s.size();
      s.push_back(v);
      v = a[v - 1];
    }
    c = s.size() - ord[v];
    l = ord[v];
  }
  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  ll k, i;
  cin >> k;
  vector<ll> a(n);
  vector<ll> next(n);
  for (i; i < n; i++)
    cin >> a[i];
  next[0] = 1;
  ll goal, loop;
  for (i = 0; i < n; i++) {
    next[i + 1] = a[next[i] - 1];
    goal = next[i + 1];
    if (next[i] == a[0]) {
      loop = i;
      break;
    }
  }
  k = k % loop;
  next[0] = 1;
  for (i = 0; i < k; i++) {
    next[i + 1] = a[next[i] - 1];
    goal = next[i + 1];
  }

  cout << goal << endl;

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
#define PI 3.14159265358979323846264338327950L

#define INF 0x3f3f3f3f
// const int N = 2e5 + 5;

int32_t main() {

  IOS;

  int N, K;

  cin >> N >> K;

  vector<int> arr(N);

  for (int i = 0; i < N; i++) {

    cin >> arr[i];

    arr[i]--;
  }

  vector<int> vis(N);

  vector<int> start, cyc;

  int x = 0;

  while (true) {

    vis[x] = 1;
    start.push_back(x);

    x = arr[x];

    if (vis[x]) {

      while (true) {

        cyc.push_back(start.back());

        int tmp = start.back();
        start.pop_back();

        if (tmp == x) {
          break;
        }
      }
      break;
    }
  }

  reverse(cyc.begin(), cyc.end());
  if (K < start.size()) {
    cout << start[K] + 1 << endl;
  } else {

    K -= start.size();

    K %= cyc.size();

    cout << cyc[K] + 1 << endl;
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
#define PI 3.14159265358979323846264338327950L

#define INF 0x3f3f3f3f
// const int N = 2e5 + 5;

int arr[200005];

int32_t main() {

  IOS;

  int N, K;

  cin >> N >> K;

  for (int i = 0; i < N; i++) {

    cin >> arr[i];

    arr[i]--;
  }

  vector<int> vis(N);

  vector<int> start, cyc;

  int x = 0;

  while (true) {

    vis[x] = 1;
    start.push_back(x);

    x = arr[x];

    if (vis[x]) {

      while (true) {

        cyc.push_back(start.back());

        int tmp = start.back();
        start.pop_back();

        if (tmp == x) {
          break;
        }
      }
      break;
    }
  }

  reverse(cyc.begin(), cyc.end());
  if (start.size() < K) {
    cout << arr[K] + 1 << endl;
  } else {

    K -= start.size();

    K %= cyc.size();

    cout << arr[K] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define srep(i, s, t) for (int i = s; i < t; i++)
#define drep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef vector<vector<char>> field_t;

typedef pair<int, int> point_t;

int main() {
  int64_t N, K, i, j, ans, seg, root, Z, C, root2;
  bool T;
  T = true;
  ans = 1;
  root = 0;
  cin >> N >> K;
  Z = K;
  vector<int> A(N), B(N);
  rep(i, N) { cin >> A[i]; }
  while (T) {
    B[ans - 1]++;
    seg = A[ans - 1];
    if (B[ans - 1] <= 1) {
      ans = seg;
      Z--;
    } else {
      if (B[ans - 1] == 2) {
        root++;
        ans = seg;
        Z--;
      }
    }
    if (Z == 0) {
      T = false;
    }
    if (B[ans - 1] >= 3) {
      root2 = root;
      T = false;
    }
  }
  C = Z % root2;
  if (root2 > 0) {
    rep(i, C) { ans = A[ans - 1]; }
    cout << ans << endl;
    return 0;
  } else {
    cout << ans << endl;
    return 0;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define srep(i, s, t) for (int i = s; i < t; i++)
#define drep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef vector<vector<char>> field_t;

typedef pair<int, int> point_t;

int main() {
  int64_t N, K, i, j, ans, seg, root, Z, C;
  bool T;
  T = true;
  ans = 1;
  root = 0;
  cin >> N >> K;
  Z = K;
  vector<int> A(N), B(N);
  B[0] = 1;
  rep(i, N) { cin >> A[i]; }
  while (T) {
    B[ans - 1] = +1;
    seg = A[ans - 1];
    if (B[ans - 1] <= 1) {
      ans = seg;
      Z--;
    } else {
      if (B[ans - 1] == 2) {
        root++;
      }
    }
    if (Z == 0 || B[ans - 1] >= 3) {
      T = false;
    }
  }
  C = Z % root;
  if (root > 0) {
    rep(i, C) { ans = A[ans - 1]; }
    cout << ans << endl;
    return 0;
  } else {
    cout << ans << endl;
    return 0;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for (int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int n, arr[200005], dist[200005], a, b, len, head;
ll k;
bool vis[200005];

int main() {
  fastio;
  cin >> n >> k;
  FOR(i, 1, n) cin >> arr[i];
  a = arr[arr[1]], b = arr[1];
  while (a != b)
    a = arr[arr[a]], b = arr[b];
  head = a;
  b = arr[b];
  len = 1;
  while (b != a) {
    b = arr[b];
    len++;
  }
  int cur = 1;
  while (true) {
    if (k == 0)
      break;
    if (cur == head)
      break;
    cur = arr[cur];
    k--;
  }
  if (k == 0)
    cout << cur;
  else {
    k %= len;
    while (k--)
      head = arr[head];
    cout << head;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

ll n, arr[200005], dist[200005], pre, len, head, k, cur = 1;
bool vis[200005];

void dfs(ll x, ll d) {
  if (vis[x]) {
    head = x;
    pre = dist[x];
    len = d - dist[x];
    return;
  }
  dist[x] = d;
  vis[x] = 1;
  dfs(arr[x], d + 1);
}

int main() {
  fastio;
  cin >> n >> k;
  FOR(i, 1, n) cin >> arr[i];
  dfs(1, 0);
  while (k--) {
    cur = arr[cur];
    if (cur == head)
      break;
  }
  if (k == 0)
    cout << cur;
  else {
    k %= len;
    while (k--)
      cur = arr[cur];
    cout << cur;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
  }
  vector<int> vis(n, -1);
  vector<int> path;
  int i = 0;
  while (vis[i] == -1) {
    path.push_back(i);
    vis[i] = path.size() - 1;
    i = v[i];
  }
  long long l = vis[i];
  long long m = path.size();
  // cout << l << " " << m << "\\n";
  if (k < l) {
    cout << path[k] + 1 << "\\n";
  } else {
    cout << path[l + (k - l) % (m - l)] + 1 << "\\n";
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  vector<bool> vis(n + 1, false);
  vector<int> path;
  int i = 1;
  long long l = 1;
  while (vis[i] == false) {
    path.push_back(i);
    vis[i] = true;
    i = v[i];
    l = i;
  }
  long long m = path.size();
  if (k < l) {
    cout << path[k] << "\\n";
  } else {
    cout << path[l + (k - l) % (m - l)] << "\\n";
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

long long i, j, n, m, k;

/*int main(){
    int x, ans = INT_MAX;
    bool ok = false;
    cin >> n >> m >> x;
    long long c[n][m];
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){
            cin >> c[i][j];
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){
            cin >> c[i][j];
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){

        }
    }
}*/

int main() {
  cin >> n >> k;
  int a[n];
  int lst[n];
  map<int, int> p;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  lst[0] = 1;
  for (i = 1; i < n; i++) {
    lst[i] = a[lst[i - 1] - 1];
    if (i == k) {
      cout << lst[i];
      return 0;
    }
  }
  int pre, post;
  for (i = 0; i < n; i++) {
    if (!p.count(lst[i]))
      p[lst[i]] = i;
    else {
      pre = p[lst[i]];
      post = i;
      break;
    }
  }
  k -= pre;
  k %= post - pre;
  cout << lst[k + pre] << endl;
}

Error: 0

Fixed submission:

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

long long i, j, n, m, k;

/*int main(){
    int x, ans = INT_MAX;
    bool ok = false;
    cin >> n >> m >> x;
    long long c[n][m];
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){
            cin >> c[i][j];
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){
            cin >> c[i][j];
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){

        }
    }
}*/

int main() {
  cin >> n >> k;
  int a[n];
  int lst[n];
  map<int, int> p;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  lst[0] = 1;
  for (i = 1; i < n; i++) {
    lst[i] = a[lst[i - 1]];
  }
  int pre, post;
  for (i = 0; i < n; i++) {
    if (!p.count(lst[i])) {
      p[lst[i]] = i;
      if (i == k) {
        cout << lst[i];
        return 0;
      }
    } else {
      pre = p[lst[i]];
      post = i;
      break;
    }
  }
  k -= pre - 1;
  k %= post - pre;
  cout << lst[k + pre] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAXN = 200010;
const int MAXM = 100010;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m;
ll k;
int ans, tot, cnt;

int a[MAXN];
int head[MAXN];

int s, t;

inline int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    s = (s << 3) + (s << 1) + ch - 48;
    ch = getchar();
  }
  return s * f;
}

int vis[MAXN];
int xun[MAXN];
int b[MAXN];

void dfs(int x) {
  if (vis[x]) {
    s = vis[x], t = cnt;
    return;
  }
  vis[x] = ++cnt;
  b[cnt] = x;
  dfs(a[x]);
  return;
}

int main() {
  int T;
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  dfs(1);
  ++k;
  if (k <= cnt) {
    printf("%d\\n", b[k]);
    return 0;
  }
  k -= (s - 1);
  k = (k - 1) % (t - s + 1) + 1;
  printf("%d\\n", b[s + k - 1]);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAXN = 100010;
const int MAXM = 100010;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m;
ll k;
int ans, tot, cnt;

int a[MAXN];
int head[MAXN];

int s;

inline int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    s = (s << 3) + (s << 1) + ch - 48;
    ch = getchar();
  }
  return s * f;
}

struct edge {
  int to, next;
} e[MAXN * 2];

void add_edge(int x, int y) {
  e[++tot].to = y;
  e[tot].next = head[x];
  head[x] = tot;
  return;
}

int vis[MAXN];
int xun[MAXN];
int b[MAXN];

int main() {
  int T;
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int p = 1, len = 1;
  xun[cnt++] = 1;
  vis[1] = 1;
  b[1] = 1;
  while (1) {
    //		cout << p << endl;
    if (!vis[a[p]]) {
      vis[a[p]] = 1;
      ++len;
      xun[cnt++] = a[p];
      b[a[p]] = cnt;
      p = a[p];
    } else {
      s = a[p];
      break;
    }
  }
  //	for (int i = 1; i <= n; i++) {
  //		cout << b[i] << " ";
  //	}
  //	cout << endl;
  len = b[p] - b[s] + 1;
  //	cout << len << len << endl;
  int yu = (k - 1) % (ll)len + 1;
  cout << xun[yu] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;

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

  vector<int> dists;
  vector<int> loop, straight;
  map<int, bool> alreadyVisit;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;

    dists.push_back(A - 1);
    alreadyVisit[i] = false;
  }

  alreadyVisit[0] = true;
  straight.push_back(0);
  int loopStartPosition = 0;
  auto loopStart = straight.begin();
  long long int pDist = 0;
  for (int i = 0; i < N; i++) {
    long long int dist = dists[pDist];
    // cout << "dist:" << dist << endl;
    if (alreadyVisit[dist]) {
      loopStart = find(straight.begin(), straight.end(), dist);

      for (auto itr = loopStart; itr != straight.end(); itr++) {
        // cout << "loop:" << *itr << endl;
        loop.push_back(*itr);
      }

      loopStartPosition = distance(straight.begin(), loopStart);
      break;
    } else {
      straight.push_back(dist);
      alreadyVisit[dist] = true;
      pDist = dist;
      if (i == K - 1) {
        cout << dist + 1 << endl;
        return 0;
      }
    }
  }

  // cout << loop[(K - loopStartPosition) % loop.size()] + 1 << endl;
  // cout << loop.size() << endl;
  // cout << K - loopStartPosition << endl;
  // cout << *loopStart << endl;
  cout << *(loopStart + ((K - loopStartPosition) % loop.size())) + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;

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

  vector<int> dists;
  vector<int> loop, straight;
  map<int, int> alreadyVisit;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;

    dists.push_back(A - 1);
  }

  alreadyVisit[0] = 1;
  straight.push_back(0);
  int loopStartPosition = 0;
  long long int pDist = 0;
  for (int i = 0; i < N; i++) {
    long long int dist = dists[pDist];

    if (alreadyVisit.find(dist) != alreadyVisit.end()) {
      auto loopStart = find(straight.begin(), straight.end(), dist);

      for (auto itr = loopStart; itr != straight.end(); itr++) {
        loop.push_back(*itr);
      }

      loopStartPosition = distance(straight.begin(), loopStart);
    } else {
      straight.push_back(dist);
      alreadyVisit[dist] = 1;
      pDist = dist;
      if (i == K - 1) {
        cout << dist + 1 << endl;
        return 0;
      }
    }
  }

  cout << loop[(K - loopStartPosition) % loop.size()] + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> s;
  vector<ll> ord(n + 1, -1);
  ll c = 1, l = 0;
  ll v = 1;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.emplace_back(v);
    v = a[v - 1];
  }
  c = s.size() - ord[v];
  l = ord[v];
  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> s;
  vector<ll> ord(n + 1, -1);
  ll c = 1, l = 0;
  ll v = 1;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.emplace_back(v);
    v = a[v - 1];
  }
  c = s.size() - ord[v];
  l = ord[v];
  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[(k - l) % c + k] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vector<vector<ll>> doub(62, vector<ll>(n, 0));
  for (ll i = 0; i < n; i++) {
    cin >> doub[0][i];
    doub[0][i]--;
  }
  for (ll i = 0; i < 61; i++) {
    for (ll j = 0; j < n; j++) {
      doub[i + 1][j] = doub[i][doub[i][j]];
    }
  }
  ll now = 0;
  for (ll i = 0; i < 62; i++) {
    if ((k >> i) & 1) {
      now = doub[i][now];
    }
  }
  cout << now + 1 << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  set<ll> b;
  vector<ll> c;
  ll now = 0;
  b.insert(0);
  ll m = 0;
  vector<ll> ans;
  for (ll i = 1; i <= n; i++) {
    if (k == i) {
      cout << a[now] << endl;
      return 0;
    }

    ll now = a[now];
    if (b.count(now)) {
      for (ll j = 0; j < c.size(); j++) {
        if (now == c[j])
          m == j;
      }
      for (ll k = m; k < c.size(); k++) {
        ans.emplace_back(c[k]);
      }
    }
    c.push_back(now - 1);
    b.insert(now - 1);
  }
  cout << ans[(k - m - 1) % ans.size()] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll MAX_N = 2e5 + 2;
vector<ll> adj[MAX_N];
vector<ll> used(MAX_N, 0);
vector<ll> path;
vector<ll> cycle;
ll cycle_start;
void dfs(ll cur) {
  used[cur] = 1;
  path.push_back(cur);
  if (used[adj[cur][0]] == 1) {
    path.push_back(adj[cur][0]);
    cycle_start = adj[cur][0];
    return;
  } else {
    dfs(adj[cur][0]);
  }
}
void dfs2(ll cur) {
  cycle.push_back(cur);
  if (adj[cur][0] == cycle_start) {
    return;
  } else
    dfs2(adj[cur][0]);
}
int main() {
  /// freopen("sub1_21.txt","r",stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k;
  ll u0, v0;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> u0;
    adj[i].push_back(u0);
  }
  dfs(1);
  dfs2(cycle_start);
  if (k <= (path.size() - 1)) {
    cout << path[k] << endl;
    return 0;
  }
  k -= (path.size() - 1);
  k %= cycle.size();
  cout << cycle[k] << endl;
}
/// 1 3 4


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll MAX_N = 1e5 + 2;
vector<ll> adj[MAX_N];
vector<ll> used(MAX_N);
vector<ll> path;
vector<ll> cycle;
int cycle_start;
void dfs(ll cur, ll depth) {
  used[cur] = 1;
  path.push_back(cur);
  if (used[adj[cur][0]] == 1) {
    cycle_start = adj[cur][0];
    return;
  } else {

    dfs(adj[cur][0], depth + 1);
  }
}
void dfs2(int cur) {
  cycle.push_back(cur);
  if (adj[cur][0] == cycle_start) {
    return;
  } else
    dfs2(adj[cur][0]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k;
  ll u0, v0;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> u0;
    adj[i].push_back(u0);
  }
  dfs(1, 1);
  dfs2(cycle_start);
  if (k < path.size()) {
    cout << path[k - 1] << endl;
    return 0;
  }
  k -= path.size();
  k %= cycle.size();
  cout << cycle[k] << endl;
}