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

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

const int MAXN = 100005;
const int INF = 0x7f7f7f7f;

int A[MAXN];
int L[MAXN];
int Re[MAXN];
bool Vis[MAXN];

int main() {
  int N;
  long long K;
  cin >> N >> K;
  for (int i = 1; i <= N; ++i)
    cin >> A[i];

  int r = 0, pos = 1;
  Vis[1] = true;
  L[0] = 1;
  Re[1] = 0;

  while (1) {
    pos = A[pos];
    L[++r] = pos;

    if (Vis[pos])
      break;
    Re[pos] = r;
    Vis[pos] = true;
  }

  K = min(K, 1ll * Re[pos]) + max(0ll, K - Re[pos]) % (r - Re[pos]);

  cout << L[K] << endl;

  return 0;
}


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

const int MAXN = 200005;
const int INF = 0x7f7f7f7f;

int A[MAXN];
int L[MAXN];
int Re[MAXN];
bool Vis[MAXN];

int main() {
  int N;
  long long K;
  cin >> N >> K;
  for (int i = 1; i <= N; ++i)
    cin >> A[i];

  int r = 0, pos = 1;
  Vis[1] = true;
  L[0] = 1;
  Re[1] = 0;

  while (1) {
    pos = A[pos];
    L[++r] = pos;

    if (Vis[pos])
      break;
    Re[pos] = r;
    Vis[pos] = true;
  }

  K = min(K, 1ll * Re[pos]) + max(0ll, K - Re[pos]) % (r - Re[pos]);

  cout << L[K] << endl;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  const int MAX_N = 1e5 + 10;
  ll N, K;
  int A[MAX_N], B[MAX_N];

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

  B[0] = 0;
  int v = 0, cost = 0;
  while (B[A[v]] == -1) {
    B[A[v]] = ++cost;
    v = A[v];
  }

  int offset = B[A[v]];
  int mod = ++cost - offset;

  int step = K <= offset ? K : (K - offset) % mod + offset;

  int res = 0;
  for (int i = 0; i < step; ++i) {
    res = A[res];
  }
  cout << res + 1 << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  const int MAX_N = 2e5 + 10;
  ll N, K;
  int A[MAX_N], B[MAX_N];

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

  B[0] = 0;
  int v = 0, cost = 0;
  while (B[A[v]] == -1) {
    B[A[v]] = ++cost;
    v = A[v];
  }

  int offset = B[A[v]];
  int mod = ++cost - offset;

  int step = K <= offset ? K : (K - offset) % mod + offset;

  int res = 0;
  for (int i = 0; i < step; ++i) {
    res = A[res];
  }
  cout << res + 1 << endl;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int64_t N, K;
  cin >> N >> K;
  int32_t Ai[100001];
  int64_t Aiused[100001];
  int32_t realstate[100000];

  for (int i = 1; i <= N; ++i) {
    cin >> Ai[i];
    Aiused[i] = -1;
    realstate[i - 1] = 0;
  }
  int64_t mod = K + 1;
  int64_t offset = 0;

  Aiused[1] = 0;
  realstate[0] = 1;
  int32_t jump = Ai[1];
  int64_t i;
  for (i = 1; i <= K; i++) {
    if (Aiused[jump] < 0) {
      Aiused[jump] = i;
      realstate[i] = jump; /*current city*/
      jump = Ai[jump];     /*next city*/
    } else {
      offset = Aiused[jump];
      mod = i - offset;
      break;
    }
    //        cout<<i<<"city"<<realstate[i]<<"next"<<jump<<endl;
  }
  /*
  cout<<Ai[jump]<<endl;
  cout<<i<<endl;
  cout<<mod<<endl;
  cout<<offset<<endl;
  */
  uint64_t valnum = offset + ((K - offset) % mod);

  cout << realstate[valnum] << endl;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int64_t N, K;
  cin >> N >> K;
  int32_t Ai[200001];
  int64_t Aiused[200001];
  int32_t realstate[200000];

  for (int i = 1; i <= N; ++i) {
    cin >> Ai[i];
    Aiused[i] = -1;
    realstate[i - 1] = 0;
  }
  int64_t mod = K + 1;
  int64_t offset = 0;

  Aiused[1] = 0;
  realstate[0] = 1;
  int32_t jump = Ai[1];
  int64_t i;
  for (i = 1; i <= K; i++) {
    if (Aiused[jump] < 0) {
      Aiused[jump] = i;
      realstate[i] = jump; /*current city*/
      jump = Ai[jump];     /*next city*/
    } else {
      offset = Aiused[jump];
      mod = i - offset;
      break;
    }
    //        cout<<i<<"city"<<realstate[i]<<"next"<<jump<<endl;
  }
  /*
  cout<<Ai[jump]<<endl;
  cout<<i<<endl;
  cout<<mod<<endl;
  cout<<offset<<endl;
  */
  uint64_t valnum = offset + ((K - offset) % mod);

  cout << realstate[valnum] << endl;
}
Problem p02684 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, K;
  cin >> N >> K;
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i], A[i]--;
  map<pair<int, long long>, int> memo;
  function<int(int, long long)> rec = [&](int v, long long k) {
    if (memo.count(make_pair(v, k)))
      return memo[make_pair(v, k)];
    if (k == 1)
      return A[v];
    int u = rec(v, k / 2);
    if (k & 1)
      u = A[u];
    return memo[make_pair(v, k)] = rec(u, k / 2);
  };
  cout << rec(0, K) + 1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, K;
  cin >> N >> K;
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i], A[i]--;
  int to[80][N];
  for (int i = 0; i < N; i++)
    to[0][i] = A[i];
  for (int k = 1; k < 80; k++) {
    for (int i = 0; i < N; i++) {
      to[k][i] = to[k - 1][to[k - 1][i]];
    }
  }
  int ans = 0;
  for (int j = 63; j >= 0; j--) {
    if ((K >> j) & 1)
      ans = to[j][ans];
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, a;
string k;
int G[100010];
bool used[100010];

bool comp(string a, string b) {
  if (a.length() != b.length())
    return a.length() > b.length();
  else
    return a > b;
}

signed main() {
  cin >> N >> k;
  a = 0;
  rep(i, 0, N) {
    int to;
    cin >> to;
    G[i] = to - 1;
  }

  vector<int> v;
  int C = 0, T = 0;
  while (1) {
    used[a] = true;
    v.push_back(a);
    a = G[a];
    if (used[a])
      break;
  }

  T = find(v.begin(), v.end(), a) - v.begin();
  C = v.size() - T;

  if (comp(k, to_string(T))) {
    int mo = 0;
    rep(i, 0, k.length()) { mo = (mo * 10 + k[i] - '0') % C; }
    mo = (mo - (T % C) + C) % C;
    cout << v[T + mo] + 1 << endl;
  } else {
    int step = atoi(k.c_str());
    cout << v[step] + 1 << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, a;
string k;
int G[300010];
bool used[300010];

bool comp(string a, string b) {
  if (a.length() != b.length())
    return a.length() > b.length();
  else
    return a > b;
}

signed main() {
  cin >> N >> k;
  a = 0;
  rep(i, 0, N) {
    int to;
    cin >> to;
    G[i] = to - 1;
  }

  vector<int> v;
  int C = 0, T = 0;
  while (1) {
    used[a] = true;
    v.push_back(a);
    a = G[a];
    if (used[a])
      break;
  }

  T = find(v.begin(), v.end(), a) - v.begin();
  C = v.size() - T;

  if (comp(k, to_string(T))) {
    int mo = 0;
    rep(i, 0, k.length()) { mo = (mo * 10 + k[i] - '0') % C; }
    mo = (mo - (T % C) + C) % C;
    cout << v[T + mo] + 1 << endl;
  } else {
    int step = atoi(k.c_str());
    cout << v[step] + 1 << endl;
  }
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 2000;
const int X = 64;

int main() {
  unsigned long long N, K;
  int A[MAX][X];

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

  for (int i = 1; i < X; i++) {
    for (int j = 0; j < N; j++) {
      A[j][i] = A[A[j][i - 1]][i - 1];
    }
  }

  int ans = 0;
  int cnt = X - 1;

  while (K) {
    while (K >= (1ULL << cnt)) {
      ans = A[ans][cnt];
      K -= (1ULL << cnt);
    }
    cnt--;
  }

  cout << ans + 1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 200000;
const int X = 64;

int main() {
  unsigned long long N, K;
  int A[MAX][X];

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

  for (int i = 1; i < X; i++) {
    for (int j = 0; j < N; j++) {
      A[j][i] = A[A[j][i - 1]][i - 1];
    }
  }

  int ans = 0;
  int cnt = X - 1;

  while (K) {
    while (K >= (1ULL << cnt)) {
      ans = A[ans][cnt];
      K -= (1ULL << cnt);
    }
    cnt--;
  }

  cout << ans + 1 << endl;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define MAX 20000005
#define fastcin                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;

ll vis[MAX], cycle[MAX];
map<ll, ll> ttime;
map<ll, ll> inverstime;

void solve() {
  ll n, k;
  cin >> n >> k;

  ll v[n + 1];

  for (int i = 1; i <= n; i++)
    cin >> v[i];

  ll j = 1, t = 0;
  while (vis[j] == 0) {
    ttime[t] = j;
    inverstime[j] = t;
    vis[j] = 1;
    j = v[j];
    t++;
  }

  if (k < t) {
    cout << ttime[k] << endl;
    return;
  }

  ll init = j, d = t - inverstime[j];
  cycle[0] = init;
  j = v[init];

  for (int i = 1;; i++) {
    if (j == init)
      break;
    cycle[i] = j;
    j = v[j];
  }

  k -= ttime[init];
  cout << cycle[k % d] << endl;
}

int main() {

  fastcin solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define MAX 20000005
#define fastcin                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;

ll vis[MAX], cycle[MAX];
map<ll, ll> ttime;
map<ll, ll> inverstime;

void solve() {
  ll n, k;
  cin >> n >> k;

  ll v[n + 1];

  for (int i = 1; i <= n; i++)
    cin >> v[i];

  ll j = 1, t = 0;
  while (vis[j] == 0) {
    ttime[t] = j;
    inverstime[j] = t;
    vis[j] = 1;
    j = v[j];
    t++;
  }

  if (k < t) {
    cout << ttime[k] << endl;
    return;
  }

  ll init = j, d = t - inverstime[j];
  cycle[0] = init;
  j = v[init];

  for (int i = 1;; i++) {
    if (j == init)
      break;
    cycle[i] = j;
    j = v[j];
  }

  k -= t - d;

  cout << cycle[k % d] << endl;
}

int main() {

  fastcin solve();
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
// AtCoder template
// 最近sabaがかまってくれないからキレて暴れてる
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)

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

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

  // サイクル検出
  vector<bool> visited(n, false);
  int now = 0;
  int start;
  while (true) {
    if (visited[now]) {
      start = now;
      break;
    }
    visited[now] = true;
    now = a[now];
  }

  // サイクル長を求める
  ll cycle_d = 0;
  now = start;
  visited.assign(n, false);
  vector<int> cycle;
  while (true) {
    if (visited[now]) {
      break;
    }
    cycle.push_back(now);
    visited[now] = true;
    now = a[now];
    cycle_d++;
  }

  // 0からサイクル始点までの距離を求める
  ll dist = 0;
  now = 0;
  while (true) {
    if (now == start) {
      break;
    }
    now = a[now];
    dist++;
  }

  // 計算

  // 出力
  cout << cycle[(k - dist) % cycle_d] + 1 << endl;
}

Fixed submission:
// AtCoder template
// 最近sabaがかまってくれないからキレて暴れてる
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)

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

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

  // サイクル検出
  vector<bool> visited(n, false);
  int now = 0;
  int start;
  while (true) {
    if (visited[now]) {
      start = now;
      break;
    }
    visited[now] = true;
    now = a[now];
  }

  // サイクル長を求める
  ll cycle_d = 0;
  now = start;
  visited.assign(n, false);
  vector<int> cycle;
  while (true) {
    if (visited[now]) {
      break;
    }
    cycle.push_back(now);
    visited[now] = true;
    now = a[now];
    cycle_d++;
  }

  // 0からサイクル始点までの距離を求める
  ll dist = 0;
  now = 0;
  while (true) {
    if (now == start) {
      break;
    }
    now = a[now];
    dist++;
  }

  // 計算

  // 出力
  if (k >= dist)
    cout << cycle[(k - dist) % cycle_d] + 1 << endl;
  else {
    now = 0;
    while (k) {
      now = a[now];
      k--;
    }
    cout << now + 1 << endl;
  }
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> Vl;
typedef pair<int, int> pii;
typedef vector<pii> Vll;
typedef vector<pair<int, pair<int, int>>> Vlll;
#define S second
#define F first
#define mp make_pair
#define ms(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define sl(n) cin >> n
#define pl(n) cout << n << " "
const int Nx = 1e5 + 2;
const int inf = (int)(1e9 + 10LL);
void solve() {
  int n, k;
  sl(n);
  sl(k);
  Vl arr(n + 1);
  for (int i = 1; i <= n; i++)
    sl(arr[i]);
  unordered_map<int, int> freq;
  int i = 1;
  int pivot, start = 1;
  freq[1] = 0;
  int value;
  for (i = 1; i <= k; i++) {
    start = arr[start];
    if (freq[start] > 0) {
      pivot = i - freq[start];
      value = start;
      break;
    }
    freq[start] = i;
    // cout<<start<<" ";
  }
  k = (k - freq[value]) % pivot;
  start = value;
  for (int i = 1; i <= k; i++) {
    start = arr[start];
  }
  cout << start << endl;
}

signed main() {
  int test = 1;
  // sl(test);
  while (test--) {
    solve();
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> Vl;
typedef pair<int, int> pii;
typedef vector<pii> Vll;
typedef vector<pair<int, pair<int, int>>> Vlll;
#define S second
#define F first
#define mp make_pair
#define ms(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define sl(n) cin >> n
#define pl(n) cout << n << " "
const int Nx = 1e5 + 2;
const int inf = (int)(1e9 + 10LL);
void solve() {
  int n, k;
  sl(n);
  sl(k);
  Vl arr(n + 1);
  for (int i = 1; i <= n; i++)
    sl(arr[i]);
  unordered_map<int, int> freq;
  int i = 1;
  int pivot, start = 1;
  freq[1] = 0;
  int value;
  for (i = 1; i <= k; i++) {
    start = arr[start];
    if (freq[start] > 0) {
      pivot = i - freq[start];
      value = start;
      break;
    }
    freq[start] = i;
    // cout<<start<<" ";
  }
  if (i >= k) {
    cout << start << endl;
    return;
  }

  k = (k - freq[value]) % pivot;
  start = value;
  for (int i = 1; i <= k; i++) {
    start = arr[start];
  }
  cout << start << endl;
}

signed main() {
  int test = 1;
  // sl(test);
  while (test--) {
    solve();
  }
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
/*"Success isn't permanent, failure isn't fatal,
                             it's the courage to continue that counts"*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\n")
#define priln(n) printf("%d\n", n)
#define lpriln(n) printf("%lld\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
const int mod = 1e9 + 7;
const int N = 1e5 + 4;
const int lim = 61;

int dp[N][64];

int main() {
  int n;
  ll k;

  scn(n);
  lscn(k);

  rep(i, 1, n + 1) scn(dp[i][0]);

  rep(i, 1, lim) {
    rep(node, 1, n + 1) dp[node][i] = dp[dp[node][i - 1]][i - 1];
  }

  int cur = 1;

  rep(i, 0, lim) if (k & (1LL << i)) cur = dp[cur][i];

  pri(cur);

  return 0;
}


Fixed submission:
/*"Success isn't permanent, failure isn't fatal,
                             it's the courage to continue that counts"*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\n")
#define priln(n) printf("%d\n", n)
#define lpriln(n) printf("%lld\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
const int mod = 1e9 + 7;
const int N = 2e5 + 4;
const int lim = 61;

int dp[N][64];

int main() {
  int n;
  ll k;

  scn(n);
  lscn(k);

  rep(i, 1, n + 1) scn(dp[i][0]);

  rep(i, 1, lim) {
    rep(node, 1, n + 1) dp[node][i] = dp[dp[node][i - 1]][i - 1];
  }

  int cur = 1;

  rep(i, 0, lim) if (k & (1LL << i)) cur = dp[cur][i];

  pri(cur);

  return 0;
}
Problem p02684 (C++)

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

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

  vector<lint> A(N);
  for (auto &elem : A)
    cin >> elem;
  for (auto &elem : A)
    elem--;

  if (K <= N + N) {
    lint idx = 0;
    for (lint i = 0; i < K; i++) {
      idx = A[idx];
    }
    cout << ++idx << endl;
    return 0;
  }

  vector<lint> v;
  map<lint, lint> prev;
  lint idx = 0, loop;
  for (lint i = 0;; i++) {
    if (prev.count(idx)) {
      loop = i - prev[idx];
      break;
    }
    prev[idx] = i;
    v.push_back(idx);
    idx = A[idx];
  }
  K -= (v.size() - loop);
  vector<lint> vec(v.end() - loop, v.end());
  cout << ++vec[K % v.size()] << endl;
}

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

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

  vector<lint> A(N);
  for (auto &elem : A)
    cin >> elem;
  for (auto &elem : A)
    elem--;

  if (K <= N + N) {
    lint idx = 0;
    for (lint i = 0; i < K; i++) {
      idx = A[idx];
    }
    cout << ++idx << endl;
    return 0;
  }

  vector<lint> v;
  map<lint, lint> prev;
  lint idx = 0, loop;
  for (lint i = 0;; i++) {
    if (prev.count(idx)) {
      loop = i - prev[idx];
      break;
    }
    prev[idx] = i;
    v.push_back(idx);
    idx = A[idx];
  }
  K -= (v.size() - loop);
  vector<lint> vec(v.end() - loop, v.end());
  cout << ++vec[K % vec.size()] << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m, tot, now, ans, sum;
int a[N], b[N], c[N], f[N];
signed main() {
  scanf("%lld%lld", &n, &m);
  for (register int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  if (m <= n) {
    now = 1;
    for (register int i = 1; i <= m; ++i)
      now = a[now];
    printf("%lld\n", now);
    return 0;
  }

  m++;
  now = 1;
  tot = 1;
  f[1] = tot;
  b[tot] = 1;
  while (true) {
    if (f[a[now]]) {
      m -= f[a[now]] - 1;
      for (register int i = 1; i <= tot - f[a[now]] + 1; ++i)
        c[i] = b[i + f[a[now]] - 1];
      break;
    }
    tot++;
    f[a[now]] = tot;
    b[tot] = a[now];
    now = a[now];
  }
  //	for (register int i=1; i<=tot-f[a[now]]+1; ++i) printf("%lld ",c[i]);
  //puts("");
  sum = tot - f[a[now]] + 1;
  if (m % sum == 0)
    ans = sum;
  else
    ans = m % sum;
  printf("%lld\n", c[ans]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n, m, tot, now, ans, sum;
int a[N], b[N], c[N], f[N];
signed main() {
  scanf("%lld%lld", &n, &m);
  for (register int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  if (m <= n) {
    now = 1;
    for (register int i = 1; i <= m; ++i)
      now = a[now];
    printf("%lld\n", now);
    return 0;
  }

  m++;
  now = 1;
  tot = 1;
  f[1] = tot;
  b[tot] = 1;
  while (true) {
    if (f[a[now]]) {
      m -= f[a[now]] - 1;
      for (register int i = 1; i <= tot - f[a[now]] + 1; ++i)
        c[i] = b[i + f[a[now]] - 1];
      break;
    }
    tot++;
    f[a[now]] = tot;
    b[tot] = a[now];
    now = a[now];
  }
  //	for (register int i=1; i<=tot-f[a[now]]+1; ++i) printf("%lld ",c[i]);
  //puts("");
  sum = tot - f[a[now]] + 1;
  if (m % sum == 0)
    ans = sum;
  else
    ans = m % sum;
  printf("%lld\n", c[ans]);
  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##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> vec(N + 1);
  REP(i, N + 1) vec[i] = 0;
  vector<ll> path;
  ll tmp = 1;
  vec[0] = 1;
  ll ans = 0;
  path.push_back(vec[0]);
  ll cnt = 0;
  for (ll i = 0; i < K; ++i) {
    if (vec[tmp] == 0) {
      vec[tmp] = A[tmp - 1];
      tmp = A[tmp - 1];
      path.push_back(tmp);
      cnt++;
    } else if (vec[tmp] != 0) {
      // なんらかの処理
      ans = tmp;
      break;
    }
  }

  vector<ll> b;
  bool flag = false;
  REP(i, path.size()) {
    if (path[i] == ans)
      flag = !flag;
    if (flag == true)
      b.push_back(path[i]);
  }
  cout << b[(K - (cnt - b.size())) % b.size()] << endl;
}


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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> vec(N + 1);
  REP(i, N + 1) vec[i] = 0;
  vector<ll> path;
  ll tmp = 1;
  vec[0] = 1;
  ll ans = 0;
  path.push_back(vec[0]);
  ll cnt = 0;
  for (ll i = 0; i < K; ++i) {
    if (vec[tmp] == 0) {
      vec[tmp] = A[tmp - 1];
      tmp = A[tmp - 1];
      path.push_back(tmp);
      cnt++;
    } else if (vec[tmp] != 0) {
      // なんらかの処理
      ans = tmp;
      break;
    }
  }

  vector<ll> b;
  bool flag = false;
  REP(i, path.size()) {
    if (path[i] == ans)
      flag = !flag;
    if (flag == true)
      b.push_back(path[i]);
  }

  if (K - (cnt - b.size()) <= 0) {
    cout << path[K] << endl;
  } else {
    ll c = (K - (cnt - b.size())) % b.size();
    cout << b[c] << endl;
  }
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int N = 1e5 + 7;
const int K = 62;

int up[K][N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, nd;
  cin >> n >> nd;
  for (int i = 0; i < n; i++) {
    cin >> up[0][i];
    up[0][i]--;
  }
  for (int i = 0; i + 1 < K; i++) {
    for (int j = 0; j < n; j++) {
      up[i + 1][j] = up[i][up[i][j]];
    }
  }
  int p = 0;
  for (int i = 0; i < K; i++) {
    if ((nd >> i) & 1)
      p = up[i][p];
  }
  cout << p + 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int N = 2e5 + 7;
const int K = 62;

int up[K][N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, nd;
  cin >> n >> nd;
  for (int i = 0; i < n; i++) {
    cin >> up[0][i];
    up[0][i]--;
  }
  for (int i = 0; i + 1 < K; i++) {
    for (int j = 0; j < n; j++) {
      up[i + 1][j] = up[i][up[i][j]];
    }
  }
  int p = 0;
  for (int i = 0; i < K; i++) {
    if ((nd >> i) & 1)
      p = up[i][p];
  }
  cout << p + 1 << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define multi_test true and false
#define all(a) (a).begin(), (a).end()
using namespace std;

void dfs(int v, vector<bool> &bani, vector<int> &path, int arr[]) {
  if (bani[v]) {
    path.push_back(v);
    return;
  }
  bani[v] = true;
  path.push_back(v);
  dfs(arr[v], bani, path, arr);
}

void solve() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int &i : arr)
    cin >> i;

  for (int &i : arr)
    i--;

  vector<int> path;
  vector<bool> bani(n, false);
  dfs(0, bani, path, arr);

  int index = -1;
  for (int i = 0; i < (int)path.size(); i++) {
    if (path[i] == path.back()) {
      index = i;
      break;
    }
  }
  vector<int> new_path(path.begin() + index, path.end());
  // for(int i : new_path) cout << i << ' ';
  // cout << endl ;
  k -= index;
  n = (int)new_path.size() - 1;
  k = k % n;
  //    cout << k << ' ' << n << endl ;
  cout << new_path[k] + 1 << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int test = 1;
  if (multi_test)
    cin >> test;
  while (test--)
    solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define multi_test true and false
#define all(a) (a).begin(), (a).end()
using namespace std;

void dfs(int v, vector<bool> &bani, vector<int> &path, int arr[]) {
  if (bani[v]) {
    path.push_back(v);
    return;
  }
  bani[v] = true;
  path.push_back(v);
  dfs(arr[v], bani, path, arr);
}

void solve() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int &i : arr)
    cin >> i;

  for (int &i : arr)
    i--;

  vector<int> path;
  vector<bool> bani(n, false);
  dfs(0, bani, path, arr);

  int index = -1;
  for (int i = 0; i < (int)path.size(); i++) {
    if (path[i] == path.back()) {
      index = i;
      break;
    }
  }
  if (k < (int)path.size()) {
    cout << path[k] + 1 << endl;
    return;
  }
  vector<int> new_path(path.begin() + index, path.end());
  // for(int i : new_path) cout << i << ' ';
  // cout << endl ;
  k -= index;
  n = (int)new_path.size() - 1;
  k = k % n;
  //    cout << k << ' ' << n << endl ;
  cout << new_path[k] + 1 << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int test = 1;
  if (multi_test)
    cin >> test;
  while (test--)
    solve();

  return 0;
}
Problem p02684 (C++)

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<vector<int>> next(60, vector<int>(N));
  for (int j = 0; j < N; j++) {
    cin >> next.at(0).at(j);
    next.at(0).at(j)--;
  }
  for (int i = 0; i < 59; i++) {
    for (int j = 0; j < N; j++) {
      next.at(i + 1).at(j) = next.at(i).at(next.at(i).at(j));
    }
  }
  int j = 0;
  for (int i = 0; j < 60; j++) {
    if (K & (1LL << i)) {
      j = next.at(i).at(j);
    }
  }
  cout << j + 1 << endl;
}

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<vector<int>> next(60, vector<int>(N));
  for (int j = 0; j < N; j++) {
    cin >> next.at(0).at(j);
    next.at(0).at(j)--;
  }
  for (int i = 0; i < 59; i++) {
    for (int j = 0; j < N; j++) {
      next.at(i + 1).at(j) = next.at(i).at(next.at(i).at(j));
    }
  }
  int j = 0;
  for (int i = 0; i < 60; i++) {
    if (K & (1LL << i)) {
      j = next.at(i).at(j);
    }
  }
  cout << j + 1 << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define eb(t) emplace_back(t)
typedef long long ll;
typedef long long unsigned int llu;
ll INF = 1000000009;
ll MOD = 1000000007;

ll a[20010];

void solve() {

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

  set<ll> st;
  vector<ll> v;
  v.eb(0);
  st.insert(0);
  ll pointer = 0;
  ll t1 = 0;
  ll count1 = 1;
  while (true) {
    // cout << pointer << endl;
    decltype(st)::iterator it = st.find(a[pointer]);
    if (it != st.end()) { // 見つかった
      // cout << "find" << *it << std::endl;
      t1 = *it;
      break;
    }
    st.insert(a[pointer]);
    pointer = a[pointer];
    count1++;
    v.eb(pointer);
  }

  ll count2 = 1;
  ll tm = 0;
  while (true) {
    if (a[tm] != t1) {
      tm = a[tm];
      count2++;
    } else {
      break;
    }
  }

  /*
  for(auto e:v){
      cout << e << "!" << endl;
  }
  */

  // cout << count1 << " " << count2 << endl;

  ll y, x;
  if (count1 == count2) {
    y = count1;
    x = 0;
  } else {
    y = count1 - count2;
    x = count2;
  }

  if (k < x) {
    cout << v[k] + 1 << endl;
    return;
  }

  /*
  cout << v.size() << endl;
  cout << k << endl;
  cout << x << endl;
  cout << y << endl;

  cout << (k-x)%y+x << endl;
  */
  cout << v[(k - x) % y + x] + 1 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define eb(t) emplace_back(t)
typedef long long ll;
typedef long long unsigned int llu;
ll INF = 1000000009;
ll MOD = 1000000007;

ll a[200100];

void solve() {

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

  set<ll> st;
  vector<ll> v;
  v.eb(0);
  st.insert(0);
  ll pointer = 0;
  ll t1 = 0;
  ll count1 = 1;
  while (true) {
    // cout << pointer << endl;
    decltype(st)::iterator it = st.find(a[pointer]);
    if (it != st.end()) { // 見つかった
      // cout << "find" << *it << std::endl;
      t1 = *it;
      break;
    }
    st.insert(a[pointer]);
    pointer = a[pointer];
    count1++;
    v.eb(pointer);
  }

  ll count2 = 1;
  ll tm = 0;
  while (true) {
    if (a[tm] != t1) {
      tm = a[tm];
      count2++;
    } else {
      break;
    }
  }

  /*
  for(auto e:v){
      cout << e << "!" << endl;
  }
  */

  // cout << count1 << " " << count2 << endl;

  ll y, x;
  if (count1 == count2) {
    y = count1;
    x = 0;
  } else {
    y = count1 - count2;
    x = count2;
  }

  if (k < x) {
    cout << v[k] + 1 << endl;
    return;
  }

  /*
  cout << v.size() << endl;
  cout << k << endl;
  cout << x << endl;
  cout << y << endl;

  cout << (k-x)%y+x << endl;
  */
  cout << v[(k - x) % y + x] + 1 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
#define DEBUG(x) cout << x << '\n'

int n, a[MAXN], vis[MAXN], pos[MAXN];
ll k;
int dfs(int u) {
  if (~vis[a[u]])
    return a[u];
  vis[a[u]] = vis[u] + 1;
  pos[vis[a[u]]] = a[u];
  dfs(a[u]);
}
int main() {
#ifdef Irene
  freopen("in.txt", "r", stdin);
#endif // Irene
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i], vis[i] = -1;
  vis[1] = 0, pos[0] = 1;
  int x = dfs(1);
  int mx = *max_element(vis + 1, vis + n + 1) + 1;
  int ans;
  if (k < mx)
    ans = pos[k];
  else {
    int circle = mx - vis[x];
    k -= vis[x];
    k %= circle;
    ans = pos[vis[x] + k];
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
#define DEBUG(x) cout << x << '\n'

int n, a[MAXN], vis[MAXN], pos[MAXN];
ll k;
int dfs(int u) {
  if (~vis[a[u]])
    return a[u];
  vis[a[u]] = vis[u] + 1;
  pos[vis[a[u]]] = a[u];
  return dfs(a[u]);
}
int main() {
#ifdef Irene
  freopen("in.txt", "r", stdin);
#endif // Irene
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i], vis[i] = -1;
  vis[1] = 0, pos[0] = 1;
  int x = dfs(1);
  int mx = *max_element(vis + 1, vis + n + 1) + 1;
  int ans;
  if (k < mx)
    ans = pos[k];
  else {
    int circle = mx - vis[x];
    k -= vis[x];
    k %= circle;
    ans = pos[vis[x] + k];
  }
  cout << ans;
}
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 INF 1001001001
using ll = long long;
using dd = long double;

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  rep(i, n) {
    cin >> to[0][i];
    to[0][i]--;
  }

  rep(i, D - 1) rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; }

  int v = 0;
  for (int i = D - 1; i >= 0; --i) {
    ll l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }
  cout << v + 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 INF 1001001001
using ll = long long;
using dd = long double;

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  rep(i, n) {
    cin >> to[0][i];
    to[0][i]--;
  }

  rep(i, D - 1) rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; }

  int v = 0;
  for (int i = D - 1; i >= 0; --i) {
    ll l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }
  cout << v + 1 << endl;

  return 0;
}
Problem p02684 (C++)

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> array(N + 1);
  array[0] = 0;
  vector<int> reach(N + 1);
  vector<long> loop(0);
  for (int i = 0; i < N; i++)
    cin >> array[i + 1];
  long place = 1;
  long count = 0;
  long mark = 0;
  long j = 0;
  while (reach[place] < 3) {
    reach[place]++;
    if (reach[place] == 2 && count == 0)
      mark = j - 1;
    if (reach[place] == 2) {
      loop.push_back(place);
      count++;
    }

    place = array[place];
    j++;
  }
  long ans = (K - mark) % count - 1;
  if (ans == -1)
    ans = count - 1;
  cout << loop[ans] << endl;
}

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> array(N + 1);
  array[0] = 0;
  vector<int> reach(N + 1);
  vector<long> loop(0);
  for (int i = 0; i < N; i++)
    cin >> array[i + 1];
  long place = 1;
  long count = 0;
  long mark = 0;
  long j = 0;
  while (reach[place] < 3) {
    reach[place]++;
    if (reach[place] == 2 && count == 0)
      mark = j - 1;
    if (reach[place] == 2) {
      loop.push_back(place);
      count++;
    }

    place = array[place];
    j++;
    if (j == K) {
      cout << place << endl;
      return 0;
    }
  }
  long ans = (K - mark) % count - 1;
  if (ans == -1)
    ans = count - 1;
  cout << loop[ans] << endl;
}
Problem p02684 (C++)

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

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

  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i]--;
  }
  long now = 0;
  vector<int> v(N);
  while (K > 0) {
    now = a[now];
    K--;
    //  cout<<now+1<<endl;
    if (v[now] == 1) {
      v[now]++;
      break;
    } else
      v[now]++;
  }
  // cout<<"K"<<K<<endl;
  long loop = 0;
  while (K > 0) {
    now = a[now];
    K--;
    loop++;
    //  cout<<now+1<<endl;
    if (v[now] == 2) {
      v[now]++;
      break;
    } else
      v[now]++;
  }
  // cout<<"K"<<K<<endl;
  // cout<<loop<<endl;
  K %= loop;
  while (K > 0) {
    now = a[now];
    K--;
    //  cout<<now+1<<endl;
  }
  cout << now + 1 << endl;
}


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

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

  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i]--;
  }
  long now = 0;
  vector<int> v(N);
  while (K > 0) {
    now = a[now];
    K--;
    //  cout<<now+1<<endl;
    if (v[now] == 1) {
      v[now]++;
      break;
    } else
      v[now]++;
  }
  // cout<<"K"<<K<<endl;
  long loop = 0;
  while (K > 0) {
    now = a[now];
    K--;
    loop++;
    //  cout<<now+1<<endl;
    if (v[now] == 2) {
      v[now]++;
      break;
    } else
      v[now]++;
  }
  // cout<<"K"<<K<<endl;
  // cout<<loop<<endl;
  if (loop > 0)
    K %= loop;
  while (K > 0) {
    now = a[now];
    K--;
    //  cout<<now+1<<endl;
  }
  cout << now + 1 << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
int nxt[62][200010] = {};
void solve() {
  ll N, K;
  cin >> N >> K;
  ll A[200010];
  rep(i, 0, N) {
    cin >> A[i];
    A[i]--;
  }
  rep(i, 0, N) { nxt[0][i] = A[i]; }

  rep(k, 0, 62) {
    rep(i, 0, N) {
      if (nxt[k][i] == -1) {
        nxt[k + 1][i] = -1;
      } else {
        nxt[k + 1][i] = nxt[k][nxt[k][i]];
      }
    }
  }
  ll pos = 0;

  rrep(b, 62, 0) {
    if ((K >> b) & 1) {
      pos = nxt[b][pos];
    }
  }
  print(pos + 1);
}

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

Fixed submission:
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
int nxt[65][200010] = {};
void solve() {
  ll N, K;
  cin >> N >> K;
  ll A[200010];
  rep(i, 0, N) {
    cin >> A[i];
    A[i]--;
  }
  rep(i, 0, N) { nxt[0][i] = A[i]; }

  rep(k, 0, 62) {
    rep(i, 0, N) {
      if (nxt[k][i] == -1) {
        nxt[k + 1][i] = -1;
      } else {
        nxt[k + 1][i] = nxt[k][nxt[k][i]];
      }
    }
  }
  ll pos = 0;

  rrep(b, 62, 0) {
    if ((K >> b) & 1) {
      pos = nxt[b][pos];
    }
  }
  print(pos + 1);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
/*
AtCoderBeginnerContest167
Problem D - Teleporter /
Time Limit: 2 sec / Memory Limit: 1024 MB
Score:
400
points
*/

#include <iostream>
#include <vector>

int main() {
  int N;
  long K;
  std::vector<int> A;
  A.push_back(0);
  std::cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int currA;
    std::cin >> currA;
    A.push_back(currA);
  }
  std::vector<int> vis(N + 1, -1);
  std::vector<int> loc;
  int T = 0;
  int pos = 1;
  while (vis[pos] == -1) {
    vis[pos] = T;
    loc.push_back(pos);
    T++;
    pos = A[pos];
  }
  int prevT = vis[pos];
  if (K >= prevT) {
    int slack = K - prevT;
    int period = T - prevT;
    slack %= period;
    K = prevT + slack;
  }
  std::cout << loc[K] << "\n";
}


Fixed submission:
/*
AtCoderBeginnerContest167
Problem D - Teleporter /
Time Limit: 2 sec / Memory Limit: 1024 MB
Score:
400
points
*/

#include <iostream>
#include <vector>

int main() {
  int N;
  long K;
  std::vector<int> A;
  A.push_back(0);
  std::cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int currA;
    std::cin >> currA;
    A.push_back(currA);
  }
  std::vector<int> vis(N + 1, -1);
  std::vector<int> loc;
  int T = 0;
  int pos = 1;
  while (vis[pos] == -1) {
    vis[pos] = T;
    loc.push_back(pos);
    T++;
    pos = A[pos];
  }
  int prevT = vis[pos];
  if (K >= prevT) {
    long slack = K - prevT;
    int period = T - prevT;
    slack %= period;
    K = prevT + slack;
  }
  std::cout << loc[K] << "\n";
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  int N, i, j, x, y, MOD, A[200000] = {0}, check[200000] = {},
                          move[200000] = {0};
  long long K;
  x = scanf("%d %lld", &N, &K);
  for (i = 0; i < N; i++)
    x = scanf("%d", &A[i]), check[i] = -1;
  for (i = 0, j = 0;; i = A[i] - 1, j++)
    if (check[i] < 0)
      check[i] = j, move[j] = i;
    else {
      x = j - 1, y = check[i], MOD = x - y - 1;
      break;
    }
  if (K > x)
    K = (K - y) % MOD + y;
  return printf("%d\n", move[K] + 1), 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int N, i, j, x, y, MOD, A[200000] = {0}, check[200000] = {},
                          move[200000] = {0};
  long long K;
  x = scanf("%d %lld", &N, &K);
  for (i = 0; i < N; i++)
    x = scanf("%d", &A[i]), check[i] = -1;
  for (i = 0, j = 0;; i = A[i] - 1, j++)
    if (check[i] < 0)
      check[i] = j, move[j] = i;
    else {
      x = j - 1, y = check[i], MOD = x - y + 1;
      break;
    }
  if (K > x)
    K = (K - y) % MOD + y;
  return printf("%d\n", move[K] + 1), 0;
}
Problem p02684 (C++)

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

using namespace std;

using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1e18;

int main() {
  ll n, m, i, j, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (auto &x : A) {
    cin >> x;
  }
  ll cnt = 0, now = 0;
  vector<ll> used(n, -1);
  used[0] = 0;
  for (i = 0; i < n; i++) {
    now = A[now] - 1;
    cnt++;
    if (used[now] >= 0)
      break;
    used[now] = i + 1;
  }
  // now が 始点
  ll ch = 0, s = 0;
  if (now != 0) {
    for (i = 0; i < n; i++) {
      ch = A[ch] - 1;
      s++;
      if (ch == now)
        break;
    }
  }
  vector<ll> loop;
  ch = now;
  do {
    loop.push_back(ch);
    ch = A[ch] - 1;
  } while (ch != now);
  ll c = (k - s) % (cnt - s);
  cout << loop[c] + 1 << endl;
  return 0;
}

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

using namespace std;

using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1e18;

int main() {
  ll n, m, i, j, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (auto &x : A) {
    cin >> x;
  }
  ll cnt = 0, now = 0;
  vector<ll> used(n, -1);
  used[0] = 0;
  for (i = 0; i < n; i++) {
    now = A[now] - 1;
    cnt++;
    if (used[now] >= 0)
      break;
    used[now] = i + 1;
    if (cnt == k) {
      cout << now + 1 << endl;
      return 0;
    }
  }
  // now が 始点
  ll ch = 0, s = 0;
  if (now != 0) {
    for (i = 0; i < n; i++) {
      ch = A[ch] - 1;
      s++;
      if (ch == now)
        break;
    }
  }
  vector<ll> loop;
  ch = now;
  do {
    loop.push_back(ch);
    ch = A[ch] - 1;
  } while (ch != now);
  ll c = (k - s) % (cnt - s);
  cout << loop[c] + 1 << endl;
  return 0;
}