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

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

int a[200020];
int lps = 1, lpp = 0, lpl = 0; // loop start, loop path, loop length
map<int, int> was;

pair<int, pair<int, int>> lpGet(int i, int s) {
  // printf(">>>> %d \\n",i);
  if (was.count(i))
    return make_pair(i, make_pair(was[i], s));
  was[i] = s;
  return lpGet(a[i], s + 1);
}

int walk(int start, int s) {
  if (s == 0)
    return start;
  return walk(a[start], s - 1);
}

int main() {
  int n;
  long long k;
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i + 1]);
  }

  pair<int, pair<int, int>> lp = lpGet(1, 0);
  // printf("lpGet: done\\n");
  // printf("lpGet: (%d, (%d,
  // %d))\\n",lp.first,lp.second.first,lp.second.second);
  lpp = lp.second.first;
  lps = lp.first;

  if (k < lpp) {
    printf("%d\\n", (walk(1, k)));
    return 0;
  }

  was.clear();
  lpl = lpGet(lps, 0).second.second;

  k = k - lpp;
  k %= lpl;

  // printf("%lld %d %d \\n",k,lps,lpp);

  printf("%d\\n", (walk(lps, k)));
  return 0;
}

Error: TLE

Fixed submission:

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

int a[200020];
map<int, bool> was;
int lps = 1, lpp = 0, lpl = 0; // loop start, loop path, loop length

int lppGet(int i, int s) {
  if (i == lps)
    return s;
  return lppGet(a[i], s + 1);
}

int walk(int start, int s) {
  if (s == 0)
    return start;
  return walk(a[start], s - 1);
}

int main() {
  int n;
  long long k;
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i + 1]);
    if (was.count(a[i + 1])) {
      lps = a[i + 1];
    }
    was[a[i + 1]] = 1;
  }
  // printf("input done..\\n");
  if (lps != 1) {
    lpp = lppGet(1, 0);
  }

  if (k < lpp) {
    printf("%d\\n", (walk(1, k)));
    return 0;
  }

  lpl = lppGet(a[lps], 1);

  k = k - lpp;
  k %= lpl;
  // printf("%lld %d %d \\n",k,lps,lpp);
  printf("%d\\n", (walk(lps, k)));
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define forr(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()), (a.end())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL << 60;
const int INF = 1001001001;
const int MOD = 1000000007;

/* --------------------------------------------------- */

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n + 1);
  rrep(i, n) { cin >> A[i]; }
  vector<ll> check(n + 1, 0);
  check[1] = 1;
  vector<ll> ans;
  ll x = 1;
  ans.push_back(1);
  ll key;
  while (true) {
    x = A[x];
    if (check[x] == 1) {
      key = x;
      break;
    } else
      check[x] = 1;
    ans.push_back(x);
  }
  ll res;
  vector<ll> c;
  ll cnt = ans.size();
  rep(i, cnt) {
    if (ans[i] != key) {
      k--;
      if (k == 0) {
        res = ans[i + 1];
        cout << res << endl;
        return 0;
      }
    } else {
      forr(j, i, cnt) c.push_back(ans[j]);
      break;
    }
  }

  cnt = c.size();
  ll a = k % cnt;
  cout << c[a] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define forr(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()), (a.end())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL << 60;
const int INF = 1001001001;
const int MOD = 1000000007;

/* --------------------------------------------------- */

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n + 1);
  rrep(i, n) { cin >> A[i]; }
  vector<ll> check(n + 1, 0);
  check[1] = 1;
  vector<ll> ans;
  ll x = 1;
  ans.push_back(1);
  ll key;
  while (true) {
    x = A[x];
    if (check[x] == 1) {
      key = x;
      break;
    } else
      check[x] = 1;
    ans.push_back(x);
  }
  vector<ll> c;
  ll cnt = ans.size();
  rep(i, cnt) {
    if (ans[i] != key) {
      k--;
    } else {
      forr(j, i, cnt) c.push_back(ans[j]);
      break;
    }
  }

  cnt = c.size();
  ll a = k % cnt;
  cout << c[a] << endl;

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: 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, a; cin >> a; i++) {
    A.at(i) = --a;
  }
  vector<int> V;
  vector<bool> B(N);
  long now = 0, last = -1;
  while (N--) {
    if (B.at(now)) {
      last = now;
      break;
    }
    V.push_back(now);
    B.at(now) = true;
    now = A.at(now);
  }
  vector<int> X, Y;
  bool b = 0;
  for (int i = 0; i < V.size(); i++) {
    if (V.at(i) == last)
      b = 1;
    if (!b)
      X.push_back(V.at(i));
    else
      Y.push_back(V.at(i));
  }
  if (K < X.size()) {
    cout << X.at(K) + 1 << "\\n";
  } else {
    K -= X.size();
    cout << Y.at(K % Y.size()) + 1 << "\\n";
  }
}

Error: 0

Fixed submission:

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> A(N);
  for (long i = 0; i < N; i++)
    cin >> A.at(i);
  for (long i = 0; i < N; i++) {
    A.at(i)--;
  }
  K--;
  vector<long> V;
  set<long> SE;
  long cur = 0;
  long tmp = -1;
  for (long i = 0; i < N; i++) {
    if (SE.count(A.at(cur))) {
      tmp = A.at(cur);
      break;
    }
    V.push_back(A.at(cur));
    SE.insert(A.at(cur));
    cur = A.at(cur);
    K--;
  }
  if (tmp = -1) {
    N = V.size();
    cout << V.at(K % N) + 1 << "\\n";
  } else {
    while (V.at(0) != tmp) {
      vector<long> copy = V;
      V.erase(unique(V.begin(), V.end()), V.end());
      if (copy.size() == 1)
        break;
      V.erase(V.begin() + 0);
      K--;
    }
    N = V.size();
    cout << V.at(K % N) + 1 << "\\n";
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long int
#define M 1000000007

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> v[i];

  vector<bool> a(n + 1, false);
  ll ans = 0;
  ll p = 1;

  while (1) {
    if (a[p] == true)
      break;
    a[p] = true;
    p = v[p];
    ans++;
  }
  for (auto i : a)
    i = false;

  ll ans1 = 0;
  while (1) {
    if (a[p] == true)
      break;
    a[p] = true;
    p = v[p];
    ans1++;
  }
  // cout<<ans<<" "<<ans1<<endl;

  // ans=ans+1;
  // cout<<p<<" ";
  if (k <= ans - ans1) {
    // k=k%ans;
    p = 1;
    while (k--) {
      p = v[p];
    }
    cout << p << endl;
  } else {
    k = k - (ans - ans1);
    k = k % ans1;
    while (k--) {
      p = v[p];
    }
    cout << p << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll unsigned long long int
#define M 1000000007

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> v[i];

  vector<bool> a(n + 1, false);
  ll ans = 0;
  ll p = 1;

  while (1) {
    if (a[p] == true)
      break;
    a[p] = true;
    p = v[p];
    ans++;
  }
  for (auto i : a)
    i = false;

  ll ans1 = 0;
  while (1) {
    if (a[p] == true)
      break;
    a[p] = true;
    p = v[p];
    ans1++;
  }
  // cout<<ans<<" "<<ans1<<endl;

  // ans=ans+1;

  if (k <= ans1 - ans) {
    // k=k%ans;
    p = 1;
    while (k--) {
      p = v[p];
    }
    cout << p << endl;
  } else {
    k = k - (ans1 - ans);
    k = k % ans;
    while (k--) {
      p = v[p];
    }
    cout << p << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  long long N, K;
  cin >> N >> K;

  vector<long long> A(N);

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

  // ルート:root[i] -> i番目の移動先
  vector<int> root;

  // 通ったか否かを管理
  // 通った場合は、何番目に通ったかを記憶
  // 通っていない場合は、-1
  vector<int> visited(N + 1, -1);

  // 現在地点:1からスタートする
  int current = 1;

  // 同じ位置を通るまでループ
  while (visited[current] == -1) {

    // ルートに現在地点を追加
    root.push_back(current);

    // x番目に通った
    visited[current] = root.size();

    // 現在地点を更新
    current = A[current - 1];
  }

  // ループしている町の個数
  int roopSize = root.size() - visited[current] + 1;

  /*
  for (int i = 0; i <(int) root.size(); i++) {
    cout << root[i] << " ";
  }
  cout << endl;
  */

  // ループするまでに移動する町の個数
  int noRoopSize = visited[current] - 1;

  if (K < noRoopSize) {
    // K がループするまでに移動する町の個数より小さい場合は
    // K 番目を出力
    cout << root[K] << endl;
  } else {
    // そうじゃない場合は、余りの位置
    K -= noRoopSize;
    K %= roopSize;

    /*
    cout << roopSize << endl;
    cout << noRoopSize << endl;
    cout << K << endl;
    */

    cout << root[noRoopSize + K] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  long long N, K;
  cin >> N >> K;

  vector<long long> A(N);

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

  bool flg = true;
  int current = 0;
  vector<int> root;
  map<int, bool> visited;

  while (flg) {
    if (visited[current] == false) {
      visited[current] = true;
      root.push_back(current);
      current = A[current];
    } else {
      flg = false;
    }
  }

  vector<int> roop;
  int k = K;

  for (int i = 0; i < (int)root.size(); i++) {
    if (root[i] == current) {
      k -= i;
      flg = true;
    }
    if (flg) {
      roop.push_back(root[i]);
    }
  }

  if (k < 0) {
    cout << root[K - 1] << endl;
  } else {
    long long stop = k % roop.size();
    cout << roop[stop] + 1 << endl;
  }

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

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

int main() {
  long long n, k;
  cin >> n >> k;
  long long a[n + 1];
  for (long long i = 1; i <= n; i++)
    cin >> a[i];
  vector<long long> path;
  set<long long> pr;

  long long curr = 1;
  path.push_back(curr);
  pr.insert(1);
  long long st = 0;
  while (true) {
    curr = a[curr];
    if (pr.find(curr) != pr.end()) {
      // st = cycle start number
      st = curr;
      break;
    } else {
      path.push_back(curr);
      pr.insert(curr);
    }
  }
  vector<long long>::iterator it = find(path.begin(), path.end(), st);
  long long b = it - path.begin();
  long long l = path.size();
  long long int i;
  // for(i=0;i<l;i++)
  // {
  //     cout << path[i] << " ";
  // }
  // cout << '\\n';
  // cout << b  << " " << l << '\\n';

  if (k < l) {
    cout << path[k] << endl;
    return 0;
  }
  // cout << "YEs\\n";
  k -= b;
  l -= b;
  long long rem = (k % l);

  // cout << b << " " << rem << '\\n';
  if ((b + rem) == 0) {
    cout << path[b] << endl;
    return 0;
  }
  cout << a[path[b + rem - 1]] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  long long n, k;
  cin >> n >> k;
  long long a[n + 1];
  for (long long i = 1; i <= n; i++)
    cin >> a[i];
  vector<long long> path;
  set<long long> pr;

  long long curr = 1;
  path.push_back(curr);
  pr.insert(1);
  long long st = 0;
  while (true) {
    curr = a[curr];
    if (pr.find(curr) != pr.end()) {
      // st = cycle start number
      st = curr;
      break;
    } else {
      path.push_back(curr);
      pr.insert(curr);
    }
  }
  vector<long long>::iterator it = find(path.begin(), path.end(), st);
  long long b = it - path.begin();
  long long l = path.size();
  long long int i;
  // for(i=0;i<l;i++)
  // {
  //     cout << path[i] << " ";
  // }
  // cout << '\\n';
  // cout << b  << " " << l << '\\n';

  if (k < l) {
    cout << path[k] << endl;
    return 0;
  }
  // cout << "YEs\\n";
  k -= b;
  l -= b;
  long long rem = k % l;
  // cout << b + rem - 1 << '\\n';
  cout << a[path[b + rem - 1]] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

ll N, K;
vector<ll> A;
vector<bool> visit;

int main() {
  //  ll ans;
  ll lk, lm;
  ll pos, start;
  ll rest;

  cin >> N >> K;
  A.resize(N + 1);
  visit.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    visit[i] = false;
  }

  pos = 1;
  for (lk = 0; lk < K; lk++) {
    // printf("%lld ", pos);
    if (visit[pos])
      break;
    visit[pos] = true;
    pos = A[pos];
  }
  // printf("\\n");
  if (lk >= K) {
    cout << pos << endl;
    return 0;
  }
  // printf("lk = %lld K%%lk = %lld\\n", lk, K%lk);
  start = pos;
  for (lm = 0; lm + lk < K;) {
    pos = A[pos];
    lm++;
    if (pos == start)
      break;
  }
  if (lm + lk >= K) {
    cout << pos << endl;
    return 0;
  }
  // printf("lm = %lld\\n", lm);
  K -= lk;
  rest = K % lm;
  for (lm = 0; lm < rest; lm++) {
    pos = A[pos];
  }
  cout << pos << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

ll N, K;
vector<ll> A;
vector<bool> visit;

int main() {
  //  ll ans;
  ll lk, lm;
  ll pos, start;
  ll rest;

  cin >> N >> K;
  A.resize(N + 1);
  visit.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    visit[i] = false;
  }

  pos = 1;
  for (lk = 0; lk < K; lk++) {
    // printf("%lld ", pos);
    if (visit[pos])
      break;
    visit[pos] = true;
    pos = A[pos];
  }
  // printf("\\n");
  if (lk >= K) {
    cout << pos << endl;
    return 0;
  }
  // printf("lk = %lld K%%lk = %lld\\n", lk, K%lk);
  start = pos;
  for (lm = 0; lm + lk < K; lm++) {
    pos = A[pos];
    if (pos == start)
      break;
  }
  if (lm + lk >= K) {
    cout << pos << endl;
    return 0;
  }
  // printf("lm = %lld\\n", lm);
  K -= lk;
  rest = K % lm;
  for (lm = 0; lm < rest; lm++) {
    pos = A[pos];
  }
  cout << pos << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
typedef long long ll;
ll n, m, a[200002], b[200002], s[200002], L, tmp, tmp2;
int main(void) {
  cin >> n >> m;
  m++;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  L = 1;
  for (int i = 1; i <= n + 1; i++) {
    b[i] = L;
    if (s[L] != 0) {
      tmp = i;
      break;
    }
    s[L] = i;
    L = a[L];
  }
  if (m <= tmp) {
    cout << b[m] << endl;
    return 0;
  } else {
    // cout<<m<<" "<<s[L]<<" "<<tmp<<" "<<s[L]<<endl;
    tmp2 = (m - s[L] + 1) % (tmp - s[L]);
    if (tmp2 == 0)
      tmp2 = tmp - s[L];
  }
  // cout<<tmp2<<endl;
  for (int i = 1; i <= tmp2; i++) {
    s[L]++;
  }
  cout << b[s[L] - 1] << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;
ll n, m, a[100002], b[100002], s[100002], L, tmp, tmp2;
int main(void) {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  L = 1;
  for (int i = 1; i <= n + 1; i++) {
    b[i] = L;
    if (s[L] != 0) {
      tmp = i;
      break;
    }
    s[L] = i;
    L = a[L];
  }
  if (m <= tmp) {
    cout << b[m] << endl;
    return 0;
  } else {
    // cout<<m<<" "<<s[L]<<" "<<tmp<<" "<<s[L]<<endl;
    tmp2 = (m - s[L] + 1) % (tmp - s[L]);
    if (tmp2 == 0)
      tmp2 = tmp - s[L];
  }
  // cout<<tmp2<<endl;
  for (int i = 1; i <= tmp2; i++) {
    s[L]++;
  }
  cout << b[s[L]] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define ll long long
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define hell 1000000007
#define PI 3.141592653589793

int main() {
  fastio;
  int testcases = 1;
  // cin>>testcases;
  while (testcases--) {
    int n;
    ll k;
    cin >> n >> k;
    vi towns(n + 1);
    for (int i = 1; i <= n; i++)
      cin >> towns[i];
    vi visited;
    int current = 1;
    int used = 0;
    while (used < n) {
      visited.pb(towns[current]);
      current = towns[current];
      ++used;
      if (used >= k)
        break;
    }

    if (used < k) {
      int pos;
      for (int i = 0; i < sz(visited); i++) {
        if (visited[i] == towns[current]) {
          pos = i;
          break;
        }
      }
      int period = used - pos;
      int rem = (k - used) % period;
      if (rem == 0)
        rem = period;
      current = visited[pos + rem - 1];
    }
    cout << current;
    cout << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define ll long long
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define hell 1000000007
#define PI 3.141592653589793

int main() {
  fastio;
  int testcases = 1;
  // cin>>testcases;
  while (testcases--) {
    int n;
    ll k;
    cin >> n >> k;
    vi towns(n + 1);
    for (int i = 1; i <= n; i++)
      cin >> towns[i];
    vi visited;
    int current = 1;
    int used = 0;
    while (!cpresent(visited, towns[current])) {
      visited.pb(towns[current]);
      current = towns[current];
      ++used;
      if (used >= k)
        break;
    }

    if (used < k) {
      int pos;
      for (int i = 0; i < sz(visited); i++) {
        if (visited[i] == towns[current]) {
          pos = i;
          break;
        }
      }
      int period = used - pos;
      int rem = (k - used) % period;
      if (rem == 0)
        rem = period;
      current = visited[pos + rem - 1];
    }
    cout << current;
    cout << "\\n";
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  int cnt = 1, tmp = k, aa = 0;
  vector<int> check(n, 0);
  check[0] = 1;
  for (long long i = 1; i <= k; i++) {
    cnt = a[cnt - 1];
    if (check[cnt - 1] == 0)
      check[cnt - 1] = i + 1;
    else {
      tmp = i + 1 - check[cnt - 1];
      aa = i - tmp;
      // cout<<tmp<<' '<<cnt<<' '<<aa<<endl;
      break;
    }
  }
  tmp = (k - aa) % tmp;
  for (int i = 0; i < tmp; i++) {
    cnt = a[cnt - 1];
  }
  cout << cnt << endl;
}


Error: 0

Fixed submission:

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

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

  int cnt = 1, tmp, aa;
  vector<int> check(n, 0);
  check[0] = 1;
  for (long long i = 1; i <= k; i++) {
    cnt = a[cnt - 1];
    if (check[cnt - 1] == 0)
      check[cnt - 1] = i + 1;
    else {
      tmp = i + 1 - check[cnt - 1];
      aa = i - tmp;
      // cout<<tmp<<' '<<cnt<<' '<<aa<<endl;
      break;
    }
  }
  tmp = (k - aa) % tmp;
  for (int i = 0; i < tmp; i++) {
    cnt = a[cnt - 1];
  }
  cout << cnt << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
  ll N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> B(N + 1);
  bool flag1 = true;
  set<int> list;
  int res = 1;
  rep(i, N) { cin >> A.at(i); }
  B.at(0) = 1;
  for (int i = 0; i < N; i++) {
    B.at(i + 1) = A.at(B.at(i) - 1);
  }
  bool flag = true;
  ll s, t;
  rep(i, N + 1) {
    if (flag1) {
      if (list.count(B[i])) {
        res = B[i];
        t = i;
        flag1 = false;
      }
      list.insert(B[i]);
    }
    if (flag1 == false) {
      break;
    }
  }
  rep(i, t) {
    if (B[i] == res) {
      s = i;
      break;
    }
  }
  if (K <= s) {
    cout << B[K] << endl;
  } else {
    cout << B[s + (K - s) % (t - s)] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
  ll N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> B(N + 1);
  bool flag1 = true;
  set<int> list;
  int res = 1;
  rep(i, N) {
    cin >> A.at(i);
    if (flag1) {
      if (list.count(A.at(i))) {
        res = A.at(i);
        flag1 = false;
      }
      list.insert(A.at(i));
    }
  }
  B.at(0) = 1;
  for (int i = 0; i < N; i++) {
    B.at(i + 1) = A.at(B.at(i) - 1);
  }
  bool flag = true;
  bool flag2 = true;
  ll s, t;
  rep(i, N + 1) {
    if (res == B[i]) {
      if (flag) {
        s = i;
        flag = false;
      } else if (flag2) {
        t = i;
        flag2 = false;
      }
    }
    if (flag == false && flag2 == false) {
      break;
    }
  }
  if (K <= s) {
    cout << B[K] << endl;
  } else {
    cout << B[s + (K - s) % (t - s)] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

int main(void) {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  vector<bool> seen(n, false);
  deque<int> path;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  int x = 0;
  while (true) {
    if (seen[x]) {
      while (path[0] != x) {
        path.pop_front();
        --k;
        if (k == 0) {
          cout << path[0] + 1 << endl;
          return 0;
        }
      }
      break;
    }
    seen[x] = true;
    path.push_back(x);
    x = a[x];
  }

  cout << path[k % path.size()] + 1 << endl;
  return 0;
}


Error: WA

Fixed submission:

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

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

int main(void) {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  vector<bool> seen(n, false);
  deque<int> path;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  int x = 0;
  while (true) {
    if (seen[x]) {
      while (a[0] != x) {
        path.pop_front();
        --k;
        if (k == 0) {
          cout << a[0] + 1 << endl;
          return 0;
        }
      }
      break;
    }
    seen[x] = true;
    path.push_back(x);
    x = a[x];
  }

  cout << a[k % path.size()] + 1 << endl;
  return 0;
}