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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int maxn = 200010;

int n, K;
int A[maxn];
int visited[maxn];
int ans;

void dfs(int x, int k, int d) {
  // cout << x << " " << k << " " << d << "\\n";
  if (k == 0) {
    ans = x;
    return;
  }

  // if (k < 0) assert(false);

  if (d == -1) {
    dfs(A[x], k - 1, -1);
  } else {
    if (visited[x]) {
      k %= (d - visited[x]);
      if (k == 0) {
        ans = x;
        return;
      } else {
        dfs(A[x], k - 1, -1);
      }
    } else {
      visited[x] = d;
      dfs(A[x], k - 1, d + 1);
    }
  }
}

int32_t main() {
  FAST cin >> n >> K;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }

  dfs(1, K, 0);
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int maxn = 200010;

int n, K;
int A[maxn];
int visited[maxn];
int ans;

void dfs(int x, int k, int d) {
  // cout << x << " " << k << " " << d << "\\n";
  if (k == 0) {
    ans = x;
    return;
  }

  if (k < 0)
    assert(false);

  if (d == -1) {
    dfs(A[x], k - 1, -1);
  } else {
    if (visited[x]) {
      k %= (d - visited[x]);
      if (k == 0) {
        ans = x;
        return;
      } else {
        dfs(A[x], k - 1, -1);
      }
    }
    visited[x] = d;
    dfs(A[x], k - 1, d + 1);
  }
}

int32_t main() {
  FAST cin >> n >> K;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }

  dfs(1, K, 0);
  cout << ans;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  long long int k;
  cin >> n >> k;
  vector<int> visit(n + 1);
  vector<int> arr(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  int pos = 1;
  int len = 0;
  while (visit[pos] == 0) {
    visit[pos] = 1;
    pos = arr[pos];
    len++;
  }
  int pos1 = arr[pos];
  int cycle = 1;
  while (pos1 != pos) {
    pos1 = arr[pos1];
    cycle++;
  }

  int j;
  if (k <= (len - cycle)) {
    j = k;
    pos = 1;
  } else {
    k = (k - (len - cycle));
    j = k % cycle;
  }

  while (j--) {
    pos = arr[pos];
  }
  cout << pos << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k;
  cin >> n >> k;
  long long int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  long long int p = A[0];
  k--;
  while (k--) {
    p = A[p - 1];
  }
  cout << p << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long int
using namespace std;
int n, m;
bool check(int x, int y) {
  if (x >= 0 and y >= 0 and x < n and y < m)
    return true;
  return false;
}
signed main() {
  ios_base::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);
  int n, k;
  cin >> n >> k;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  // for(int i=0; i<n; i++) cout<<a[i]<<" "; cout<<endl;
  vector<int> dp(n + 1, 0);
  int i = 1, c = 0;
  for (; c < k; c++) {
    if (dp[i] == 0) {
      dp[i] = 1;
      i = a[i];
      // cout<<i<<" d"<<endl;
    } else
      break;
  }

  if (c == k)
    cout << i << endl;
  else {
    vector<int> d(n + 1, 0);
    int j = i, count = 0;
    // d[j]=1;
    for (int z = 0; z < n; z++) {
      if (d[j] == 0) {
        d[j] = 1;
        count++;
        j = a[j];
      }
    }
    k -= (c - count);
    // int mod=c-j;
    k = (k % count);
    int e = 0;
    while (e < k) {
      e++;
      i = a[i];
    }
    cout << i << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

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

#include <bits/stdc++.h>

using namespace std;
#define DBG(x)                                                                 \\
  (void)(cout << "L" << __LINE__ << ": " << #x << " = " << (x) << '\\n')
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 210000;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setiosflags(ios::fixed) << setprecision(12);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  vector<ll> v;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  v.push_back(1);
  int num = 1;
  vector<ll> cnt(maxn);
  cnt[num] = 1;
  int cur;
  while (true) {
    num = a[num];
    if (cnt[num]) {
      cur = num;
      break;
    }
    cnt[num] = 1;
    v.push_back(num);
    // DBG(num);
  }
  int len = v.size();
  int pos;
  for (int i = 0; i < len; i++) {
    if (v[i] == cur) {
      pos = i;
      break;
    }
  }
  if (k <= pos) {
    cout << v[k] << "\\n";
  } else {
    ll cnt = k - pos;
    vector<int> b;
    for (int i = pos; i < v.size(); i++) {
      b.push_back(v[i]);
    }
    int cur1 = b.size();
    int cur2 = cnt % cur1;
    cout << b[cur2] << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

// #define DEBUG

const int NUM = 2 * 10e5 + 1;
// 存储
long long list[NUM];
// 标志访问
bool visit[NUM];
// 回路栈
vector<long long> vec;

int main() {
#ifdef DEBUG
  freopen("debug/test.txt", "r", stdin);
  freopen("debug/result.txt", "w", stdout);
#endif // DEBUG
  // test case
  long long n, k;
  scanf("%lld %lld", &n, &k);
  // 初始化
  memset(list, 0, sizeof(list));
  memset(visit, 0, sizeof(visit));
  // 输入
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &list[i]);
  }
  // 初始化
  long long buffer = 1;
  // 计算
  for (int i = 0; i < n; i++) {
    // 标志访问
    visit[buffer] = 1;
    // 节点入栈
    vec.push_back(buffer);
    // 获取下一个节点
    buffer = list[buffer - 1];
    // 若形成环路
    if (visit[buffer] == 1) {
      break;
    }
  }
  // 在栈中找出环路开始位置
  auto it = find(vec.begin(), vec.end(), buffer);
  // 环路长度
  long long cycleLength = vec.end() - it;
  // 环路开始前的栈深度
  long long stackLength = it - vec.begin();
  // debug
  // for (size_t i = 0; i < vec.size(); i++)
  // {
  //     printf("%lld ", vec[i]);
  // }
  // printf("\\n");
  // printf("cycleLength:%lld, stackLength:%lld\\n", cycleLength, stackLength);
  // 最终位置
  long long posMark;
  if (cycleLength > 1 && k > stackLength) {
    posMark = (k - stackLength) % cycleLength;
  } else if (cycleLength <= 1) {
    posMark = 0;
  } else {
    posMark = stackLength - k;
  }
  // 输出
  printf("%lld\\n", vec[stackLength + posMark]);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

// std::cout << std::fixed << std::setprecision(10)
// std::ios::sync_with_stdio(false);
constexpr long long MOD = 1000000007;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

std::pair<int, int> dfs(int i, int prev, int n, const std::vector<int> &A,
                        std::vector<int> &memo) {
  if (0 <= memo[i]) {
    return {i, prev};
  }
  memo[i] = n;
  return dfs(A[i], i, n + 1, A, memo);
}

int dfs1(int i, int n, const std::vector<int> &A) {
  if (n == 0)
    return i;
  return dfs1(A[i], n - 1, A);
}

int main() {
  long long N, K;
  std::cin >> N >> K;
  std::vector<int> A(N);
  for (auto &val : A) {
    std::cin >> val;
    --val;
  }
  std::vector<int> memo(N, -1);
  auto p = dfs(0, 0, 0, A, memo);
  int i = p.first;
  int j = p.second;
  int d = memo[i];
  if (K <= d) {
    std::cout << dfs1(0, K, A) + 1 << std::endl;
    return 0;
  }
  K -= d;
  long long l = memo[j] - memo[i] + 1;
  if (l == 1) {
    std::cout << i + 1 << std::endl;
    return 0;
  }
  K %= l;
  std::cout << dfs1(i, K, A) + 1 << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

// std::cout << std::fixed << std::setprecision(10)
// std::ios::sync_with_stdio(false);
constexpr long long MOD = 1000000007;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

int dfs(int i, int n, const std::vector<int> &A, std::vector<int> &memo) {
  if (0 <= memo[i]) {
    return i;
  }
  memo[i] = n;
  return dfs(A[i], n + 1, A, memo);
}

int dfs1(int i, int n, const std::vector<int> &A) {
  if (n == 0)
    return i;
  return dfs1(A[i], n - 1, A);
}

int main() {
  long long N, K;
  std::cin >> N >> K;
  std::vector<int> A(N);
  for (auto &val : A) {
    std::cin >> val;
    --val;
  }
  std::vector<int> memo(N, -1);
  int i = dfs(0, 0, A, memo);
  int d = memo[i];
  if (K < d) {
    std::cout << dfs1(0, K, A) << std::endl;
    return 0;
  }
  K -= d;
  int j = 0;
  for (int k = 0; k < A.size(); ++k) {
    if (A[k] == i && 0 <= memo[k]) {
      j = k;
      break;
    }
  }
  long long l = memo[j] - memo[i] + 1;
  K %= l;
  std::cout << dfs1(i, K, A) + 1 << std::endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  long long curr = 0;
  long long st = 0;
  while (true) {
    if (pr.find(curr) != pr.end()) {
      st = curr;
      break;
    } else {
      path.push_back(curr);
      pr.insert(curr);
      curr = a[curr];
    }
  }
  vector<long long>::iterator it = find(path.begin(), path.end(), st);
  long long b = it - path.begin();
  long long l = path.size();
  // for (int i = 0; i < l; i++)
  // cout << path[i] << " ";
  // cout << endl;
  if (k < l) {
    cout << path[k] + 1 << endl;
    return 0;
  }

  vector<long long> cycle;
  for (int i = b; i < l; i++) {
    cycle.push_back(path[i]);
  }
  k -= b;
  l = cycle.size();
  // for (int i = 0; i < l; i++)
  // cout << cycle[i] << " ";
  // cout << endl;
  long long rem = k % l;
  cout << cycle[rem] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  long long n, k;
  cin >> n >> k;
  long long a[n + 1];
  for (long long i = 1; i <= n; i++)
    cin >> a[i];
  vector<long long> path;
  set<long long> pr;

  long long curr = 1;
  path.push_back(curr);
  pr.insert(1);
  long long st = 0;
  while (true) {
    curr = a[curr];
    if (pr.find(curr) != pr.end()) {
      st = curr;
      break;
    } else {
      path.push_back(curr);
      pr.insert(curr);
    }
  }
  vector<long long>::iterator it = find(path.begin(), path.end(), st);
  long long b = it - path.begin();
  long long l = path.size();
  if (k <= l) {
    cout << path[k] << endl;
    return 0;
  }
  k -= b;
  l -= b;
  long long rem = k % l;
  cout << a[path[b + rem - 1]] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

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

  vector<int> s, o(n + 1, -1);
  int t = 1, l = 0, v = 1;
  while (o[v] == -1) {
    o[v] = s.size();
    s.push_back(v);
    v = a[v - 1];
  }
  t = s.size() - o[v];
  l = o[v];

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= t;
    cout << s[l + k] << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

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

  vector<int> s, o(n + 1, -1);
  int t = 1, l = 0, v = 1;
  while (o[v] == -1) {
    o[v] = s.size();
    s.push_back(v);
    v = a[v - 1];
  }
  t = s.size() - o[v];
  l = o[v];

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= t;
    cout << s[l + k] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  int pos = 1;
  int key = 0;
  vector<int> count(n + 1);
  for (int i = 1; i <= k; i++) {
    pos = a[pos];
    count[pos]++;
    if (count[pos] == 2) {
      key = pos;
      break;
    }
  }

  int preCount = 0;
  int keyCount = 0;
  pos = 1;
  vector<int> town;
  for (int i = 1; i <= k; i++) {
    pos = a[pos];
    if (pos == key)
      keyCount++;
    if (keyCount == 0)
      preCount++;
    else if (keyCount == 1)
      town.push_back(pos);
    else if (keyCount == 2)
      break;
  }
  if (0 <= k - preCount - 1)
    cout << town[(k - preCount - 1) % town.size()] << endl;
  else {
    int pos = 1;
    for (int i = 1; i <= k; i++)
      pos = a[pos];
    cout << pos << endl;
  }
}


Error: 0

Fixed submission:

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

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

  int pos = 1;
  int key = 0;
  vector<int> count(n + 1);
  for (int i = 1; i <= k; i++) {
    pos = a[pos];
    count[pos]++;
    if (count[pos] == 2) {
      key = pos;
      break;
    }
  }

  int preCount = 0;
  int keyCount = 0;
  pos = 1;
  vector<int> town;
  for (int i = 1; i <= k; i++) {
    pos = a[pos];
    if (pos == key)
      keyCount++;
    if (keyCount == 0)
      preCount++;
    else if (keyCount == 1)
      town.push_back(pos);
    else if (keyCount == 2)
      break;
  }
  int back = town[town.size() - 1];
  town.pop_back();
  town.insert(town.begin(), back);
  cout << town[(k - preCount) % town.size()] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 10);
  for (int i = 1; i <= n; i++) {
    cin >> a[i]; // 1 index
  }

  vector<ll> f(300000); // 何回目にどこにいるか
  f[0] = 1;             // 最初は1
  ll key = 0;
  set<ll> st;
  st.insert(1);
  for (int i = 0;; i++) {
    ll cur = f[i];
    ll to = a[cur];
    f[i + 1] = to;

    // はじめて同じ数がでたらbreak
    if (st.find(to) != st.end()) {
      key = to;
      break;
    }

    st.insert(to);
  }

  ll offset = 0;
  for (int i = 0; i < f.size(); i++) {
    if (f[i] == key) {
      break;
    } else {
      offset++;
    }
  }

  vector<ll> freqs;
  freqs.push_back(key);
  for (int i = offset + 1; i < f.size(); i++) {
    if (f[i] == key) {
      break;
    } else {
      freqs.push_back(f[i]);
    }
  }

  ll freq = freqs.size();

  ll ans = 0;

  if (k > offset) {
    k -= offset;
    ans = freqs[k % freq];
  } else {
    ans = f[k];
  }

  cout << ans << ln;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 10);
  for (int i = 1; i <= n; i++) {
    cin >> a[i]; // 1 index
  }

  vector<ll> f(300000); // 何回目にどこにいるか
  f[0] = 1;             // 最初は1
  ll key = 0;
  set<ll> st;
  st.insert(1);
  for (int i = 0; i < 200000; i++) {
    ll cur = f[i];
    ll to = a[cur];
    f[i + 1] = to;

    // はじめて同じ数がでたらbreak
    if (st.find(to) != st.end()) {
      key = to;
      break;
    }

    st.insert(to);
  }

  ll offset = 0;
  for (int i = 0; i < f.size(); i++) {
    if (f[i] == key) {
      break;
    } else {
      offset++;
    }
  }

  vector<ll> freqs;
  freqs.push_back(key);
  for (int i = offset + 1; i < f.size(); i++) {
    if (f[i] == key) {
      break;
    } else {
      freqs.push_back(f[i]);
    }
  }

  ll freq = freqs.size();

  ll ans = freqs[(k % freq) - offset];

  if (k < offset) {
    ans = f[k];
  }

  cout << ans << ln;

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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array) (sizeof(array) / sizeof(array[0]))
#define res(i, n) for (int i = n;; i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define REV(i, n) for (int i = n - 1; i > 0; i--)
#define req(i, n, m) for (int i = n; i < m; i++)
#define REQ(i, n, m, l) for (int i = n; i < m; i += l)
#define _GLIBCXX_DEBUG
int INF = 1e9 + 7;
long double PI =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998626034825342117;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1) {
  unsigned day;
  if (dow < dow1)
    dow += 7;
  day = dow - dow1;
  day += 7 * n - 6;
  return day;
}
signed gcd(long long x, long long y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
signed lcm(long long x, long long y) { return x / gcd(x, y) * y; }
unsigned DayToWeekNumber(unsigned day) { return (day - 1) / 7 + 1; }
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0) {
  return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<bool> B(N, false);
  B[0] = true;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<int> C(N + 1);
  C[0] = 0;
  for (long long i = 1; i < N + 1; i++) {
    C[i] = A[C[i - 1]] - 1;
    if (B[A[C[i]]] == true) {
      for (long long j = 0; j < i; j++) {
        if (C[j] == C[i]) {
          int a = (K - (j + 1)) % (j - i);
          cout << C[j + a + 1] + 1 << endl;
          return 0;
        }
      }
    }
    B[A[C[i]]] = true;
    if (i == K) {
      cout << C[i] + 1 << endl;
      return 0;
    }
  }
  cout << C[K % N] + 1 << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<bool> B(N, false);
  B[0] = true;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  B[0] = true;
  vector<int> C(N);
  C[0] = 0;
  for (int i = 0; i < N; i++) {
    C[i + 1] = A[C[i]] - 1;
    if (B[A[C[i]] - 1] == true) {
      for (int j = 0; j < i; j++) {
        if (C[j] == C[i]) {
          cout << C[(K - j) % i] << endl;
          return 0;
        }
      }
    }
    B[A[C[i]]] = true;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\\n'

using namespace std;
struct custom_hash {
  static unsigned long long splitmix64(unsigned long long x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(unsigned long long x) const {
    static const unsigned long long FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
mt19937
    random_generator(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 2e5 + 3;

int n;
long long k;
int a[maxn];
void read() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
}

bool used[maxn];
void solve() {
  vector<int> v;
  int curr = 1;
  while (!used[curr]) {
    used[curr] = true;
    v.push_back(curr);

    curr = a[curr];
  }

  for (int i = 0; i < (int)v.size(); i++)
    if (v[i] == curr) {
      if (k < i)
        cout << v[k] << endl;

      else {
        long long r = k - i;
        int l = (int)v.size() - i;

        r %= l;
        cout << v[i + r] << endl;
      }

      return;
    }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  read();
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\\n'

using namespace std;
struct custom_hash {
  static unsigned long long splitmix64(unsigned long long x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(unsigned long long x) const {
    static const unsigned long long FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
mt19937
    random_generator(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 2e5 + 3;

int n;
long long k;
int a[maxn];
void read() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
}

bool used[maxn];
void solve() {
  vector<int> v;
  int curr = 1;
  while (!used[curr]) {
    used[curr] = true;
    v.push_back(curr);

    curr = a[curr];
  }

  for (int i = 0; i < (int)v.size(); i++)
    if (v[i] == curr) {
      if (k < i)
        cout << v[k] << endl;

      else {
        int r = k - i;
        int l = (int)v.size() - i;

        r %= l;
        cout << v[i + r] << endl;
      }

      return;
    }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  read();
  solve();
}