prithivMLmods/Coder-Stat huggingface dataset:
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<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    A.at(i)--;
  }
  vector<int> B;
  int index = 0;
  int count = 0;
  set<int> gone;
  gone.insert(0);
  long long right = 0, left = 0;
  while (count < N) {
    B.push_back(A.at(index));
    // cout << "B = " << B.at(count) << endl;
    if (gone.count(A.at(index))) {
      right = A.at(index);
      left = count;
      break;
    }
    gone.insert(A.at(index));
    index = A.at(index);
    count++;
  }
  if (right == 0)
    right = -1;
  // cout << "count = " << count << endl;
  // cout << "right = " << right << endl;
  for (int i = 0; i < count; i++) {
    if (B.at(i) == right) {
      right = i;
      break;
    }
  }
  int ans = 0;
  if (left + 1 >= K)
    ans = B.at(K - 1);
  else {
    // cout << "left = " << left << ", right = " << right << endl;
    long long loop = left - right;
    // cout << "loop = " << loop << endl;
    long long pos;
    if (loop)
      pos = right + (K - right - 1) % loop;
    else
      pos = right;
    // cout << "pos = " << pos << endl;
    ans = B.at(pos);
  }
  cout << ans + 1 << endl;
}


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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    A.at(i)--;
  }
  vector<int> B;
  int index = 0;
  int count = 0;
  set<int> gone;
  gone.insert(0);
  long long right = 0, left = 0;
  while (count < N) {
    B.push_back(A.at(index));
    // cout << "B = " << B.at(count) << endl;
    if (gone.count(A.at(index))) {
      right = A.at(index);
      left = count;
      break;
    }
    gone.insert(A.at(index));
    index = A.at(index);
    count++;
  }
  if (right == 0)
    right = -1;
  // cout << "count = " << count << endl;
  // cout << "right = " << right << endl;
  for (int i = 0; i < count; i++) {
    if (B.at(i) == right) {
      right = i;
      break;
    }
  }
  int ans = 0;
  if (left + 1 >= K)
    ans = B.at(K - 1);
  else {
    // cout << "left = " << left << ", right = " << right << endl;
    long long loop = left - right;
    // cout << "loop = " << loop << endl;
    long long pos;
    if (loop)
      pos = right + (K - right - 1) % loop;
    else
      pos = right;
    // cout << "pos = " << pos << endl;
    if (pos != -1)
      ans = B.at(pos);
    else
      ans = 0;
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++)

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

using ll = long long;
using namespace std;

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

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

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

using ll = long long;
using namespace std;

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

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c = 1, l = 0;
  int v = 1;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.push_back(v);
    v = as[v - 1];
  }
  c = s.size() - ord[v];
  l = ord[v];
  if (k < l) {
    cout << s[k] << endl;
  } else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

/* exchange */
#define chmin(a, b) (a) = min((ll)(a), (ll)(b))
#define chmax(a, b) (a) = max((ll)(a), (ll)(b))

/* output */
#define I(x) cin >> x;
#define D(x) cerr << (x) << " ";
#define BR cerr << "\n";
#define P(x) cout << (x) << endl;
#define FIX cout << fixed << setprecision(10);

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll K;
ll ret = 0;
vvl table(61, vl(ARRAY));

int main(void) {

  I(N);
  I(K);
  FOR(i, 1, N + 1) { I(table[1][i]); }
  FOR(i, 2, 61) {
    FOR(j, 1, N + 1) { table[i][j] = table[i - 1][table[i - 1][j]]; }
  }

  ll now = 1;
  FOR(i, 1, 61) {
    if ((K >> (i - 1)) & 1) {
      now = table[i][now];
    }
  }

  P(now);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

/* exchange */
#define chmin(a, b) (a) = min((ll)(a), (ll)(b))
#define chmax(a, b) (a) = max((ll)(a), (ll)(b))

/* output */
#define I(x) cin >> x;
#define D(x) cerr << (x) << " ";
#define BR cerr << "\n";
#define P(x) cout << (x) << endl;
#define FIX cout << fixed << setprecision(10);

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll K;
ll ret = 0;
vvl table(61, vl(200005));

int main(void) {

  I(N);
  I(K);
  FOR(i, 1, N + 1) { I(table[1][i]); }
  FOR(i, 2, 61) {
    FOR(j, 1, N + 1) { table[i][j] = table[i - 1][table[i - 1][j]]; }
  }

  ll now = 1;
  FOR(i, 1, 61) {
    if ((K >> (i - 1)) & 1) {
      now = table[i][now];
    }
  }

  P(now);
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#pragma warning(disable : 4996)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793

using namespace std;
using ll = long long;

const int D = 60;
const int MAX_N = 200005;

int to[D][MAX_N];

int main() {

  ll n, k;
  cin >> n >> k;

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

  rep(i, n) {
    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 <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#pragma warning(disable : 4996)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793

using namespace std;
using ll = long long;

const int D = 60;
const int MAX_N = 200005;

int to[D][MAX_N];

int main() {

  ll n, 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>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using lint = long long;

int main() {
  int n;
  lint k;
  scanf("%d%lld", &n, &k);
  vector<int> to(n);
  rep(u, n) scanf("%d", &to[u]), to[u]--;

  static int dbl[60][100000];
  rep(u, n) dbl[0][u] = to[u];
  rep(t, 59) rep(u, n) dbl[t + 1][u] = dbl[t][dbl[t][u]];

  int pos = 0;
  rep(t, 60) if (k >> t & 1) pos = dbl[t][pos];
  printf("%d\n", pos + 1);

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using lint = long long;

int main() {
  int n;
  lint k;
  scanf("%d%lld", &n, &k);
  vector<int> to(n);
  rep(u, n) scanf("%d", &to[u]), to[u]--;

  static int dbl[60][200000];
  rep(u, n) dbl[0][u] = to[u];
  rep(t, 59) rep(u, n) dbl[t + 1][u] = dbl[t][dbl[t][u]];

  int pos = 0;
  rep(t, 60) if (k >> t & 1) pos = dbl[t][pos];
  printf("%d\n", pos + 1);

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <array>
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(void) {
  int n;
  long int k;
  std::array<int, 200000> next_town;
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int next;
    cin >> next;
    next_town[i] = next - 1;
  }

  int current_index = 0;

  std::map<int, int> arrive_time;
  bool dumped = false;
  for (long int i = 0; i < k; i++) {
    arrive_time[current_index] = i;
    current_index = next_town[current_index];

    if (!dumped && arrive_time.find(current_index) != arrive_time.end()) {
      int roop_start = arrive_time[current_index];
      int roop_length = i - roop_start;
      i = k - (k - 1 - roop_start) % roop_length - 1;
      dumped = true;
    }
  }
  cout << current_index + 1;
}

Fixed submission:
#include <array>
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(void) {
  int n;
  long int k;
  std::array<int, 200000> next_town;
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int next;
    cin >> next;
    next_town[i] = next - 1;
  }

  int current_index = 0;

  std::map<int, int> arrive_time;
  bool dumped = false;
  for (long int i = 0; i < k; i++) {
    arrive_time[current_index] = i;
    current_index = next_town[current_index];

    if (!dumped && arrive_time.find(current_index) != arrive_time.end()) {
      int roop_start = arrive_time[current_index];
      int roop_length = i - roop_start + 1;
      i = k - (k - roop_start) % roop_length - 1;
      dumped = true;
    }
  }
  cout << current_index + 1;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
/*
        Date:2020.5.10
        Name:Aliemo
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

#define int long long
#define rr register

#define MAXN 1010
#define inf 1e18

using namespace std;

inline int read(int &T) {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + (ch ^ 48);
    ch = getchar();
  }
  return T = f ? -s : s;
}

void print(int x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

int n, k, now, last, sum, ans;

int a[MAXN], vis[MAXN];

bool f;

signed main() {
  n = read(n);
  k = read(k);
  for (rr int i = 1; i <= n; i++)
    a[i] = read(a[i]);
  now = 1;
  vis[now]++;
  while (k--) {
    now = a[now];
    // cout << now;
    if (vis[now] == 1 && !f) {
      // ans++;
      vis[now]++;
      f = 1;
      int t = now;
      t = a[t];
      ans++;
      while (vis[t] != 2) {
        t = a[t];
        ans++;
      }
      // cout << ans << " ";
      k %= ans;
    }
    vis[now]++;
  }
  cout << now;
}


Fixed submission:
/*
        Date:2020.5.10
        Name:Aliemo
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

#define int long long
#define rr register

#define MAXN 200010
#define inf 1e18

using namespace std;

inline int read(int &T) {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + (ch ^ 48);
    ch = getchar();
  }
  return T = f ? -s : s;
}

void print(int x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

int n, k, now, last, sum, ans;

int a[MAXN], vis[MAXN];

bool f;

signed main() {
  n = read(n);
  k = read(k);
  for (rr int i = 1; i <= n; i++)
    a[i] = read(a[i]);
  now = 1;
  vis[now]++;
  while (k--) {
    now = a[now];
    // cout << now;
    if (vis[now] == 1 && !f) {
      // ans++;
      vis[now]++;
      f = 1;
      int t = now;
      t = a[t];
      ans++;
      while (vis[t] != 2) {
        t = a[t];
        ans++;
      }
      // cout << ans << " ";
      k %= ans;
    }
    vis[now]++;
  }
  cout << now;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  long long k;
  int x;
  scanf("%d %lld", &n, &k);

  vector<int> next(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x);
    next[i] = x - 1;
  }

  vector<bool> visited(n);
  vector<int> chain;

  visited[0] = true;
  chain.push_back(0);

  while (k > 0) {
    int nn = next[chain[chain.size() - 1]];
    --k;

    chain.push_back(nn);

    if (visited[nn]) {
      break;
    }
    visited[nn] = true;
  }

  if (k == 0) {
    printf("%d\n", chain[chain.size() - 1] + 1);
  }

  // Find cycle

  int index = 0;
  while (chain[index++] != chain[chain.size() - 1])
    ;

  vector<int> whole_cycle(chain.begin() + index, chain.end());

  --k;

  k %= whole_cycle.size();

  printf("%d", whole_cycle[k] + 1);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  long long k;
  int x;
  scanf("%d %lld", &n, &k);

  vector<int> next(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x);
    next[i] = x - 1;
  }

  vector<bool> visited(n);
  vector<int> chain;

  visited[0] = true;
  chain.push_back(0);

  while (k > 0) {
    int nn = next[chain[chain.size() - 1]];
    --k;

    chain.push_back(nn);

    if (visited[nn]) {
      break;
    }
    visited[nn] = true;
  }

  if (k == 0) {
    printf("%d\n", chain[chain.size() - 1] + 1);
    return 0;
  }

  // Find cycle

  int index = 0;
  while (chain[index++] != chain[chain.size() - 1])
    ;

  vector<int> whole_cycle(chain.begin() + index, chain.end());

  --k;

  k %= whole_cycle.size();

  printf("%d", whole_cycle[k] + 1);

  return 0;
}
Problem p02684 (C++)

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

using namespace std;

using ll = long long;

#define MOD_NUMBER 1000000007

// ベクトル要素の列挙
template <class T> void vector_print(vector<T> vec) {
  for (unsigned int i = 0; i < vec.size(); i++) {
    cout << vec.at(i);
    if (i < vec.size() - 1) {
      cout << " ";
    } else if (i == vec.size() - 1) {
      cout << endl;
    }
  }
}

// エラトステネスの篩による素数配列の出力
vector<int> Eratosthenes(int N) {
  vector<int> p = {2};
  int sqrtN = sqrt(N);
  for (int i = 3; i <= N; i++) {
    p.push_back(i);
    for (int j = 0; p.at(j) <= sqrtN; j++) {
      if (i % p.at(j) == 0) {
        p.pop_back();
        break;
      }
    }
  }
  return p;
}

int main() {
  ll n, k, x = 1, size = 0, spare;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> b(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < n; i++) {
    if (a.at(x - 1) <= 0) {
      break;
    }
    spare = x;
    x = a.at(x - 1);
    a.at(spare - 1) = -size;
    b.at(i) = x;
    size++;
  }
  if (k <= size) {
    cout << b.at(k - 1) << endl;
    return 0;
  }
  k += a.at(x - 1);
  k %= size - a.at(x - 1);
  k -= a.at(x - 1);
  if (k == 0) {
    k = size;
  }
  cout << b.at(k - 1) << endl;
  return 0;
}


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

using namespace std;

using ll = long long;

#define MOD_NUMBER 1000000007

// ベクトル要素の列挙
template <class T> void vector_print(vector<T> vec) {
  for (unsigned int i = 0; i < vec.size(); i++) {
    cout << vec.at(i);
    if (i < vec.size() - 1) {
      cout << " ";
    } else if (i == vec.size() - 1) {
      cout << endl;
    }
  }
}

// エラトステネスの篩による素数配列の出力
vector<int> Eratosthenes(int N) {
  vector<int> p = {2};
  int sqrtN = sqrt(N);
  for (int i = 3; i <= N; i++) {
    p.push_back(i);
    for (int j = 0; p.at(j) <= sqrtN; j++) {
      if (i % p.at(j) == 0) {
        p.pop_back();
        break;
      }
    }
  }
  return p;
}

int main() {
  ll n, k, x = 1, size = 0, spare;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> b(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < n; i++) {
    if (a.at(x - 1) <= 0) {
      break;
    }
    spare = x;
    x = a.at(x - 1);
    a.at(spare - 1) = -size;
    b.at(i) = x;
    size++;
  }
  if (k <= size) {
    cout << b.at(k - 1) << endl;
    return 0;
  }
  k += a.at(x - 1);
  k %= size + a.at(x - 1);
  k -= a.at(x - 1);
  if (k == 0) {
    k = size;
  }
  cout << b.at(k - 1) << endl;
  return 0;
}
Problem p02684 (C++)

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

using namespace std;
const int N = 2e5 + 1;
long long k;
int n, a[N];
bool avail[N];
vector<int> V;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  V.push_back(0);
  int start = 1;
  bool run = false;
  while (avail[a[start]] == false) {
    start = a[start];
    avail[start] = true;
    V.push_back(start);
  }
  // V.push_back(a[start]);
  // for (int i = 1; i < V.size(); i++) cout << V[i] << " "; cout << "\n";
  int d, c = V.size() - 1;
  for (int i = 1; i < V.size() - 1; i++)
    if (a[start] == V[i])
      d = i;
  // cout << d << " " << c << "\n";
  if (k < d)
    cout << V[k];
  else {
    k = k - d + 1;
    k = k % (c - d + 1);
    if (k == 0)
      cout << V[c];
    else
      cout << V[d + k - 1];
  }
  return 0;
}


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

using namespace std;
const int N = 2e5 + 1;
long long k;
int n, a[N];
bool avail[N];
vector<int> V;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  V.push_back(0);
  int start = 1;
  bool run = false;
  while (avail[a[start]] == false) {
    start = a[start];
    avail[start] = true;
    V.push_back(start);
  }
  // V.push_back(a[start]);
  // for (int i = 1; i < V.size(); i++) cout << V[i] << " "; cout << "\n";
  int d, c = V.size() - 1;
  for (int i = 1; i < V.size(); i++)
    if (a[start] == V[i])
      d = i;
  // cout << d << " " << c << "\n";
  if (k < d)
    cout << V[k];
  else {
    k = k - d + 1;
    k = k % (c - d + 1);
    if (k == 0)
      cout << V[c];
    else
      cout << V[d + k - 1];
  }
  return 0;
}
Problem p02684 (C++)

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

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

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

  map<ll, ll> mp;
  ll count = 0;

  ll curr = 0;
  mp[curr] = 0;

  while (true) {
    curr = a[curr];
    count++;
    if (count == k) {
      return curr + 1;
    }
    if (mp.count(curr)) {
      break;
    }
    mp[curr] = count;
  }

  ll loop_begin = curr;
  ll loop = count - mp[loop_begin];

  k = k - mp[loop_begin];
  k = k % loop;

  curr = loop_begin;
  for (int i = 0; i < k; i++) {
    curr = a[curr];
  }

  cout << curr + 1 << endl;
}


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

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

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

  map<ll, ll> mp;
  ll count = 0;

  ll curr = 0;
  mp[curr] = 0;

  while (true) {
    curr = a[curr];
    count++;
    if (count == k) {
      cout << curr + 1 << endl;
      return 0;
    }
    if (mp.count(curr)) {
      break;
    }
    mp[curr] = count;
  }

  ll loop_begin = curr;
  ll loop = count - mp[loop_begin];

  k = k - mp[loop_begin];
  k = k % loop;

  curr = loop_begin;
  for (int i = 0; i < k; i++) {
    curr = a[curr];
  }

  cout << curr + 1 << endl;
}
Problem p02684 (C++)

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

using namespace std;

#define ll long int
#define ull unsigned long long int

#define I_read() freopen("input.tex", "r", stdin);

#define O_write() freopen("output.txt", "w", stdout);

int main() {

  //	ios_base::sync_with_stdio(false);
  //	cin.tie(0);

  //	I_read();
  //	O_write();

  ll a;
  ull b, temp, rem, numt = 0;

  cin >> a >> b;

  ll town[a], trv[a], ptow = 0;
  bool pass[a];

  for (ll i = 0; i < a; i++) {
    cin >> town[i];
    pass[i] = 0;
    //	cout<<pass[i]<<" ";
  }
  cout << endl;

  while (pass[ptow] != 1 && numt < b) {
    trv[ptow] = numt;
    pass[ptow] = 1;
    //	cout<<ptow+1<<"->";
    ptow = town[ptow] - 1;
    numt++;
  }

  //	cout<<"numt="<<numt<<"pass[ptow]="<<pass[ptow]<<endl;

  if (numt != b) {
    //	cout<<"not-compleated-";
    temp = numt - trv[ptow];

    rem = (b - numt) % temp;

    //	cout<<"temp="<<temp<<"rem="<<rem<<"->";
  }

  while (rem > 0) {
    // trv[ptow] =numt;
    // pass[ptow] = 1;
    ptow = town[ptow] - 1;
    rem--;
  }
  cout << ptow + 1 << endl;
  return 0;
}


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

using namespace std;

#define ll long int
#define ull unsigned long long int

#define I_read() freopen("input.tex", "r", stdin);

#define O_write() freopen("output.txt", "w", stdout);

int main() {

  //	ios_base::sync_with_stdio(false);
  //	cin.tie(0);

  //	I_read();
  //	O_write();

  ll a;
  ull b, temp, rem = 0, numt = 0;

  cin >> a >> b;

  ll town[a], trv[a], ptow = 0;
  bool pass[a];

  for (ll i = 0; i < a; i++) {
    cin >> town[i];
    pass[i] = 0;
    //	cout<<pass[i]<<" ";
  }
  cout << endl;

  while (pass[ptow] != 1 && numt < b) {
    trv[ptow] = numt;
    pass[ptow] = 1;
    //	cout<<ptow+1<<"->";
    ptow = town[ptow] - 1;
    numt++;
  }

  //	cout<<"numt="<<numt<<"pass[ptow]="<<pass[ptow]<<endl;

  if (numt != b) {
    //	cout<<"not-compleated-";
    temp = numt - trv[ptow];

    rem = (b - numt) % temp;

    //	cout<<"temp="<<temp<<"rem="<<rem<<"->";
  }

  while (rem > 0) {
    // trv[ptow] =numt;
    // pass[ptow] = 1;
    ptow = town[ptow] - 1;
    rem--;
  }
  cout << ptow + 1 << endl;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
static const int MAX = 200005;

int N;
ll K;
int A[MAX], B[MAX], C[MAX];

int main() {
  cin >> N >> K;
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> V;

  int x = A[0];
  B[0] = 1;
  C[0] = 0;
  for (int i = 1; B[x] == 0; i++) {
    B[x] = 1;
    C[x] = i;
    V.push_back(x);
    x = A[x];
  }
  int n = V.size() + 1;
  int m = n - C[x];
  ll a;
  if (n > K)
    a = K;
  else {
    a = ((K - C[x]) % m) + C[x];
  }
  if (a == -1)
    cout << 1 << endl;
  else
    cout << V[a - 1] + 1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
static const int MAX = 200005;

int N;
ll K;
int A[MAX], B[MAX], C[MAX];

int main() {
  cin >> N >> K;
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> V;

  int x = A[0];
  B[0] = 1;
  C[0] = 0;
  for (int i = 1; B[x] == 0; i++) {
    B[x] = 1;
    C[x] = i;
    V.push_back(x);
    x = A[x];
  }
  int n = V.size() + 1;
  int m = n - C[x];
  ll a;
  if (n > K)
    a = K;
  else {
    a = ((K - C[x]) % m) + C[x];
  }
  if (a == 0)
    cout << 1 << endl;
  else
    cout << V[a - 1] + 1 << endl;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int a[200005];
vector<int> path;
long long ind[200005] = {};

bool check_cycle(int cur_index) {
  if (ind[cur_index])
    return true;
  return false;
}

int main() {
  long long n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  long long temp_k = 0, cur_index = 1;
  while (!ind[cur_index] && temp_k < k) {
    ind[cur_index] = temp_k++;
    path.push_back(cur_index);
    cur_index = a[cur_index];
  }
  if (temp_k == k) {
    cout << cur_index;
  } else {
    cout
        << path[((k - ind[cur_index]) % (path.size() - cur_index)) + cur_index];
  }

  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int a[200005];
vector<int> path;
long long ind[200005] = {};

bool check_cycle(int cur_index) {
  if (ind[cur_index])
    return true;
  return false;
}

int main() {
  long long n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  long long temp_k = 0, cur_index = 1;
  while (!ind[cur_index] && temp_k < k) {
    ind[cur_index] = temp_k++;
    path.push_back(cur_index);
    cur_index = a[cur_index];
  }
  if (temp_k == k) {
    cout << cur_index;
  } else {
    cout << path[((k - ind[cur_index]) % (path.size() - ind[cur_index])) +
                 ind[cur_index]];
  }

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int next[70][200008];
int main() {
  int N;
  ll K;
  scanf("%d%lld", &N, &K);
  rep(i, N) {
    scanf("%d", &next[0][i]);
    next[0][i]--;
  }
  rep(j, 70) {
    rep(i, N) { next[j + 1][i] = next[j][next[j][i]]; }
  }
  int cur = 0;
  rep(j, 70) {
    if (K % 2)
      cur = next[j][cur];
    K /= 2;
  }
  printf("%d\n", cur + 1);
  return 0;
}


Fixed submission:
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int next[70][200008];
int main() {
  int N;
  ll K;
  scanf("%d%lld", &N, &K);
  rep(i, N) {
    scanf("%d", &next[0][i]);
    next[0][i]--;
  }
  rep(j, 69) {
    rep(i, N) { next[j + 1][i] = next[j][next[j][i]]; }
  }
  int cur = 0;
  rep(j, 70) {
    if (K % 2)
      cur = next[j][cur];
    K /= 2;
  }
  printf("%d\n", cur + 1);
  return 0;
}
Problem p02684 (C++)

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

using namespace std;
typedef long long LL;
int T[200001];
bool vis[200001];
int ans, mulai, selesai;
vector<int> d;
LL K;

void dfs(int nod) {
  vis[nod] = 1;
  d.pb(nod);
  if (K == 0) {
    ans = nod;
    return;
  }
  if (!vis[T[nod]])
    K--, dfs(T[nod]);
  else {
    mulai = T[nod];
    selesai = nod;
  }
}

int main() {
  int N;
  scanf("%d%lld", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &T[i]);
  }
  dfs(1);
  if (K > 0) {
    int cycle;
    K--;
    int l1 = d.size();
    for (int i = 0; i < l1; i++) {
      if (d[i] == mulai) {
        for (int j = i + 1; j < l1; j++) {
          if (d[j] == selesai) {
            cycle = j - i + 1;
            mulai = i;
            selesai = j;
            i = l1;
            break;
          }
        }
      }
    }
    K %= cycle;
    ans = d[mulai + K];
  }
  printf("%d\n", ans);
}

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

using namespace std;
typedef long long LL;
int T[200001];
bool vis[200001];
int ans, mulai, selesai;
vector<int> d;
LL K;

void dfs(int nod) {
  vis[nod] = 1;
  d.pb(nod);
  if (K == 0) {
    ans = nod;
    return;
  }
  if (!vis[T[nod]])
    K--, dfs(T[nod]);
  else {
    mulai = T[nod];
    selesai = nod;
  }
}

int main() {
  int N;
  scanf("%d%lld", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &T[i]);
  }
  dfs(1);
  if (K > 0) {
    int cycle;
    K--;
    int l1 = d.size();
    for (int i = 0; i < l1; i++) {
      if (d[i] == mulai) {
        for (int j = i; j < l1; j++) {
          if (d[j] == selesai) {
            cycle = j - i + 1;
            mulai = i;
            selesai = j;
            i = l1;
            break;
          }
        }
      }
    }
    K %= cycle;
    ans = d[mulai + K];
  }
  printf("%d\n", ans);
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<ll> Vec;
typedef vector<pair<ll, ll>> Pair;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

// static const ll MOD = 1000000007;
// static const ll INF = 100000000;
// #define PI 3.14159265359

int main() {
  // input
  ll N, K;
  cin >> N >> K;
  Vec A(N);
  rep(i, N) {
    cin >> A[i];
    --A[i];
  }

  // process
  Vec is_visited(N, 0);
  ll now = 0, loop = 0, remaining_K = 0;
  rep(i, K) {
    if (is_visited[now] != 0) {
      loop = i - is_visited[now];
      remaining_K = K - i;
      break;
    }
    is_visited[now] = i;
    now = A[now];
  }

  remaining_K %= loop;
  rep(i, remaining_K) { now = A[now]; }

  cout << now + 1 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<ll> Vec;
typedef vector<pair<ll, ll>> Pair;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

// static const ll MOD = 1000000007;
// static const ll INF = 100000000;
// #define PI 3.14159265359

int main() {
  // input
  ll N, K;
  cin >> N >> K;
  Vec A(N);
  rep(i, N) {
    cin >> A[i];
    --A[i];
  }

  // process
  Vec is_visited(N, 0);
  ll now = 0, loop = 0, remaining_K = 0;
  rep(i, K) {
    if (is_visited[now] != 0) {
      loop = i - is_visited[now];
      remaining_K = K - i;
      break;
    }
    is_visited[now] = i;
    now = A[now];
  }

  if (remaining_K != 0) {
    remaining_K %= loop;
    rep(i, remaining_K) { now = A[now]; }
  }

  cout << now + 1 << 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++)
using namespace std;

signed main(void) {
  int n;
  long long k, cnt, cnt_loop;
  cin >> n >> k;
  int a[n], label[n];
  int pre, tmp;
  vector<bool> visit(n, false);
  vector<int> ans;

  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  queue<int> que;
  que.push(a[0]);
  pre = -1;
  cnt = 1;
  label[0] = cnt;
  visit[0] = true;
  while (true) {
    int now = que.front();
    que.pop();
    if (visit[now]) {
      cnt_loop = label[pre] - label[now] + 1;
      tmp = now;
      break;
    }
    cnt++;
    label[now] = cnt;
    visit[now] = true;
    que.push(a[now]);
    pre = now;
  }
  ans.push_back(tmp + 1);
  rep(i, cnt_loop - 1) {
    int now = a[tmp];
    tmp = now;
    ans.push_back(tmp + 1);
  }
  k -= (cnt - cnt_loop);
  k %= cnt_loop;
  cout << ans[k] << endl;
  return 0;
}


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

signed main(void) {
  int n;
  long long k, cnt, cnt_loop;
  cin >> n >> k;
  int a[n], label[n];
  int pre, tmp;
  vector<bool> visit(n, false);
  vector<int> ans;

  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  queue<int> que;
  que.push(a[0]);
  pre = -1;
  cnt = 1;
  label[0] = cnt;
  visit[0] = true;
  while (true) {
    int now = que.front();
    que.pop();
    if (visit[now]) {
      cnt_loop = label[pre] - label[now] + 1;
      tmp = now;
      break;
    }
    if (cnt >= k) {
      cout << now + 1 << endl;
      return 0;
    }
    cnt++;
    label[now] = cnt;
    visit[now] = true;
    que.push(a[now]);
    pre = now;
  }
  ans.push_back(tmp + 1);
  rep(i, cnt_loop - 1) {
    int now = a[tmp];
    tmp = now;
    ans.push_back(tmp + 1);
  }
  k -= (cnt - cnt_loop);
  k %= cnt_loop;
  cout << ans[k] << endl;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pai;
typedef pair<ll, ll> pal;
const int MOD = 1e9 + 7;
const int MODD = 998244353;
const ll LLINF = 1e18;

ll dub[60][100001];

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

  int log_k = log2(k);

  for (int i = 0; i < n; i++) {
    dub[0][i] = a[i];
  }

  for (int i = 1; i <= log_k; i++) {
    for (int j = 0; j < n; j++) {
      dub[i][j] = dub[i - 1][dub[i - 1][j]];
    }
  }

  ll ans = 0;
  for (int i = 0; i <= log_k; i++) {
    if (k & (1LL << i)) {
      ans = dub[i][ans];
    }
  }
  cout << ans + 1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pai;
typedef pair<ll, ll> pal;
const int MOD = 1e9 + 7;
const int MODD = 998244353;
const ll LLINF = 1e18;

ll dub[62][200001];

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

  int log_k = log2(k);

  for (int i = 0; i < n; i++) {
    dub[0][i] = a[i];
  }

  for (int i = 1; i <= log_k; i++) {
    for (int j = 0; j < n; j++) {
      dub[i][j] = dub[i - 1][dub[i - 1][j]];
    }
  }

  ll ans = 0;
  for (int i = 0; i <= log_k; i++) {
    if (k & (1LL << i)) {
      ans = dub[i][ans];
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  ll now = 0;
  vector<ll> hist;
  set<ll> se;
  while (1) {
    hist.pb(now);
    se.insert(now);
    if (se.count(A[now]))
      break;
    now = A[now];
  }
  ll save = 0;
  rep(i, hist.size()) {
    if (hist[i] == A[now]) {
      save = i;
      break;
    }
  }
  vector<ll> new_hist;
  for (int i = save; i < hist.size(); i++) {
    new_hist.pb(hist[i]);
  }
  K -= save;
  ll len = new_hist.size();
  cout << new_hist[K % len] + 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  ll now = 0;
  vector<ll> hist;
  set<ll> se;
  while (1) {
    hist.pb(now);
    se.insert(now);
    if (se.count(A[now]))
      break;
    now = A[now];
  }
  ll save = 0;
  rep(i, hist.size()) {
    if (hist[i] == A[now]) {
      save = i;
      break;
    }
  }
  vector<ll> new_hist;
  for (int i = save; i < hist.size(); i++) {
    new_hist.pb(hist[i]);
  }
  if (K < save) {
    cout << hist[K] + 1 << endl;
    return 0;
  }
  K -= save;
  ll len = new_hist.size();
  cout << new_hist[K % len] + 1 << endl;
  return 0;
}
Problem p02684 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define in(n) cin >> (n)
#define out(ans) cout << (ans) << endl;
#define keta(n) cout << fixed << setprecision((n));
using ll = int64_t;
using v = vector<int>;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> tolist(N);
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    A--;
    tolist.at(i) = A;
  }

  bool roopjud = false;
  vector<bool> judlist(N, false);
  judlist.at(0) = true;

  ll gpoint = 0, npoint, rs = -1, mcount = 0, rcount = 0, ans;
  while (rs == -1 && mcount <= N) {
    npoint = tolist.at(gpoint);
    if (judlist.at(npoint)) {
      rs = gpoint;
      roopjud = true;
    } else {
      gpoint = npoint;
      mcount++;
    }
  }

  if (roopjud) {
    gpoint = rs;
    npoint = -1;
    while (npoint != rs) {
      npoint = tolist.at(gpoint);
      rcount++;
      gpoint = npoint;
    }

    if (K - mcount >= 0) {
      gpoint = rs;
      if ((K - mcount) % rcount == 0) {
        ans == rs;
      } else {
        for (ll i = 0; i < (K - mcount) % rcount; i++) {
          ans = tolist.at(gpoint);
          gpoint = ans;
        }
      }
    }

    else {
      gpoint = 0;
      for (ll i = 0; i < K; i++) {
        ans = tolist.at(gpoint);
        gpoint = ans;
      }
    }
  }

  else {
    gpoint = 0;
    for (ll i = 0; i < K; i++) {
      ans = tolist.at(gpoint);
      gpoint = ans;
    }
  }

  ans++;

  cout << ans << endl;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define in(n) cin >> (n)
#define out(ans) cout << (ans) << endl;
#define keta(n) cout << fixed << setprecision((n));
using ll = int64_t;
using v = vector<int>;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> tolist(N);
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    A--;
    tolist.at(i) = A;
  }

  bool roopjud = false;
  vector<bool> judlist(N, false);
  judlist.at(0) = true;

  ll gpoint = 0, npoint, rs = -1, mcount = 0, rcount = 0, ans;
  while (rs == -1 && mcount <= N) {
    npoint = tolist.at(gpoint);
    if (judlist.at(npoint)) {
      rs = gpoint;
      roopjud = true;
    } else {
      judlist.at(gpoint) = true;
      gpoint = npoint;
      mcount++;
    }
  }

  if (roopjud) {
    gpoint = rs;
    npoint = -1;
    while (npoint != rs) {
      npoint = tolist.at(gpoint);
      rcount++;
      gpoint = npoint;
    }

    if (K - mcount >= 0) {
      gpoint = rs;
      if ((K - mcount) % rcount == 0) {
        ans == rs;
      } else {
        for (ll i = 0; i < (K - mcount) % rcount; i++) {
          ans = tolist.at(gpoint);
          gpoint = ans;
        }
      }
    }

    else {
      gpoint = 0;
      for (ll i = 0; i < K; i++) {
        ans = tolist.at(gpoint);
        gpoint = ans;
      }
    }
  }

  else {
    gpoint = 0;
    for (ll i = 0; i < K; i++) {
      ans = tolist.at(gpoint);
      gpoint = ans;
    }
  }

  ans++;

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

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

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

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

  map<ll, ll> voyage;
  while (voyage[pos] < 1 && k > 0) {
    voyage[pos] = k;
    pos = a[pos];
    k--;
  }
  k %= voyage[pos] - k;
  while (k > 0) {
    pos = a[pos];
    k--;
  }
  cout << pos + 1 << "\n";

  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

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

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

  map<ll, ll> voyage;
  while (voyage[pos] < 1 && k > 0) {
    voyage[pos] = k;
    pos = a[pos];
    k--;
  }
  if (voyage[pos] > 0)
    k %= voyage[pos] - k;
  while (k > 0) {
    pos = a[pos];
    k--;
  }
  cout << pos + 1 << "\n";

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = s; i < (int)n; i++)
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  mp[0]++;
  ll key = 0;
  ll time = 0;
  ll loop = 0;
  vector<ll> vec;
  vector<ll> vec0;
  vec0.push_back(0);
  rep(i, 2 * n + 10) {
    mp[a[key] - 1]++;
    if (mp[a[key] - 1] == 1) {
      time++;
      vec0.push_back(a[key]);
    }
    if (mp[a[key] - 1] == 2) {
      loop++;
      vec.push_back(a[key]);
    }
    key = a[key] - 1;
  }
  if (k > time && (k - time) % loop != 0)
    cout << vec[(k - time) % loop - 1] << endl;
  if (k > time && (k - time) % loop == 0)
    cout << vec[(k - time) % loop - 1 + loop] << endl;
  else
    cout << vec0[k] << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = s; i < (int)n; i++)
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  mp[0]++;
  ll key = 0;
  ll time = 0;
  ll loop = 0;
  vector<ll> vec;
  vector<ll> vec0;
  vec0.push_back(0);
  rep(i, 2 * n + 10) {
    mp[a[key] - 1]++;
    if (mp[a[key] - 1] == 1) {
      time++;
      vec0.push_back(a[key]);
    }
    if (mp[a[key] - 1] == 2) {
      loop++;
      vec.push_back(a[key]);
    }
    key = a[key] - 1;
  }
  if (k > time && (k - time) % loop != 0)
    cout << vec[(k - time) % loop - 1] << endl;
  else if (k > time && (k - time) % loop == 0)
    cout << vec[loop - 1] << endl;
  else
    cout << vec0[k] << endl;
}
Problem p02684 (C++)

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

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

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

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

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

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

    time++;
  }

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

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

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

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

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

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

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

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

    time++;
  }

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

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

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

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

  vector<int> tp(N + 1);
  tp[0] = 1;

  vector<int> visited(N + 1, -1);
  visited[1] = 0;

  ll cycle = 0;
  ll first = 0;
  for (ll i = 1; i <= N; i++) {
    tp[i] = A[tp[i - 1]];
    if (visited[tp[i]] != -1) {
      first = visited[tp[i]];
      cycle = i - visited[tp[i]];
      break;
    } else {
      visited[tp[i]] = i;
    }
  }
  ll n;
  if (K > first)
    n = ((K - first) % cycle) + first;
  else
    n = K;
  cout << tp[K] << endl;
}


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

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

  vector<int> tp(N + 1);
  tp[0] = 1;

  vector<int> visited(N + 1, -1);
  visited[1] = 0;

  ll cycle = 0;
  ll first = 0;
  for (ll i = 1; i <= N; i++) {
    tp[i] = A[tp[i - 1]];
    if (visited[tp[i]] != -1) {
      first = visited[tp[i]];
      cycle = i - visited[tp[i]];
      break;
    } else {
      visited[tp[i]] = i;
    }
  }
  ll n;
  if (K > first)
    n = ((K - first) % cycle) + first;
  else
    n = K;
  cout << tp[n] << endl;
}