prithivMLmods/Coder-Stat huggingface dataset:
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  vector<bool> was(n, false);
  long long cycle = 1, now = 0;
  vector<long long> path;
  while (!was[now]) {
    was[now] = true;
    now = a[now];
    k--;
  }
  long long st = now;
  path.push_back(st);
  now = a[now];
  while (now != st) {
    path.push_back(now);
    cycle++;
    now = a[now];
  }
  k %= cycle;
  cout << path[k] + 1;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  vector<bool> was(n, false);
  long long cycle = 1, now = 0;
  vector<long long> path;
  while (!was[now]) {
    was[now] = true;
    now = a[now];
    k--;
    if (!k) {
      cout << now + 1;
      return 0;
    }
  }
  long long st = now;
  path.push_back(st);
  now = a[now];
  while (now != st) {
    path.push_back(now);
    cycle++;
    now = a[now];
  }
  k %= cycle;
  cout << path[k] + 1;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
// cand <genesis?>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll base = 7;
const ll N = 1e5 + 9;
const ll inf = 1000100009;
typedef pair<ll, ll> LL;

ll n, k, a[N], i, chk[N];
vector<ll> pos;
ll getpos(ll x) {
  for (ll i = 0; i < pos.size(); i++)
    if (pos[i] == x)
      return i;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  // freopen("test.INP", "r", stdin);
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  i = 1;
  while (chk[i] != 1) {
    // cout<<i<<"x\n";
    chk[i] = 1;
    pos.push_back(i);
    i = a[i];
  }
  ll p = getpos(i);
  // cout<<i<<"\n";
  if (k <= p)
    cout << pos[k];
  else {
    k -= p;
    k %= (pos.size() - p);
    cout << pos[p + k];
  }
}


Fixed submission:
// cand <genesis?>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll base = 7;
const ll N = 3e5 + 9;
const ll inf = 1000100009;
typedef pair<ll, ll> LL;

ll n, k, a[N], i, chk[N];
vector<ll> pos;
ll getpos(ll x) {
  for (ll i = 0; i < pos.size(); i++)
    if (pos[i] == x)
      return i;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  // freopen("test.INP", "r", stdin);
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  i = 1;
  while (chk[i] != 1) {
    // cout<<i<<"x\n";
    chk[i] = 1;
    pos.push_back(i);
    i = a[i];
  }
  ll p = getpos(i);
  // cout<<i<<"\n";
  if (k <= p)
    cout << pos[k];
  else {
    k -= p;
    k %= (pos.size() - p);
    cout << pos[p + k];
  }
}
Problem p02684 (C++)

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

int main() {
  int N, K, x, d1 = -1, d2 = -1;
  set<int> s;
  s.insert(0);

  cin >> N >> K;
  vector<int> A(N), t(N, 0);
  rep(i, N) cin >> A.at(i);

  rep(i, N - 1) {
    t.at(i + 1) = A.at(t.at(i)) - 1;
    if (s.count(t.at(i + 1))) {
      x = t.at(i + 1);
      break;
    }
    s.insert(t.at(i + 1));
  }

  rep(i, N) if (t.at(i) == x) if (d1 == -1) { d1 = i; }
  else {
    d2 = i;
    break;
  }

  if (K <= d1)
    cout << t.at(K) + 1 << endl;
  else
    cout << t.at(((K - d1) % (d2 - d1)) + d1) + 1 << endl;
}

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

int main() {
  int N, x, d1 = -1, d2 = -1;
  ll K;
  set<int> s;
  s.insert(0);

  cin >> N >> K;
  vector<int> A(N), t(N, 0);
  rep(i, N) cin >> A.at(i);

  rep(i, N - 1) {
    t.at(i + 1) = A.at(t.at(i)) - 1;
    if (s.count(t.at(i + 1))) {
      x = t.at(i + 1);
      break;
    }
    s.insert(t.at(i + 1));
  }

  rep(i, N) if (t.at(i) == x) if (d1 == -1) { d1 = i; }
  else {
    d2 = i;
    break;
  }

  if (K <= d1)
    cout << t.at(K) + 1 << endl;
  else
    cout << t.at(((K - d1) % (d2 - d1)) + d1) + 1 << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;

int main(void) {
  int N;
  long long K;
  int a;
  cin >> N >> K;

  Graph G(N);
  for (int i = 0; i < N; i++) {
    cin >> a;
    G[i].push_back(a);
  }
  vector<int> seen;
  vector<int> ord(N + 1, -1);

  int now = 1;
  int l = 0;
  int c = 1;
  while (1) {
    if (ord[now] != -1) {
      break;
    }
    ord[now] = seen.size();
    seen.push_back(now);
    now = G[now][0];
  }

  c = seen.size() - ord[now];
  l = ord[now];
  if (K < l) {
    cout << seen[K] << endl;
  } else {
    cout << seen[l + ((K - l) % c)] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;

int main(void) {
  int N;
  long long K;
  int a;
  cin >> N >> K;

  Graph G(N);
  for (int i = 0; i < N; i++) {
    cin >> a;
    G[i].push_back(a);
  }
  vector<int> seen;
  vector<int> ord(N + 1, -1);

  int now = 1;
  int l = 0;
  int c = 1;
  while (1) {
    if (ord[now] != -1) {
      break;
    }
    ord[now] = seen.size();
    seen.push_back(now);
    now = G[now - 1][0];
  }

  c = seen.size() - ord[now];
  l = ord[now];
  if (K < l) {
    cout << seen[K] << endl;
  } else {
    cout << seen[l + ((K - l) % c)] << endl;
  }
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const long long INF = 1LL << 60;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vector<bool> b(n, false);
  vector<int> c;
  int x = 0;
  int pos = 0;
  rep(i, n * 2) {
    if (!b[pos]) {
      b[pos] = true;
      c.push_back(pos);
      pos = a[pos];
    } else {
      x = pos;
      break;
    }
  }

  vector<int> d;
  bool ok = false;
  rep(i, c.size()) {
    if (c[i] == x)
      ok = true;
    if (ok)
      d.push_back(c[i]);
  }

  int l = d.size();
  int jama = c.size() - l;
  cout << d[((k - jama) % l)] + 1 << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const long long INF = 1LL << 60;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vector<bool> b(n, false);
  vector<int> c;
  int x = 0;
  int pos = 0;
  rep(i, n * 2) {
    if (!b[pos]) {
      b[pos] = true;
      c.push_back(pos);
      pos = a[pos];
    } else {
      x = pos;
      break;
    }
  }

  vector<int> d;
  bool ok = false;
  rep(i, c.size()) {
    if (c[i] == x)
      ok = true;
    if (ok)
      d.push_back(c[i]);
  }

  int l = d.size();
  int jama = c.size() - l;
  if (k < jama) {
    cout << c[k] + 1 << endl;
  } else {
    cout << d[(k - jama) % l] + 1 << endl;
  }

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long int p;
  cin >> p;
  vector<int> a(n), t(n, -1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
  }
  int start = 0;
  int m_time = 0;
  t[start] = m_time++;
  int cycle_length = 0;
  vector<pair<int, int>> cycle;
  while (true) {
    int prev = start;
    start = a[start];
    if (t[start] != -1) {
      cycle_length = t[prev] - t[start] + 1;
      for (int i = 0; i < n; i++) {
        if (t[i] >= t[start]) {
          cycle.push_back({t[i], i});
        }
      }
      break;
    }
    t[start] = m_time++;
  }
  p -= m_time - cycle_length;
  sort(cycle.begin(), cycle.end());
  p %= cycle_length;
  cout << cycle[p].second + 1 << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long int p;
  cin >> p;
  vector<int> a(n), t(n, -1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
  }
  int start = 0;
  int m_time = 0;
  t[start] = m_time++;
  int cycle_length = 0;
  vector<pair<int, int>> cycle;
  while (true) {
    int prev = start;
    start = a[start];
    if (t[start] != -1) {
      cycle_length = t[prev] - t[start] + 1;
      for (int i = 0; i < n; i++) {
        if (t[i] >= t[start]) {
          cycle.push_back({t[i], i});
        }
      }
      break;
    }
    t[start] = m_time++;
    if (1ll * m_time == p + 1) {
      cout << start + 1 << "\n";
      return 0;
    }
  }
  p -= m_time - cycle_length;
  sort(cycle.begin(), cycle.end());
  p %= cycle_length;
  cout << cycle[p].second + 1 << "\n";
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#define ll long long
#define N 10000
#define mod 1000000007
#define LL 100
using namespace std;

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

  map<ll, ll> data;
  ll count1 = 1;
  ll count2 = 0;
  ll ans = 1; // 現在の位置
  vector<ll> pp;
  data[1]++;
  // cout<<num[1];

  while (true) {
    // cout<<ans<<endl;
    ans = num[ans];
    if (data[ans] == 0) {
      count1++;
      data[ans]++;
    } else if (data[ans] == 1) {
      pp.push_back(ans);
      count2++;
      data[ans]++;
    } else
      break;
  }
  // cout<<pp[2]<<endl;
  ll p = k - count1;
  // cout<<count1<<endl;
  ll r = p % count2;
  // cout<<count2<<endl;
  cout << pp[r] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#define ll long long
#define N 10000
#define mod 1000000007
#define LL 100
using namespace std;

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

  map<ll, ll> data;
  ll count1 = 1;
  ll count2 = 0;
  ll ans = 1; // 現在の位置
  vector<ll> pp;
  data[1]++;
  // cout<<num[1];

  while (true) {
    // cout<<ans<<endl;
    ans = num[ans];
    if (data[ans] == 0) {
      count1++;
      data[ans]++;
    } else if (data[ans] == 1) {
      pp.push_back(ans);
      count2++;
      data[ans]++;
    } else
      break;
  }
  if (k >= count1) {
    ll p = k - count1;
    // cout<<count1<<endl;
    ll r = p % count2;
    // cout<<count2<<endl;
    cout << pp[r] << endl;
  } else {
    ans = 1;
    for (int i = 0; i < k; i++) {
      ans = num[ans];
    }
    cout << ans << endl;
  }
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define enld '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[6] = {1, 0, -1, 0, 1, 1};
const int dy[6] = {0, 1, 0, -1, 1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<ll> dist(N + 1, -1);
  ll now = A[1];
  dist[now] = 1;
  K--;
  if (K == 1) {
    cout << now << enld;
    return 0;
  }
  while (1) {
    if (dist[A[now]] != -1) {
      ll m = dist[now] + 1 - dist[A[now]];
      K %= m;
      while (K > 0) {
        now = A[now];
        K--;
      }
      break;
    } else {
      dist[A[now]] = dist[now] + 1;
      if (K == 0) {
        break;
      }
      now = A[now];
      K--;
    }
  }
  cout << now << enld;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define enld '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[6] = {1, 0, -1, 0, 1, 1};
const int dy[6] = {0, 1, 0, -1, 1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<ll> dist(N + 1, -1);
  ll now = A[1];
  dist[now] = 1;
  K--;
  if (K == 1) {
    cout << now << enld;
    return 0;
  }
  while (1) {
    if (dist[A[now]] != -1) {
      ll m = dist[now] + 1 - dist[A[now]];
      K %= m;
      while (K > 0) {
        now = A[now];
        K--;
      }
      break;
    } else {
      dist[A[now]] = dist[now] + 1;
      if (K == 0) {
        break;
      }
      now = A[now];
      K--;
    }
  }
  cout << now << enld;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

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

  int m[N + 1];
  rep(i, N) m[i + 1] = A[i];

  pair<int, bool> visit[N + 1];
  rep(i, N + 1) visit[i] = make_pair(0, false);

  int now = 1;

  int roop;
  int count;
  rep(i, K) {
    if (visit[now].second == true) {
      roop = i - visit[now].first;
      count = i;
      break;
    }
    visit[now] = make_pair(i, true);
    now = m[now];
  }

  K -= (long long)count;
  roop = K % (long long)roop;

  rep(i, roop) now = m[now];

  cout << now << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

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

  int m[N + 1];
  rep(i, N) m[i + 1] = A[i];

  pair<int, bool> visit[N + 1];
  rep(i, N + 1) visit[i] = make_pair(0, false);

  int now = 1;

  int roop = 1;
  int count = 1;
  rep(i, K) {
    if (visit[now].second == true) {
      roop = i - visit[now].first;
      count = i;
      break;
    }
    visit[now] = make_pair(i, true);
    now = m[now];
  }

  K -= (long long)count;
  roop = K % (long long)roop;

  rep(i, roop) now = m[now];

  cout << now << endl;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

int main(void) {

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

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

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

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

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

  cout << ans << endl;

  return 0;
}

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

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

int main(void) {

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

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

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

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

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

  cout << ans << endl;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> ipair;
typedef pair<ll, ll> lpair;
typedef tuple<int, int, int> ituple;

// const int INF = INT_MAX;
// const ll INF = LLONG_MAX;
// const int MOD = ((int)1e9 + 7);
// const ld EPS = (1e-10);
#define PI acosl(-1)
#define MAX_N (100000 + 2)
int n, a[MAX_N];
int firstVisited[MAX_N];
ll k;

void exec() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  int ans = 0, i = 1, current = 1;
  firstVisited[1] = i;
  bool isFindLoop = false;
  while (true) {
    if (k <= 0) {
      ans = current;
      break;
    }

    k--;
    i++;
    current = a[current - 1];
    // printf("loop %d: current = %d, k = %lld\n", i, current, k);

    if (!isFindLoop && firstVisited[current]) {
      ll loop = i - firstVisited[current];
      if (loop > 0) {
        // printf("loop = %lld, k = %lld\n", loop, k);
        k %= loop;
        // printf("after k = %lld\n", k);
      }

      isFindLoop = true;
    }

    firstVisited[current] = i;
  }

  cout << ans << endl;
}

void solve() {
  int t = 1;
  // scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    exec();
  }
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> ipair;
typedef pair<ll, ll> lpair;
typedef tuple<int, int, int> ituple;

// const int INF = INT_MAX;
// const ll INF = LLONG_MAX;
// const int MOD = ((int)1e9 + 7);
// const ld EPS = (1e-10);
#define PI acosl(-1)
#define MAX_N (200000 + 2)
int n, a[MAX_N];
int firstVisited[MAX_N];
ll k;

void exec() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  int ans = 0, i = 1, current = 1;
  firstVisited[1] = i;
  bool isFindLoop = false;
  while (true) {
    if (k <= 0) {
      ans = current;
      break;
    }

    k--;
    i++;
    current = a[current - 1];
    // printf("loop %d: current = %d, k = %lld\n", i, current, k);

    if (!isFindLoop && firstVisited[current]) {
      ll loop = i - firstVisited[current];
      if (loop > 0) {
        // printf("loop = %lld, k = %lld\n", loop, k);
        k %= loop;
        // printf("after k = %lld\n", k);
      }

      isFindLoop = true;
    }

    firstVisited[current] = i;
  }

  cout << ans << endl;
}

void solve() {
  int t = 1;
  // scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    exec();
  }
}

int main() {
  solve();
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

const ll LINF = 2e18;
const int INF = 1e9;

ll N, K;
vector<ll> A;

void solve() {
  vector<ll> r;
  map<ll, ll> seen;
  rep(i, N) { seen[i] = -1; }

  ll idx = 0;
  ll len = 0;

  while (true) {
    if (seen[idx] != -1) {
      break;
    }
    if (K <= len) {
      cout << idx + 1 << endl;
      return;
    }

    seen[idx] = len;
    r.push_back(idx);
    idx = A.at(idx);
    len++;
  }

  ll d;
  if (K - idx > 0) {
    d = (K - idx) % (len - idx);
  } else {
    d = K;
  }
  cout << r.at(d) + 1 << endl;
}

int main() {
  cin >> N >> K;
  A.resize(N);
  rep(i, N) {
    cin >> A.at(i);
    A.at(i)--; // 0-indexed
  }

  solve();

  return 0;
}


Fixed submission:
#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

const ll LINF = 2e18;
const int INF = 1e9;

ll N, K;
vector<ll> A;

void solve() {
  vector<ll> r;
  map<ll, ll> seen;
  rep(i, N) { seen[i] = -1; }

  ll idx = 0;
  ll len = 0;

  while (true) {
    if (seen[idx] != -1) {
      break;
    }
    if (K <= len) {
      cout << idx + 1 << endl;
      return;
    }

    seen[idx] = len;
    r.push_back(idx);
    idx = A.at(idx);
    len++;
  }

  ll d;
  if (K - seen[idx] >= 0) {
    if (len - seen[idx] <= 0) {
      d = seen[idx] + (K - seen[idx]);
    } else {
      d = seen[idx] + (K - seen[idx]) % (len - seen[idx]);
    }
  } else {
    d = K;
  }
  cout << r.at(d) + 1 << endl;
}

int main() {
  cin >> N >> K;
  A.resize(N);
  rep(i, N) {
    cin >> A.at(i);
    A.at(i)--; // 0-indexed
  }

  solve();

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const double PI = acos(-1);

int main() {
  long n, k;
  cin >> n >> k;
  vector<long> l(n);
  vector<long> lb(n, -1);
  vector<long> m;
  for (long i = 0; i < n; i++) {
    cin >> l[i];
  }
  long cnt = 0, next = 0, loop = -1;
  while (lb[next] == -1) {
    lb[next] = cnt;
    m.push_back(next + 1);
    next = l[next] - 1;
    cnt++;
  }

  m.erase(m.begin(), m.begin() + lb[next]);
  long remain = k - lb[next], hoge = remain % (cnt - lb[next]);
  cout << m[hoge] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const double PI = acos(-1);

int main() {
  long n, k;
  cin >> n >> k;
  vector<long> l(n);
  vector<long> lb(n, -1);
  vector<long> m;
  for (long i = 0; i < n; i++) {
    cin >> l[i];
  }
  long cnt = 0, next = 0;
  while (lb[next] == -1 && cnt <= k) {
    lb[next] = cnt;
    m.push_back(next + 1);
    next = l[next] - 1;
    cnt++;
  }

  m.erase(m.begin(), m.begin() + lb[next]);
  long remain = k - lb[next], hoge = remain % (cnt - lb[next]);
  cout << m[hoge] << endl;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
// #include<bits/stdc++.h>
// using namespace std;

// const int N = 10000, MAXI = 10000;

// int main() {
//     freopen("output.txt", "r", stdin);
//     freopen("input.txt", "w", stdout);
//     srand(time(nullptr));
//     int tc = 10;
//     cout << tc << "\n";
//     while (tc--)
//     {
//         int n = rand()%N+1;
//         cout << n << " ";
//         cout << rand()%n + 1 << "\n";
//         for(int i=1;i<=n;i++)
//             cout << rand()%MAXI + 1 << " ";
//         cout << "\n";
//         cout << "~\n";
//     }

//     return 0;
// }

//---------------------------------------------//

//  /* while(!cin.eof()) */

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9 + 9)
#define endl '\n'
#define MAXI (int)(3e17 + 10)
#define N 100005

int vis[N], a[N], last[N];
int n, k, ti = 0, ans;
vector<int> v;

void fun(int cur) {
  if (k == 0) {
    ans = cur;
    return;
  }
  if (vis[cur] == 1) {
    vector<int> x;
    for (int i = last[cur]; i < v.size(); i++)
      x.push_back(v[i]);

    int len = x.size();
    int pos = k % len;
    ans = x[pos];
    return;
  }
  v.push_back(cur);
  vis[cur] = 1;
  last[cur] = ti++;
  k--;
  fun(a[cur]);
}

// Driver code to test above functions
int32_t main() {

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

  cin >> n >> k;

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

  fun(1);

  cout << ans;

  return 0;
}

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

// const int N = 10000, MAXI = 10000;

// int main() {
//     freopen("output.txt", "r", stdin);
//     freopen("input.txt", "w", stdout);
//     srand(time(nullptr));
//     int tc = 10;
//     cout << tc << "\n";
//     while (tc--)
//     {
//         int n = rand()%N+1;
//         cout << n << " ";
//         cout << rand()%n + 1 << "\n";
//         for(int i=1;i<=n;i++)
//             cout << rand()%MAXI + 1 << " ";
//         cout << "\n";
//         cout << "~\n";
//     }

//     return 0;
// }

//---------------------------------------------//

//  /* while(!cin.eof()) */

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9 + 9)
#define endl '\n'
#define MAXI (int)(3e17 + 10)
#define N 200005

int vis[N], a[N], last[N];
int n, k, ti = 0, ans;
vector<int> v;

void fun(int cur) {
  if (k == 0) {
    ans = cur;
    return;
  }
  if (vis[cur] == 1) {
    vector<int> x;
    for (int i = last[cur]; i < v.size(); i++)
      x.push_back(v[i]);

    int len = x.size();
    int pos = k % len;
    ans = x[pos];
    return;
  }
  v.push_back(cur);
  vis[cur] = 1;
  last[cur] = ti++;
  k--;
  fun(a[cur]);
}

// Driver code to test above functions
int32_t main() {

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

  cin >> n >> k;

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

  fun(1);

  cout << ans;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t, i, j, temp1, temp2, x, y, z, sum = 0, o, n, m, k, d;
  cin >> n >> k;
  vector<ll> a(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> path;
  unordered_set<int> s;
  j = 1;
  for (i = 0; i <= k; i++) {
    if (s.find(j) == s.end()) {
      path.push_back(j);
      s.insert(j);
      j = a[j - 1];
    } else
      break;
  }
  for (i = 0; i < path.size(); i++) {
    if (path[i] == j) {
      break;
    }
    sum++;
  }
  temp1 = k - sum;
  cout << path[sum + (temp1 % (path.size() - sum))];
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t, i, j, temp1, temp2, x, y, z, sum = 0, o, n, m, k, d;
  cin >> n >> k;
  vector<ll> a(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> path;
  unordered_set<int> s;
  j = 1;
  for (i = 0; i <= k; i++) {
    if (s.find(j) == s.end()) {
      path.push_back(j);
      s.insert(j);
      j = a[j - 1];
    } else
      break;
  }
  for (i = 0; i < path.size(); i++) {
    if (path[i] == j) {
      break;
    }
    sum++;
  }
  if (sum == path.size()) {
    cout << path[path.size() - 1];
  } else {
    temp1 = k - sum;
    cout << path[sum + (temp1 % (path.size() - sum))];
  }
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <assert.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {

  unsigned long long int n, k;
  cin >> n >> k;
  vector<int> a(n + 1, -1);

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

  int now = 1;
  // naive
  if (k <= 200000) {
    // assert(now > 0);
    for (int i = 0; i < k; ++i) {
      // cout << now << " " << a[now] << endl;
      now = a[now];
    }
  } else {

    vector<int> isVisited(n, false);
    queue<int> q;
    int roop_point = 0;
    long long int roop_size = 0;

    while (1) {
      if (!isVisited[now]) {
        q.push(now);
        isVisited[now] = true;
        now = a[now];
      } else {
        roop_point = now;
        break;
      }
    }

    while (1) {
      int tmp = q.front();
      if (tmp != roop_point) {
        --k;
        q.pop();
      } else {
        roop_size = (long long int)q.size();
        break;
      }
    }

    long long int remnant = 0;
    if (roop_size != 0) {
      remnant = k % roop_size;
    }

    for (long long int i = 0; i < remnant; ++i) {
      now = a[now];
    }
  }

  cout << now << endl;

  return 0;
}

Fixed submission:
#include <assert.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {

  unsigned long long int n, k;
  cin >> n >> k;
  vector<int> a(n + 1, -1);

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

  int now = 1;
  // naive
  if (k <= 200000) {
    // assert(now > 0);
    for (int i = 0; i < k; ++i) {
      // cout << now << " " << a[now] << endl;
      now = a[now];
    }
  } else {

    vector<int> isVisited(n + 1, false);
    queue<int> q;
    int roop_point = 0;
    long long int roop_size = 0;

    while (1) {
      if (!isVisited[now]) {
        q.push(now);
        isVisited[now] = true;
        now = a[now];
      } else {
        roop_point = now;
        break;
      }
    }

    while (1) {
      int tmp = q.front();
      if (tmp != roop_point) {
        --k;
        q.pop();
      } else {
        roop_size = (long long int)q.size();
        break;
      }
    }

    long long int remnant = 0;
    if (roop_size != 0) {
      remnant = k % roop_size;
    }

    for (long long int i = 0; i < remnant; ++i) {
      now = a[now];
    }
  }

  cout << now << endl;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define SORT(v, n) std::sort(v, v + n);
#define VSORT(v) std::sort(v.begin(), v.end());
#define VRSORT(v) std::sort(v.rbegin(), v.rend());
#define ll long long
#define pb(a) push_back(a)
#define ALL(obj) (obj).begin(), (obj).end()

using namespace std;

const int INF = 99999999;
const ll LINF = 9999999999999;

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll n, k;

int main() {
  cin >> n >> k;
  vector<ll> history;
  vector<ll> ord(n + 1, -1);
  vector<ll> a(n);
  ll c = 1, l = 0;
  {
    int v = 1;
    while (ord[v] == -1) {
      ord[v] = history.size();
      history.push_back(v);
      v = a[v - 1];
    }
    // 2回目にきたとき-1回目のとき
    c = history.size() - ord[v];
    l = ord[v];
  }

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

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define SORT(v, n) std::sort(v, v + n);
#define VSORT(v) std::sort(v.begin(), v.end());
#define VRSORT(v) std::sort(v.rbegin(), v.rend());
#define ll long long
#define pb(a) push_back(a)
#define ALL(obj) (obj).begin(), (obj).end()

using namespace std;

const int INF = 99999999;
const ll LINF = 9999999999999;

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll n, k;

int main() {
  cin >> n >> k;
  vector<ll> history;
  vector<ll> ord(n + 1, -1);
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  ll c = 1, l = 0;
  {
    int v = 1;
    while (ord[v] == -1) {
      ord[v] = history.size();
      history.push_back(v);
      v = a[v - 1];
    }
    // 2回目にきたとき-1回目のとき
    c = history.size() - ord[v];
    l = ord[v];
  }

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

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 0; i < N; i++)
    cin >> A[i + 1];
  vector<ll> arrive(N + 1, -1);
  int cur = 1;
  ll t = 0;
  while (K > 0) {
    if (arrive[cur] != -1) {
      ll cycle = t - arrive[cur];
      K %= cycle;
      t += cycle;
    } else {
      arrive[cur] = t;
    }
    if (K > 0) {
      t++;
      K--;
      cur = A[cur];
    }
  }
  cout << cur << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 0; i < N; i++)
    cin >> A[i + 1];
  vector<ll> arrive(N + 1, -1);
  int cur = 1;
  ll t = 0;
  while (K > 0) {
    if (arrive[cur] != -1) {
      ll cycle = t - arrive[cur];
      if (cycle != 0)
        K %= cycle;
    } else {
      arrive[cur] = t;
    }
    if (K > 0) {
      t++;
      K--;
      cur = A[cur];
    }
  }
  cout << cur << endl;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define tie()                                                                  \
  ios_base ::sync_with_stdio(false);                                           \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long

const int N = 1e5 + 20;

bool vis[N];

int cnt = 0;

vector<int> g[N];
int last[N];

int dfs(int x, int k) {
  if (k == 0 or g[x].empty())
    return x;
  cnt++;
  if (vis[x]) {
    if (k > (cnt - last[x]))
      k %= (cnt - last[x]);
    if (k == 0 or g[x].empty())
      return x;
    return dfs(g[x][0], k - 1);
  }
  vis[x] = true;
  last[x] = cnt;
  return dfs(g[x][0], k - 1);
}

int32_t main() {
  tie();
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;

    int to;

    for (int i = 1; i <= n; i++) {
      cin >> to;
      g[i].push_back(to);
    }

    memset(vis, false, sizeof vis);
    cout << dfs(1, k) << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define tie()                                                                  \
  ios_base ::sync_with_stdio(false);                                           \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long

const int N = 2e5 + 20;

bool vis[N];

int cnt = 0;

vector<int> g[N];
int last[N];

int dfs(int x, int k) {
  if (k == 0 or g[x].empty())
    return x;
  cnt++;
  if (vis[x]) {
    if (k > (cnt - last[x]))
      k %= (cnt - last[x]);
    if (k == 0 or g[x].empty())
      return x;
    return dfs(g[x][0], k - 1);
  }
  vis[x] = true;
  last[x] = cnt;
  return dfs(g[x][0], k - 1);
}

int32_t main() {
  tie();
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;

    int to;

    for (int i = 1; i <= n; i++) {
      cin >> to;
      g[i].push_back(to);
    }

    memset(vis, false, sizeof vis);
    cout << dfs(1, k) << endl;
  }
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

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

  vector<int> town(N);
  for (int i = 0; i < N; i++) {
    cin >> town.at(i);
    town.at(i) = town.at(i) - 1;
  }

  int now = 0;
  vector<int> count(N, 0);
  long long roop_town, rest;
  bool flag = false;

  for (int i = 0; i < K; i++) {
    now = town.at(now);
    if (count.at(i) != 0) {
      flag = true;
      roop_town = i - count.at(now);
      rest = K - i - 1;
      break;
    }
    count.at(now) = i;
  }
  if (flag) {
    rest = rest % roop_town;
    for (int j = 0; j < rest; j++)
      now = town.at(now);
  }
  cout << now + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

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

  vector<int> town(N);
  for (int i = 0; i < N; i++) {
    cin >> town.at(i);
    town.at(i) = town.at(i) - 1;
  }

  int now = 0;
  vector<int> count(N, 0);
  long long roop_town, rest;
  bool flag = false;

  for (int i = 0; i < K; i++) {
    now = town.at(now);
    if (count.at(now) != 0) {
      flag = true;
      roop_town = i - count.at(now);
      rest = K - i - 1;
      break;
    }
    count.at(now) = i;
  }
  if (flag) {
    rest = rest % roop_town;
    for (int j = 0; j < rest; j++)
      now = town.at(now);
  }
  cout << now + 1 << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int n, t;
long long int k;

int A[100001];
map<int, bool> visited;

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

  cin >> n >> k;

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

  vector<int> vec;
  int now = 1;
  while (1) {
    if (visited[now]) {
      break;
    }
    visited[now] = true;
    vec.push_back(now);
    now = A[now];
  }

  int idx = 0;
  for (idx = 0; idx < vec.size(); idx++) {
    if (now == vec[idx]) {
      break;
    }
  }

  if (k < idx) {
    cout << vec[k] << '\n';
  } else {
    vector<int> circle;
    for (int j = idx; j < vec.size(); j++) {
      circle.push_back(vec[j]);
    }
    k -= idx;
    int m = circle.size();
    cout << circle[k % m] << '\n';
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n, t;
long long int k;

int A[200001];
map<int, bool> visited;

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

  cin >> n >> k;

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

  vector<int> vec;
  int now = 1;
  while (1) {
    if (visited[now]) {
      break;
    }
    visited[now] = true;
    vec.push_back(now);
    now = A[now];
  }

  int idx = 0;
  for (idx = 0; idx < vec.size(); idx++) {
    if (now == vec[idx]) {
      break;
    }
  }

  if (k < idx) {
    cout << vec[k] << '\n';
  } else {
    vector<int> circle;
    for (int j = idx; j < vec.size(); j++) {
      circle.push_back(vec[j]);
    }
    k -= idx;
    int m = circle.size();
    cout << circle[k % m] << '\n';
  }

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

int main() {
  int n;
  ll k;
  cin >> n >> k;
  int a[200000];
  rep(i, n) cin >> a[i], a[i]--;

  map<int, int> reached;
  int c = 0;
  vector<int> li;
  rep(i, k) {
    li.push_back(c);
    reached.insert(make_pair(c, i));
    c = a[c];
    if (reached.find(c) != reached.end())
      break;
  }
  int s = reached.at(c);
  int l = li.size() - s;
  int ansi = (k <= s) ? k : (s + (k - s) % l);
  int ans = li[ansi];

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


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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

int main() {
  int n;
  ll k;
  cin >> n >> k;
  int a[200000];
  rep(i, n) cin >> a[i], a[i]--;

  map<int, int> reached;
  int c = 0;
  vector<int> li;
  rep(i, n + 1) {
    li.push_back(c);
    reached.insert(make_pair(c, i));
    c = a[c];
    if (reached.find(c) != reached.end())
      break;
  }
  int s = reached.at(c);
  int l = li.size() - s;
  int ansi = (k <= s) ? k : (s + (k - s) % l);
  int ans = li[ansi];

  cout << ans + 1 << endl;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define pb push_back
#define si second
#define int long long int
using namespace std;
int a[100005], s, vis[100005];
vector<int> v, u;
void dfs(int v) {
  if (vis[v]) {
    s = v;

    return;
  }
  vis[v] = 1;
  dfs(a[v]);
}
void dfs1(int v) {
  if (v == s)
    return;
  u.pb(v);
  dfs1(a[v]);
}

int32_t main() {
  int n, k, i;
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    // mp[a[i]]=i;
  }
  dfs(1);
  i = 1;
  while (i != s) {
    v.pb(i);
    i = a[i];
  }
  u.pb(s);
  dfs1(a[s]);
  /*for(auto x:u)
          cout<<x<<" ";
  cout<<"\n";

  for(auto x:v)
          cout<<x<<" ";
  cout<<"\n";*/
  if (k < v.size())
    cout << v[k] << "\n";
  else {
    k -= v.size();
    int sz = u.size();
    cout << u[k % sz] << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define pb push_back
#define si second
#define int long long int
using namespace std;
int a[200005], s, vis[200005];
vector<int> v, u;
void dfs(int v) {
  if (vis[v]) {
    s = v;

    return;
  }
  vis[v] = 1;
  dfs(a[v]);
}
void dfs1(int v) {
  if (v == s)
    return;
  u.pb(v);
  dfs1(a[v]);
}

int32_t main() {
  int n, k, i;
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    // mp[a[i]]=i;
  }
  dfs(1);
  i = 1;
  while (i != s) {
    v.pb(i);
    i = a[i];
  }
  u.pb(s);
  dfs1(a[s]);
  /*for(auto x:u)
          cout<<x<<" ";
  cout<<"\n";

  for(auto x:v)
          cout<<x<<" ";
  cout<<"\n";*/
  if (k < v.size())
    cout << v[k] << "\n";
  else {
    k -= v.size();
    int sz = u.size();
    cout << u[k % sz] << "\n";
  }
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define INCANT                                                                 \
  cin.tie(0), ios::sync_with_stdio(false), cout << fixed << setprecision(20)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define out(a) cout << a << endl
#define PRINT(V)                                                               \
  for (auto v : (V))                                                           \
  cout << v << " "

const int MOD = 1000000007;
const double PI = acos(-1.0);

void solve() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> B;
  vector<int> C(N, -1);
  rep(i, N) {
    int ak;
    cin >> ak;
    A.at(i) = ak - 1;
  }
  int node = 0;
  ll i = 0;
  B.push_back(node);
  C[node] = i;
  ll init;
  ll loop;
  while (i < K) {
    node = A.at(node);
    i++;
    if (C[node] > -1) {
      init = C[node];
      loop = i - C[node];
      break;
    } else {
      C[node] = i;
      B.push_back(node);
    }
  }
  if (i == K - 1)
    out(C[node] + 1);
  else
    out(B.at(init + ((K - init) % loop)) + 1);
}
int main() {
  INCANT;
  solve();
  return 0;
}


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

#define INCANT                                                                 \
  cin.tie(0), ios::sync_with_stdio(false), cout << fixed << setprecision(20)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define out(a) cout << a << endl
#define PRINT(V)                                                               \
  for (auto v : (V))                                                           \
  cout << v << " "

const int MOD = 1000000007;
const double PI = acos(-1.0);

void solve() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> B;
  vector<int> C(N, -1);
  rep(i, N) {
    int ak;
    cin >> ak;
    A.at(i) = ak - 1;
  }
  int node = 0;
  ll i = 0;
  B.push_back(node);
  C[node] = i;
  ll init;
  ll loop;
  while (i < K) {
    node = A.at(node);
    i++;
    if (C[node] > -1) {
      init = C[node];
      loop = i - C[node];
      break;
    } else {
      C[node] = i;
      B.push_back(node);
    }
  }
  if (i == K)
    out(node + 1);
  else
    out(B.at(init + ((K - init) % loop)) + 1);
}
int main() {
  INCANT;
  solve();
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o << 1)
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 2e5 + 7;
/*
int head[M],cnt;
void init(){cnt=0,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,val;}ee[M*2];
void add(int x,int y){ee[++cnt].nxt=head[x],ee[cnt].to=y,head[x]=cnt;}
*/

int to[M];
int vs[M];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> to[i];
  int tp = 1;
  ll nm = 0;
  while (1) {
    vs[tp]++;
    nm++;
    if (nm == k) {
      cout << to[tp] << endl;
      return 0;
    }

    tp = to[tp];
    if (vs[tp] == 2)
      break;
  }
  ll nw = 0;
  for (int i = 1; i <= n; i++)
    if (vs[i] == 2)
      nw++;
  k -= nm;
  k %= nw;
  while (k) {
    tp = to[tp];
  }
  cout << tp << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o << 1)
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 2e5 + 7;
/*
int head[M],cnt;
void init(){cnt=0,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,val;}ee[M*2];
void add(int x,int y){ee[++cnt].nxt=head[x],ee[cnt].to=y,head[x]=cnt;}
*/

int to[M];
int vs[M];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> to[i];
  int tp = 1;
  ll nm = 0;
  while (1) {
    vs[tp]++;
    nm++;
    if (nm == k) {
      cout << to[tp] << endl;
      return 0;
    }

    tp = to[tp];
    if (vs[tp] == 2)
      break;
  }
  ll nw = 0;
  for (int i = 1; i <= n; i++)
    if (vs[i] == 2)
      nw++;
  k -= nm;
  k %= nw;
  while (k) {
    tp = to[tp];
    k--;
  }
  cout << tp << endl;
  return 0;
}