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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
  int n, k, i, j;
  cin >> n >> k;
  int a[n + 1];
  for (i = 1; i <= n; i++)
    cin >> a[i];
  vector<int> v;
  int x, b = a[1];
  v.push_back(b);
  map<int, int> m;
  m[b] = v.size() - 1;
  while (1) {
    b = a[b];
    if (m.count(b)) {
      j = m[b];
      break;
    }
    v.push_back(b);
    m[b] = v.size() - 1;
  }
  if (k <= j)
    cout << v[k - 1];
  else {
    k = k - j - 1;
    x = v.size() - j;
    k = k % x;
    cout << v[k + j];
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
  int n, k, i, j;
  cin >> n >> k;
  int a[n + 1];
  for (i = 1; i <= n; i++)
    cin >> a[i];
  vector<int> v;
  int x, b = a[1];
  v.push_back(b);
  while (1) {
    b = a[b];
    auto it = find(v.begin(), v.end(), b);
    if (it != v.end()) {
      j = it - v.begin();
      break;
    }
    v.push_back(b);
  }
  if (k <= j)
    cout << v[k - 1];
  else {
    k = k - j - 1;
    x = v.size() - j;
    k = k % x;
    cout << v[k + j];
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define For(a, b, c) for (int i = a; i < b; i += c)
#define It(m) for (auto it = m.begin(); it != m.end(); it++)
#define N 500500
#define speed                                                                  \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const ll mod = (ll)1e9 + 7;

int main() {
  speed;
  ll n, k;
  cin >> n >> k;
  vi v(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> v[i + 1];
  }
  ll j = 1;
  ll k1 = k;
  unordered_map<ll, ll> m;
  ll cnt = 0;
  ll mod = 1;
  while (k--) {
    if (m[j] == 0) {
      m[j] = cnt;
      j = v[j];
    } else {
      mod = cnt - m[j];
      break;
    }
    cnt++;
  }
  k = (k1 - m[j]) % mod;
  while (k--) {
    j = v[j];
  }
  cout << j;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define For(a, b, c) for (int i = a; i < b; i += c)
#define It(m) for (auto it = m.begin(); it != m.end(); it++)
#define N 500500
#define speed                                                                  \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const ll mod = (ll)1e9 + 7;

int main() {
  speed;
  ll n, k;
  cin >> n >> k;
  vi v(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> v[i + 1];
  }
  ll j = 1;
  ll k1 = k;
  unordered_map<ll, ll> m;
  ll cnt = 0;
  ll mod = 0;
  while (k--) {
    if (m[j] == false) {
      m[j] = cnt;
      j = v[j];
    } else {
      mod = cnt - m[j];
      break;
    }
    cnt++;
  }
  k = (k1 - m[j]) % mod;
  while (k--) {
    j = v[j];
  }
  cout << j;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<bool> seen(n, false);
  vector<int> c(0);
  int now = 0;
  seen[now] = true;
  c.push_back(now);
  int st;
  while (true) {
    now = a[now];
    if (seen[now] == true) {
      st = now;
      break;
    }
    seen[now] = true;
    c.push_back(now);
    // cout << now << endl;
  }
  if (k < c.size()) {
    cout << c[k] + 1 << endl;
    return 0;
  }
  rep(i, c.size()) {
    if (c[i] == st) {
      cout << c[(k - i) % (c.size() - i) + i] + 1 << endl;
      break;
    }
  }
  //    cout << c.size() << endl;
  //    cout << c[k%c.size()] + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:


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

int main() {
#define int long long
  int X, Y, B, C, D, N, K, rs;
  string S, T;
  cin >> N >> K;
  vector<int> A(N);
  for (int j = 0; j < N; j++) {

    cin >> A[j];
  }
  int count = 0;
  int cp = 0;
  while (count < K) {
    count++;
    cp = A[cp] - 1;
  }
  rs = cp + 1;
  cout << rs << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
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;
}

bool seen[200010];
int dd[2000010];

int r, nr;
int dfs(vector<int> a, ll k, int n) {
  seen[1] = true;
  dd[1] = 0;
  int next = a[1];
  ll cnt = 0;
  while (true) {
    cnt++;

    if (k == cnt) {
      cout << next << endl; // end
      return 0;
    }

    if (!seen[next]) {
      seen[next] = true;
      dd[next] = cnt;
      next = a[next];

    } else {
      ll ans;

      r = cnt - dd[next];
      nr = dd[next];
      int index = nr + (k - nr) % r;

      for (ll i = 1; i <= n; i++) {
        if (index == dd[i])
          ans = i;
      }
      cout << ans << endl; // end
      return 0;
    }
  }
}
int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n + 1);
  rep(i, n) cin >> a[i + 1];
  memset(seen, false, sizeof(seen));
  memset(dd, -1, sizeof(dd));
  int b = dfs(a, k, n);
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
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;
}

bool seen[200010];
int dd[2000010];

int r, nr;
int dfs(vector<int> a, ll k, int n) {
  seen[1] = true;
  dd[1] = 0;
  int next = a[1];
  ll cnt = 0;
  while (true) {
    cnt++;

    if (k == cnt) {
      cout << next << endl; // end
      return 0;
    }

    if (!seen[next]) {
      seen[next] = true;
      dd[next] = cnt;
      next = a[next];

    } else {
      ll ans;

      r = cnt - dd[next];
      nr = dd[next];
      int index = nr + (k - nr) % r;

      for (ll i = 1; i <= n; i++) {
        if (index == dd[i])
          ans = i;
      }
      cout << ans << endl; // end
      return 0;
    }
  }
}
int main() {
  int n;
  ll k;
  vector<int> a(n + 1);
  cin >> n >> k;
  rep(i, n) cin >> a[i + 1];
  memset(seen, false, sizeof(seen));
  int b = dfs(a, k, n);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
using ll = long long;

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

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

  vector<int> d;
  set<int> v;
  d.push_back(0);
  v.insert(0);
  int loop = 0;
  for (int i = 0; i < n - 1; ++i) {
    int nx = a[d[i]];
    if (v.find(nx) != v.end()) {
      loop = d.size() - distance(d.begin(), find(d.begin(), d.end(), nx));
      break;
    }
    d.push_back(nx);
    v.insert(nx);
  }

  int noLoop = d.size() - loop;
  if (k <= noLoop) {
    int ans = d[k];
    cout << ++ans << '\\n';
    return 0;
  }
  int ans = d[noLoop + (k - noLoop) % loop];
  cout << ++ans << '\\n';

  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
using ll = long long;

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

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

  vector<int> d;
  d.push_back(0);
  set<int> v;
  int loop = 0;
  for (int i = 0; i < n - 1; ++i) {
    int nx = a[d[i]];
    if (v.find(nx) != v.end()) {
      loop = d.size() - distance(d.begin(), find(d.begin(), d.end(), nx));
      break;
    }
    d.push_back(nx);
    v.insert(nx);
  }

  int noLoop = d.size() - loop;
  int ans = d[noLoop + (k - noLoop) % loop];
  cout << ++ans << '\\n';

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

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  ll k2 = k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> seen(n, 0);
  deque<int> que;
  ll now = 0;
  while (1) {
    if (k == 0) {
      break;
    }
    if (seen[now]) {
      break;
    }
    que.push_back(now);
    seen[now] = 1;
    now = a[now];
    k--;
  }

  if (k == 0) {
    cout << now + 1 << endl;
    return 0;
  }

  while (que[0] != now) {
    que.pop_front();
    k2--;
    if (k2 == 0) {
      cout << que[0] + 1 << endl;
      return 0;
    }
  }
  k2 %= que.size();
  cout << que[k2] + 1 << endl;
  return 0;
}

Error: 0

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);
  vector<ll> seen(n);
  seen.assign(n, 0);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  ll now = 0;
  ll mcycle = 0;

  while (1) {
    if (k <= 0) {
      break;
    }
    if (mcycle >= n) {
      break;
    }
    if (seen[a[now]] != 0) {
      break;
    }
    now = a[now];
    k--;
    seen[now] = 1;
    mcycle++;
  }

  vector<ll> seen2;
  seen2.assign(mcycle, 0);
  ll cycle = 1;
  seen2[now] = 1;

  if (k > 0) {
    while (seen2[a[now]] == 0 || mcycle <= 0) {
      now = a[now];
      cycle++;
      seen2[now]++;
      mcycle--;
    }
  }

  k %= cycle;
  for (ll i = 0; i <= k; ++i) {
    now = a[now];
  }
  cout << now + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: 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[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;
}


Error: 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[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;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
typedef long long ll;
using namespace std;

ll llmin(ll x, ll y) {
  if (x > y)
    return y;
  return x;
}

bool reached[200001];
ll first[200001];
ll a[200001];

int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll tele = 0, now = 1, roop;
  reached[1] = true;
  first[1] = 0;
  while (1) {
    tele++;
    now = a[now];
    if (reached[now] == true) {
      k -= first[now];
      roop = tele - first[now];
      break;
    }
    reached[now] = true;
    first[now] = tele;
    if (tele == k) {
      cout << now << endl;
      return 0;
    }
  }
  ll num = k % roop;
  for (int i = 0; i < num; i++) {
    now = a[now];
  }
  cout << now << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
typedef long long ll;
using namespace std;

ll llmin(ll x, ll y) {
  if (x > y)
    return y;
  return x;
}

bool reached[200001];
int first[200001];

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int tele = 0, now = 1, roop;
  reached[1] = true;
  first[1] = 0;
  while (1) {
    tele++;
    now = a[now];
    if (reached[now] == true) {
      k -= first[now];
      roop = tele - first[now];
      break;
    }
    reached[now] = true;
    first[now] = tele;
    if (tele == k) {
      return 0;
    }
  }
  ll num = k % roop;
  int ans = 1;
  for (int i = 0; i < num; i++) {
    now = a[now];
  }
  cout << now << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: 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;
    if (pos != -1)
      ans = B.at(pos);
    else
      ans = 0;
  }
  cout << ans + 1 << endl;
}


Error: 0

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;
    ans = B.at(pos);
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int n;
  long long k;
  std::cin >> n >> k;

  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    std::cin >> a[i];

  std::vector<int> point(n + 1, -1);
  std::vector<int> log;
  int v = 1;

  while (point[v] == -1) {
    point[v] = log.size();
    log.push_back(v);
    v = a[v - 1];
  }
  int T = log.size() - point[v];
  int l = point[v];

  if (k < l)
    std::cout << log[k] << std::endl;
  else {
    k -= l;
    k %= T;
    std::cout << log[l + k] << std::endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int n;
  long long k;
  std::cin >> n >> k;

  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    std::cin >> a[i];

  std::vector<int> point(n + 1, -1);
  std::vector<int> log;
  int v = 1;

  while (point[v] == -1) {
    point[v] = log.size();
    log.push_back(v);
    v = a[v - 1];
  }
  int T = log.size() - a[v];
  int l = a[v];

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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll, ll> prl;
typedef vector<ll> vcl;
typedef map<ll, ll> mapl;
typedef unordered_map<ll, ll> umap;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repi(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll mod = 1e9 + 7;

int main() {
  // your code goes here
  ll n, k, a[200005], now = 1, s, l, ct = -1, cm;
  bool v[200005] = {0}, ok = false;
  vcl cy, cc;
  cin >> n >> k;
  rep(i, 1, n) { cin >> a[i]; }
  cy.pb(1);
  v[1] = true;
  while (1) {
    now = a[now];
    if (!v[now]) {
      cy.pb(now);
      v[now] = true;
    } else {
      l = now;
      break;
    }
  }
  for (auto to : cy) {
    ct++;
    if (to == l && !ok) {
      ok = true;
      cm = ct;
    }
    if (ok) {
      cc.pb(to);
    }
  }
  if (k < cm) {
    cout << cy[k] << endl;
    return 0;
  }
  s = cc.size();
  k = (k - cm) % s;
  cout << cc[k] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll, ll> prl;
typedef vector<ll> vcl;
typedef map<ll, ll> mapl;
typedef unordered_map<ll, ll> umap;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repi(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll mod = 1e9 + 7;

int main() {
  // your code goes here
  ll n, k, a[200005], now = 1, s, l, ct = -1, cm;
  bool v[200005] = {0}, ok = false;
  vcl cy, cc;
  cin >> n >> k;
  rep(i, 1, n) { cin >> a[i]; }
  cy.pb(1);
  v[1] = true;
  while (1) {
    now = a[now];
    if (!v[now]) {
      cy.pb(now);
      v[now] = true;
    } else {
      l = now;
      break;
    }
  }
  for (auto to : cy) {
    ct++;
    if (to == l && !ok) {
      ok = true;
      cm = ct;
    }
    if (ok) {
      cc.pb(to);
    }
  }
  s = cc.size();
  k = (k - cm) % s;
  if (k < cm)
    cout << cy[k] << endl;
  else
    cout << cc[k] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  int nxt = a[0] - 1;
  vector<int> route(0);
  route.emplace_back(0);
  route.emplace_back(nxt);
  vector<bool> visited(n);
  visited[0] = true;
  while (visited[nxt] == false) {
    visited[nxt] = true;
    nxt = a[nxt] - 1;
    route.emplace_back(nxt);
  }
  int begin;
  rep(i, route.size()) {
    if (route[i] == nxt) {
      begin = i;
      break;
    }
  }
  ll p;
  if (k <= begin)
    p = k;
  else {
    p = (ll)begin + (k - begin) % (route.size() - 1 - begin);
  }
  cout << route[p] + 1 << endl;

  return 0;
}

Error: -8

Fixed submission:

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

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

  int nxt = a[0] - 1;
  vector<int> route(0);
  route.emplace_back(nxt);
  vector<bool> visited(n);
  visited[0] = true;
  while (visited[nxt] == false) {
    visited[nxt] = true;
    nxt = a[nxt] - 1;
    route.emplace_back(nxt);
  }
  int begin = -1;
  int end = -1;
  rep(i, route.size()) {
    if (route[i] == nxt && begin == -1) {
      begin = i + 1;
      break;
    }
  }
  ll p;
  if (k <= begin)
    p = k;
  else {
    p = (ll)begin + (k - begin) % (route.size() - begin);
  }
  cout << route[p - 1] + 1 << endl;

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int v = 1, c = 1, l = 0;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.push_back(v);
    v = a[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;
}


Error: TLE

Fixed submission:

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

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

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int v = 1, c = 1, l = 0;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.push_back(v);
    v = a[v];
  }
  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;
}