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

#include <iostream>
#include <unordered_map>
#include <vector>
int main() {
  int N;
  unsigned long long K;
  std::cin >> N >> K;
  std::vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
    A[i] -= 1;
  }
  std::vector<int> path;
  std::unordered_map<int, int> map;
  unsigned int nowTown = 0;
  unsigned long long cnt = 0;
  long long loopStart = -1;
  bool isFindLoop = 0;
  path.push_back(nowTown);
  while (cnt < K) {
    cnt++;
    nowTown = A[nowTown];
    path.push_back(nowTown);
    if (map[nowTown] == 1) {
      isFindLoop = 1;
      break;
    }
    map[nowTown] = 1;
  }
  if (isFindLoop) {
    for (int i = path.size() - 2; i >= 0; --i) {
      if (nowTown == path[i]) {
        loopStart = i;
        break;
      }
    }
    cnt = (K - cnt) % (path.size() - loopStart - 1);
    for (int i = 0; i < cnt; ++i) {
      nowTown = A[nowTown];
    }
  }
  std::cout << nowTown + 1 << std::endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int N;
  unsigned long long K;
  std::cin >> N >> K;
  std::vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
    A[i] -= 1;
  }
  std::vector<int> path;
  unsigned int nowTown = 0;
  unsigned long long cnt = 0;
  long long loopStart = -1;
  path.push_back(nowTown);
  while (cnt < K) {
    cnt++;
    nowTown = A[nowTown];
    for (long long i = path.size() - 1; i >= 0; --i) {
      if (path[i] == nowTown) {
        loopStart = i;
        break;
      }
    }
    path.push_back(nowTown);
    if (loopStart != -1) {
      break;
    }
  }
  if (cnt != K) {
    cnt = (K - cnt) % (path.size() - loopStart - 1);
    for (int i = 0; i < cnt; ++i) {
      nowTown = A[nowTown];
    }
  }
  std::cout << nowTown + 1 << std::endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
static const double PI = acos(-1);

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n); // テレポート先を記録
  vector<int> cnt(n);
  vector<bool> flg(n);

  REP(i, n) {
    int x;
    cin >> x;
    --x;
    a[i] = x;
  }
  REP(i, n) flg[i] = true;

  ll pos = 0;
  ll roop = 0;
  ll s = 0;
  for (ll i = 0; i < k; ++i) {
    // cout << pos << endl;

    if (flg[pos]) {
      // cout << "Pos:" << pos+1 << endl;
      cnt[pos] = i;
      flg[pos] = false;
      pos = a[pos]; // テレポート
    } else {
      roop = i - cnt[pos];
      s = cnt[pos];
      break;
    }

    // 閉路がなかった場合
    if (i == k - 1) {
      ++pos;
      cout << pos << endl;
      return 0;
    }
  }

  // cout << "Pos:" << pos << endl;
  // cout << "roop:" << roop << endl;

  // 自己ループ
  if (roop == 0) {
    ++pos;
    cout << pos << endl;
    return 0;
  }
  // cout << "===="<< endl;
  // cout << k%roop << endl;
  if ((k - s) % roop == 0) {

    ++pos;
    cout << pos << endl;
    // REP(i,n) cout << a[i] << endl;
    return 0;

  } else {
    REP(i, (k - s) % roop) {
      //  cout << "Pos:" << pos+1 << endl;
      pos = a[pos];
    }
    ++pos;
    cout << pos << endl;
    // REP(i,n) cout << a[i] << endl;
    return 0;
  }
  cout << k % roop << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
static const double PI = acos(-1);

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n); // テレポート先を記録
  vector<int> cnt(n);
  vector<bool> flg(n);

  REP(i, n) {
    int x;
    cin >> x;
    --x;
    a[i] = x;
  }
  REP(i, n) flg[i] = true;

  ll pos = 0;
  ll roop = 0;
  REP(i, k) {
    // cout << pos << endl;

    if (flg[pos]) {
      cnt[pos] = i;
      flg[pos] = false;
      pos = a[pos]; // テレポート
    } else {
      roop = i - cnt[pos];
      break;
    }
  }

  // cout << "Pos:" << pos << endl;
  // cout << "roop:" << roop << endl;

  REP(i, ll(k % roop)) { pos = a[pos]; }

  ++pos;
  cout << pos << endl;
  // REP(i,n) cout << a[i] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &&e : a) {
    cin >> e;
  }
  vector<int> dp = {1};
  bool flag = true;
  ll bius, term;
  set<int> st = {1};
  while (flag) {
    if (st.find(a[dp.back() - 1]) != st.end()) {
      for (int i = 0; i < dp.size(); ++i) {
        if (dp[i] == a[dp.back() - 1]) {
          flag = false;
          bius = i;
          term = dp.size() - i;
        }
      }
    }
    int next = a[dp.back() - 1];
    dp.push_back(next);
    st.insert(next);
  }
  if (k <= bius)
    cout << dp[k];
  else {
    k -= bius;
    k %= term;
    k += bius;
    cout << dp[k];
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &&e : a) {
    cin >> e;
  }
  vector<int> dp = {1};
  bool flag = true;
  ll bius, term;
  for (int j = 0; j < 3 * n; j++) {
    vector<int> tmp = dp;
    sort(tmp.begin(), tmp.end());
    if (binary_search(tmp.begin(), tmp.end(), a[dp.back() - 1])) {
      for (int i = 0; i < dp.size(); ++i) {
        if (dp[i] == a[dp.back() - 1]) {
          flag = false;
          bius = i;
          term = dp.size() - i;
        }
      }
      break;
    }
    dp.push_back(a[dp.back() - 1]);
  }
  if (k <= bius)
    cout << dp[k];
  else {
    k -= bius;
    k %= term;
    k += bius;
    cout << dp[k];
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<ll, ll> P;

const ll mod =
    1000000007; // 出力は (ans % mod + mod) % mod  (負の剰余を正にする)
const int inf = 1e9;
const long long INF = 1LL << 60; // INF = 1152921504606846976
const double PI = 3.1415926535897932;

bool v[200005];
int a[200005];
int x = 0;
vector<int> p;

ll dfs(int now) {
  // cout << now << ' ';
  p.push_back(now);
  if (v[now])
    return now;
  else {

    v[now] = true;
    return dfs(a[now]);
  }
}
ll cnt(int now) {
  if (v[now])
    return x;
  else {
    x++;
    v[now] = true;
    return cnt(a[now]);
  }
}

int main() {
  vector<int> q;
  vector<int> r;
  ll n, k;
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  ll last = dfs(0);
  rep(i, n) v[i] = false;
  ll x = cnt(last);
  // cout << last << ' ' << x << endl;
  if (k < p.size() - 1) {
    cout << p[k] + 1 << endl;
  } else {
    ll id = (p.size() - 1 - x) + (k - (ll)p.size()) % x;
    cout << p[id + 1] + 1 << endl;
  }
}


Error: WA

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> P;

const ll mod =
    1000000007; // 出力は (ans % mod + mod) % mod  (負の剰余を正にする)
const int inf = 1e9;
const long long INF = 1LL << 60; // INF = 1152921504606846976
const double PI = 3.1415926535897932;

bool v[200005];
int a[200005];
int x = 0;
vector<int> p;

int dfs(int now) {
  // cout << now << ' ';
  p.push_back(now);
  if (v[now])
    return now;
  else {

    v[now] = true;
    return dfs(a[now]);
  }
}
int cnt(int now) {
  // cout << now << ' ';
  if (v[now])
    return x;
  else {
    x++;
    v[now] = true;
    return cnt(a[now]);
  }
}

int main() {
  vector<int> q;
  vector<int> r;
  ll n, k;
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int last = dfs(0);
  int tmp = 0;
  rep(i, n) {
    if (tmp == 0)
      q.push_back(p[i]);
    else
      r.push_back(p[i]);
    if (p[i] == last)
      tmp = 1;
  }
  q.pop_back();
  if (k < q.size()) {
    cout << q[k] + 1;
  } else {
    cout << r[(k - q.size() - 1) % r.size()] + 1;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

const int maxn = 200005;
int n, a[maxn], idx[maxn];
long long k;

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int now = 1;
  bool flag = true;
  for (int i = 1; i <= k; i++) {
    now = a[now];
    if (now == 1 || idx[now]) {
      k -= idx[now];
      k %= (i - idx[now]);
      k += idx[now];
      for (int i = 1; i <= n; i++) {
        if (idx[i] == k) {
          cout << i << "\\n";
          break;
        }
      }
      flag = false;
      break;
    }
    idx[now] = i;
  }
  if (flag)
    cout << now << "\\n";
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

const int maxn = 25;
int n, a[maxn], idx[maxn];
long long k;

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int now = 1;
  for (int i = 1; i <= n; i++) {
    now = a[now];
    if (now == 1 || idx[now]) {
      k -= idx[now];
      k %= (i - idx[now]);
      k += idx[now];
      for (int i = 1; i <= n; i++) {
        if (idx[i] == k) {
          cout << i << "\\n";
          break;
        }
      }
      break;
    }
    idx[now] = i;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

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

  vector<ll> a(n + 1), p;
  map<ll, ll> m;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    m[i] = INF;
  }

  ll time = 0;
  ll city = 1;
  ll start, end;

  while (true) {
    if (m[city] == INF) {
      m[city] = time;
      p.push_back(city);
      city = a[city];
    } else {
      start = m[city];
      end = time;
      break;
    }

    time++;
  }

  if (start > k) {
    cout << p[k] << endl;
    return 0;
  }

  cout << p[(k - start) % (start - end) + start] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

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

  vector<ll> a(n), p;
  map<ll, ll> m;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    m[i] = INF;
  }

  ll time = 0;
  ll city = 1;
  ll start, end;

  while (true) {
    if (m[city] == INF) {
      m[city] = time;
      p.push_back(city);
      city = a[city];
    } else {
      start = m[city];
      end = time;
      break;
    }

    time++;
  }

  if (start > k) {
    cout << p[k] << endl;
    return 0;
  }

  cout << p[(k - start) % (start - end) + start] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

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

  vector<ll> a(n), p;
  map<ll, ll> m;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    m[i] = INF;
  }

  ll time = 0;
  ll city = 0;
  ll start, end;

  while (true) {
    if (m[city] == INF) {
      m[city] = time;
      p.push_back(city);
      city = a[city];
    } else {
      start = m[city];
      end = time;
      break;
    }

    time++;
  }

  if (start > k) {
    cout << p[k] + 1 << endl;
    return 0;
  }

  cout << p[(k - start) % (start - end) + start] + 1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

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

  vector<ll> a(n), p;
  map<ll, ll> m;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    m[i] = INF;
  }

  ll time = 0;
  ll city = 0;
  ll start, end;

  while (true) {
    if (m[city] == INF) {
      m[city] = time;
      p.push_back(city);
      city = a[city];
    } else {
      start = m[city];
      end = time;
    }

    time++;
  }

  if (start > k) {
    cout << p[k] << endl;
  }

  cout << p[(k - start) % (start - end) + start] << endl;
}
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++)
typedef pair<int, int> P;

const ll MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  vector<ll> aa;
  set<ll> st;
  set<ll> st2;
  vector<ll> loop;
  ll loop_cnt = 0LL;
  ll before_loop = 0LL;
  // st.insert(1);
  a[0] = 1;
  rep(i, n) {
    cin >> a[i + 1];
    // a[i+1];
  }
  aa.push_back(1);
  rep(i, 2 * n) { aa.push_back(a[aa[i]]); }

  rep(i, 2 * n) {
    if (!st.count(aa[i])) {
      st.insert(aa[i]);
      before_loop++;
    } else if (!st2.count(aa[i])) {
      st2.insert(aa[i]);
      loop.push_back(aa[i]);
      // loop_cnt++;
    }
  }
  ll ans;
  loop_cnt = st2.size();
  // cout << "before_loop " << before_loop << endl;
  // cout << "loop_cnt " << loop_cnt << endl;
  if (k <= before_loop) {
    ans = aa[k];
  } else {
    ll kk = ((k - before_loop) % loop_cnt);
    ans = loop[kk];
  }
  cout << ans << 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++)
typedef pair<int, int> P;

const ll MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<ll> aa;
  aa.push_back(0);
  int cnt = 1;
  while (cnt <= 3 * n) {
    int i = cnt % n;
    aa.push_back(a[aa[i - 1]]);
    cnt++;
  }

  ll r = k % n;
  // cout << r << endl;
  cout << aa[n + r + 1] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A;
  rep(i, N) {
    ll a;
    cin >> a;
    A.push_back(a);
  }

  ll town = 1;
  vector<ll> done;
  rep(i, N) done.push_back(N);
  ll begin_times = N;
  ll loop_times = N;
  rep(i, N) {
    done.at(town - 1) = i;
    town = A.at(town - 1);
    if (i == K - 1) {
      cout << town << endl;
      return 0;
    }
    if (done.at(town - 1) != N) {
      begin_times = done.at(town - 1);
      loop_times = i - begin_times + 1;
      break;
    }
  }

  ll limit = (K - begin_times) % loop_times;
  rep(i, limit) town = A.at(town - 1);
  cout << town << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  int A[N];
  rep(i, N) { cin >> A[i]; }

  int town = 1;
  vector<int> route;
  long long begin_route_length;
  long long loop_route_length;
  int flag = 0;
  for (long long i = 0; i < K; i++) {
    if (flag == 0)
      route.push_back(town);
    town = A[town - 1];
    if (flag == 0) {
      int S = route.size();
      rep(j, S) {
        if (route.at(j) == town) {
          begin_route_length = (long long)j;
          loop_route_length = i - (long long)j + 1;
          i = (begin_route_length - 1) +
              loop_route_length *
                  ((K - begin_route_length) / loop_route_length);
          flag = 1;
          break;
        }
      }
    }
  }
  cout << town << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

int to[220000];
bool reached[220000];
int cnt[220000];

int main() {
  int n;
  Int k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> to[i];
  }
  int now = 1;
  reached[1] = true;
  Int sumcnt = 0;
  int period = INF;
  while (true) {
    int nex = to[now];
    sumcnt++;
    if (sumcnt == k) {
      cout << nex << endl;
      return 0;
    }
    if (!reached[nex]) {
      reached[nex] = true;
      cnt[nex] = sumcnt;
    } else {
      period = sumcnt - cnt[nex];
      now = nex;
      break;
    }
    now = nex;
  }
  k -= sumcnt;
  k %= period;
  rep(i, k) {
    int nex = to[now];
    now = nex;
  }
  cout << now << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

int to[110000];
bool reached[110000];
int cnt[110000];

int main() {
  int n;
  Int k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> to[i];
  }
  int now = 1;
  reached[1] = true;
  Int sumcnt = 0;
  int period = INF;
  while (true) {
    int nex = to[now];
    sumcnt++;
    if (sumcnt == k) {
      cout << nex << endl;
      return 0;
    }
    if (!reached[nex]) {
      reached[nex] = true;
      cnt[nex] = sumcnt;
    } else {
      period = sumcnt - cnt[nex];
      now = nex;
      break;
    }
    now = nex;
  }
  k -= sumcnt;
  k %= period;
  rep(i, k) {
    int nex = to[now];
    now = nex;
  }
  cout << now << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  cin >> N >> K; // N=4,K=5
  vector<int> to(N);
  rep(i, N) {
    cin >> A;

    to[i] = A - 1; // to{5,4,1,4,2,1}
  }
  vector<int> a(N, 0); // a{0,0,0,0,0,0}
  vector<int> b(1, 0);
  b[0] = 0;
  int c = 0;
  while (a[b[c]] == 0) {
    a[b[c]] = 1;
    b.push_back(to[b[c]]);
    c++;
  }

  int f = b.size();
  if (K < f) {
    cout << b[K] + 1 << endl;
  } else {
    // a{1,1,1,0,1,1}, b{0,5,1,4,2,1},c=5
    auto e = find(b.begin(), b.end(), b[c]);
    b.erase(b.begin(), e);
    int g = b.size();

    long long d = (K - (f - g)) % (c - (f - g));
    cout << b[d] + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  cin >> N >> K; // N=4,K=5
  vector<int> to(N);
  rep(i, N) {
    cin >> A;

    to[i] = A - 1; // to{5,4,1,4,2,1}
  }
  vector<int> a(N, 0); // a{0,0,0,0,0,0}
  vector<int> b(1, 0);
  b[0] = 0;
  int c = 0;
  while (a[b[c]] == 0) {
    a[b[c]] = 1;
    b.push_back(to[b[c]]);
    c++;
  }
  int f = b.size();
  // a{1,1,1,0,1,1}, b{0,5,1,4,2,1},c=5
  auto e = find(b.begin(), b.end(), b[c]);
  b.erase(b.begin(), e);
  int g = b.size();

  long long d = (K - (f - g)) % (c - (f - g));
  cout << b[d] + 1 << endl;

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

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define pb push_back

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b); i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a - 1)
#define trav(x, a) for (auto &x : a)

const int MX = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1061109567; // 4557430888798830399LL
const ld EPS = 1e-9;
const ld PI = acos(-1);

ll arr[MX];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int N;
  unsigned long long K;
  cin >> N >> K;

  FOR(i, 1, N + 1) cin >> arr[i];

  bitset<MX + 1> mk;
  mk.reset();

  vi tele;
  mk[1] = true;
  int cur = 1, cycle = -1;
  tele.pb(1);
  while (true) {
    cur = arr[cur];
    if (mk[cur]) {
      cycle = cur;
      break;
    }
    tele.pb(cur);
    mk[cur] = true;
  }

  assert(cycle != -1);

  vi c;
  bool e = false;
  trav(x, tele) {
    if (x == cycle) {
      e = true;
    }
    if (e)
      c.pb(x);
    else {
      if (K == 0) {
        cout << x << "\\n";
        exit(0);
      }
      K--;
    }
  }

  ll X = (K) % (sz(c));

  cout << c[X] << "\\n";
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define pb push_back

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b); i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a - 1)
#define trav(x, a) for (auto &x : a)

const int MX = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1061109567; // 4557430888798830399LL
const ld EPS = 1e-9;
const ld PI = acos(-1);

ll arr[MX];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int N;
  ll K;
  cin >> N >> K;

  FOR(i, 1, N + 1) cin >> arr[i];

  bitset<MX + 1> mk;
  mk.reset();

  vi tele;
  mk[1] = true;
  int cur = 1, cycle = -1;
  tele.pb(1);
  while (true) {
    cur = arr[cur];
    if (mk[cur]) {
      cycle = cur;
      break;
    }
    tele.pb(cur);
    mk[cur] = true;
  }

  vi c;
  bool e = false;
  trav(x, tele) {
    if (x == cycle) {
      e = true;
    }
    if (e)
      c.pb(x);
    else
      K--;
  }

  ll X = K % (sz(c));

  cout << c[X] << "\\n";
}