alexjercan/bugnet huggingface dataset:
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
using P = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

  vector<int> town;
  set<int> st;
  town.push_back(0);
  st.insert(0);
  int i = 0;
  while (true) {
    k--;
    if (k == 0) {
      cout << a[i] + 1 << endl;
      return 0;
    }
    if (st.find(a[i]) == st.end()) {
      town.push_back(a[i]);
      st.insert(a[i]);
      i = a[i];
      continue;
    } else {
      vector<int> loop;
      rep(j, town.size()) {
        if (a[i] == town[j]) {
          for (int k = j; k < town.size(); k++)
            loop.push_back(town[k]);
          break;
        }
      }
      k %= loop.size();
      cout << loop[k] + 1 << endl;
      return 0;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef long long int lli;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

int main() {
  FastIO;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    ll ai;
    cin >> ai;
    ai--;
    a[i] = ai;
  }
  ll startP = 0;
  ll cnt = 0;
  map<int, int> m;
  m[0] = 0;
  while (true) {
    startP = a[startP];
    cnt++;
    if (m.count(startP)) {
      break;
    } else {
      m[startP] = cnt;
    }

    if (cnt == k) {
      break;
    }
  }
  ll loopCnt = (k - m[startP]) % (cnt - m[startP]);
  ll ans = startP;
  while (loopCnt > 0) {
    ans = a[ans];
    loopCnt--;
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool is_loop[1000000] = {false};
int loop_nubmer = -1;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  vector<int> s;
  vector<int> ord(N + 1, -1);
  int c = 1;
  int l = 0;
  {
    int v = 1;
    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;
using ll = long long;
bool is_loop[1000000] = {false};
int loop_nubmer = -1;

int main() {
  ll N;
  ll K;
  cin >> N >> K;
  vector<int> G[N];

  is_loop[0] = true;

  for (int i = 0; i < N; i++) {
    int to;
    cin >> to;
    to--;
    if (is_loop[to]) {
      loop_nubmer = to;
    } else {
      is_loop[to] = true;
    }
    G[i].push_back(to);
  }

  // リストのサイズを取得
  int from = loop_nubmer;
  int size = 1;
  int count = 0;
  while (true) {
    if (count > 1000000) {
      size = 1;
      break;
    }
    from = G[from][0];
    if (from == loop_nubmer) {
      break;
    }
    size++;
  }

  // 1から移動する距離を計算
  ll move = K % size;
  from = 0;
  while (K > 0) {
    from = G[from][0];
    K--;
    if (from == loop_nubmer) {
      K = K % size;
    }
  }

  cout << from + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using vec_int = vector<int>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N + 1, 0);
  rep(i, N) { cin >> A.at(i + 1); }
  int pos = 1;
  vector<int> first_visit(N + 1, -1);
  vector<int> visit_N(N + 1, -1);

  int cycle;
  int flag = 0;
  rep(i, K + 1) {
    if (first_visit.at(pos) == -1) {
      visit_N.at(i) = pos;
      first_visit.at(pos) = i;
      pos = A.at(pos);
    } else {
      cycle = i - first_visit.at(pos);
      break;
    }
    if (i == K) {
      flag == 1;
      break;
    }
  }

  if (flag == 0) {
    int aaa = (K - first_visit.at(pos)) % cycle;
    cout << visit_N.at(aaa + first_visit.at(pos)) << endl;
  } else {
    cout << visit_N.at(K) << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using vec_int = vector<int>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using ll = unsigned long long;
#define rep(i, n)                                                              \\
  for (unsigned long long i = 0; i < (unsigned long long)(n); i++)

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1, 0);
  rep(i, N) { cin >> A.at(i + 1); }
  int pos = 1;
  vector<int> first_visit(N + 1, -1);
  vector<int> visit_N(N + 1, -1);

  int cycle;
  int flag = 0;
  rep(i, K + 1) {
    if (first_visit.at(pos) == -1) {
      visit_N.at(i) = pos;
      first_visit.at(pos) = i;
      pos = A.at(pos);
    } else {
      cycle = i - first_visit.at(pos);
      break;
    }
    if (i == K) {
      flag == 1;
      break;
    }
  }

  if (flag == 0) {
    int aaa = (K - first_visit.at(pos)) % cycle;
    cout << visit_N.at(aaa + first_visit.at(pos)) << endl;
  } else {
    cout << visit_N.at(K) << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200005;
ll k, n, a[maxn], pos = 1, cnt, res, vis[maxn];
vector<ll> ans;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  while (true) {
    if (vis[pos] == 2)
      break;
    if (!vis[pos]) {
      cnt++;
      if (k == cnt)
        return cout << a[pos], 0;
    } else {
      ans.push_back(pos);
      res++;
    }
    vis[pos]++;
    pos = a[pos];
  }
  cout << ans[(k - cnt + res) % res];
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
vector<ll> v;
ll n, k, a[maxn], vis[maxn], cnt1 = 0, cnt2 = 0;
int main() {
  cin >> n >> k;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  ll t = a[1];
  while (true) {
    if (vis[t] < 2) {
      if (vis[t] == 1)
        v.push_back(t);
      vis[t]++;
      t = a[t];
    } else
      break;
  }
  for (int i = 1; i <= n; i++) {
    // cout<<"i="<<i<<"ai="<<a[i]<<"vis[i]="<<vis[i]<<endl;
    if (vis[i] == 1)
      cnt1++;
    if (vis[i] == 2)
      cnt2++;
  }
  //	cout<<cnt1<<"  "<<cnt2<<endl;
  ll ans = ((k - cnt1) % cnt2 - 1 == -1 ? v[cnt2 - 1] : (k - cnt1) % cnt2 - 1);
  cout << v[ans] << endl;
}
/*
6 727202214173249351
6 5 2 5 3 2
*/
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;

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

  set<int> used;
  vector<int> path;
  int now = 0;
  while (used.count(now) == 0) {
    path.push_back(now);
    used.insert(now);
    now = A[now];
  }

  int initial = find(path.begin(), path.end(), now) - path.begin();
  int T = path.size() - initial;

  if (K < initial) {
    cout << path[K] + 1 << endl;
    return 0;
  } else {
    cout << path[(K - initial) % T + initial] + 1 << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> path;
  vector<int> ord(n, -1);
  int w = 1;
  int l = 0;
  {
    int v = 0;
    while (ord[v] == -1) {
      ord[v] = path.size();
      path.push_back(v);
      v = a[v];
    }
    w = path.size() - ord[v];
    l = ord[v];
  }
  if (l < k)
    cout << path[k] + 1 << endl;
  else
    cout << path[(k - l) % w + l] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  ll v[n + 1];

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

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

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

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

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

  k -= t - d;

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

int main() {

  fastcin solve();
  return 0;
}


Error: -11

Fixed submission:

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

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

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

  ll v[n + 1];

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

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

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

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

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

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

int main() {

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

#include <bits/stdc++.h>
using namespace std;
// int n;
unsigned long long n, k, a[200010], f[200010], m;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i] = -1;
  }
  int town = 1;
  f[1] = 0;
  while (m < k) {
    m++;
    town = a[town];
    if (f[town] != -1)
      break;

    f[town] = m;
  }
  if (m == k) {
    cout << town;
    exit(0);
  }
  unsigned long long x;
  x = (k - m) % (m - f[town]);
  for (int i = 1; i <= x; i++)
    town = a[town];

  cout << town;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// int n;
unsigned long long n, k, a[200001], b[200001], f[200001], m, p, step = 1;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int town = 1;
  while (m <= k) {
    m++;
    town = a[town];
    if (f[town])
      break;

    f[town] = m;
  }
  if (m == k) {
    cout << town;
    exit(0);
  }
  int x = (k - f[town]) % (m - f[town]);
  for (int i = 1; i <= x; i++)
    town = a[town];

  cout << town;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {

  int N;
  long long int K;
  vector<int> A;
  vector<int> next;
  vector<int> seen;
  int res;

  int roopLength = -1;
  int roopStartKaisuu;

  cin >> N >> K;

  next.resize(N);

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A.push_back(tmp);
    seen.push_back(-1);
  }

  int i = 0, count = 0;
  while (1) {
    count++;
    if (seen[A[i] - 1] == -1) {
      seen[A[i] - 1] = count;
    } else {
      roopStartKaisuu = seen[A[i] - 1];
      roopLength = count - seen[A[i] - 1];
      break;
    }
    i = A[i] - 1;
  }

  long long int kaisuu;

  if (K > roopStartKaisuu) {
    kaisuu = (K - roopStartKaisuu) % roopLength + roopStartKaisuu;
  } else {
    kaisuu = K;
  }

  int now = 1;
  for (int i = 0; i < kaisuu; i++) {
    now = A[now - 1];
  }

  res = now;

  cout << res;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {

  int N;
  long long int K;
  vector<int> A;
  vector<int> seen;
  int res;

  int roopLength = -1;
  int roopStartIndex;

  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A.push_back(tmp);
    seen.push_back(-1);
  }

  seen[0] = 0;

  for (int i = 0; i < N; i++) {
    if (seen[A[i] - 1] == -1) {
      seen[A[i] - 1] = i + 1;
    } else {
      roopStartIndex = seen[A[i] - 1] - 1;
      roopLength = i - seen[A[i] - 1];
    }
  }

  if (roopStartIndex > K) {
    res = A[K - 1];
  } else {
    res = A[roopStartIndex + (K - roopStartIndex) % roopLength - 1];
  }

  cout << res;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error



#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int BIG_NUM = 1e8;

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

  set<int> s;
  s.insert(0);
  vector<int> t;
  t.emplace_back(0);
  int next = a[0];
  while (s.find(next) == s.end()) {
    t.emplace_back(next);
    s.insert(next);
    next = a[next];
  }

  int loopBegin = 0;
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == next) {
      loopBegin = i;
      break;
    }
  }
  if (k >= loopBegin) {
    k -= loopBegin;
    int l = t.size() - loopBegin;
    k %= l;
    cout << t[k + loopBegin] + 1 << endl;
  } else {
    cout << t[k] + 1 << endl;
  }
}

Error: 0

Fixed submission:



#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int BIG_NUM = 1e8;

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

  set<int> s;
  s.insert(0);
  vector<int> t;
  t.emplace_back(0);
  int next = a[0];
  while (s.find(next) == s.end()) {
    t.emplace_back(next);
    s.insert(next);
    next = a[next];
  }

  int loopBegin = 0;
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == next) {
      loopBegin = i;
      break;
    }
  }
  if (k >= loopBegin) {
    k -= loopBegin;
    int l = t.size() - loopBegin;
    k %= l;
  }
  cout << t[k + loopBegin] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int64_t MOD = 1000000007;
const long long INF = 1LL << 60;

int main() {
  int64_t N, K;
  cin >> N >> K;
  vector<int64_t> A(N), S;
  rep(i, N) {
    cin >> A.at(i);
    A.at(i)--;
  }

  vector<bool> seen(N, true);
  int64_t k = 0, l = 0;

  while (seen.at(k)) {
    seen.at(k) = false;
    S.push_back(k);
    k = A.at(k);
  }
  while (S.at(l) != k)
    l++;
  if (K <= l)
    cout << S.at(K) + 1;
  else {
    K -= l;
    int64_t t = S.size() - l, q = K % t;
    cout << S.at(q + l) + 1;
  }
}


Error: WA

Fixed submission:

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

int main() {
  int64_t K;
  int N;
  cin >> N >> K;
  bool finished = true;
  vector<int> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S.at(i);
    S.at(i)--;
  }
  vector<int> A = {0};
  vector<bool> B(N, true);
  int J = 0, L;
  while (finished) {
    A.push_back(S.at(J));
    B.at(J) = false;
    J = S.at(J);
    if (B.at(S.at(J)) == false) {
      finished = false;
      L = S.at(J);
    }
  }
  A.pop_back();
  L++;
  int H = A.size();
  for (int i = 0; i < H; i++)
    A.at(i)++;
  int count = 0;
  for (int i = 0; i < H; i++) {
    if (A.at(i) == L)
      break;
    else
      count++;
  }
  vector<int> R = {};
  if (count < L) {

    for (int i = count + 1; i <= L; i++) {
      R.push_back(A.at(i));
      R.at(i - count - 1)++;
    }
  } else {
    for (int i = 0; i < L; i++) {
      R.push_back(A.at(i));
    }
  }
  K--;
  if (K <= count)
    cout << A.at(K);
  else {
    K -= count;
    int P = K % (R.size());
    cout << R.at(P);
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define mod 1e9 + 7;
#define INF 1e9 + 9;
#define ps(x, y) fixed << setprecision(y) << x
typedef long long ll;
typedef unsigned long long ull;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (ll i = 1; i < n + 1; i++)
    cin >> a[i];
  vector<ll> v;
  unordered_set<ll> us;
  v.push_back(1);
  us.insert(1);
  int i = 0, req;
  while (1) {
    // cout<<a[v[i]]<<"  ";
    if (us.count(a[v[i]]) == 0) {
      v.push_back(a[v[i]]);
      us.insert(a[v[i]]);
    } else {
      req = a[v[i]];
      break;
    }
    i++;
  }
  ll nn = v.size();
  ll bc, ac;
  for (int i = 0; i < nn; i++) {
    if (v[i] == req) {
      bc = i;
      ac = nn - i;
      break;
    }
  }
  // cout<<bc<<" "<<ac<<endl;
  if (k < bc) {
    cout << v[k] << endl;
  } else {
    k = k - bc;

    ll need = k % ac;
    cout << v[need + bc] << endl;
  }

  /*for(ll x :v)
          cout<<x<<"  ";
  cout<<endl;*/

  return 0;
}

Error: 0

Fixed submission:

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

#define mod 1e9 + 7;
#define INF 1e9 + 9;
#define ps(x, y) fixed << setprecision(y) << x
typedef long long ll;
typedef unsigned long long ull;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (ll i = 1; i < n + 1; i++)
    cin >> a[i];
  vector<ll> v;
  unordered_set<ll> us;
  v.push_back(1);
  us.insert(1);
  int i = 0, t = 10, req;
  while (t--) {
    // cout<<a[v[i]]<<"  ";
    if (us.count(a[v[i]]) == 0) {
      v.push_back(a[v[i]]);
      us.insert(a[v[i]]);
    } else {
      req = a[v[i]];
      break;
    }
    i++;
  }
  ll nn = v.size();
  ll bc, ac;
  for (int i = 0; i < nn; i++) {
    if (v[i] == req) {
      bc = i;
      ac = nn - i;
      break;
    }
  }
  // cout<<bc<<" "<<ac<<endl;
  if (k < bc) {
    cout << v[k] << endl;
  } else {
    k = k - bc;

    ll need = k % ac;
    cout << v[need + bc] << endl;
  }

  /*for(ll x :v)
          cout<<x<<"  ";
  cout<<endl;*/

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

#include <iostream>

using namespace std;

int main() {
  long long int town[300000], i, start, en, ans, n, k, check[300000], cnt;
  cin >> n >> k;
  cnt = 0;
  for (i = 1; i <= n; i++) {
    cin >> town[i];
    check[i] = 0;
  }
  ans = town[1];
  k = k - 1;
  start = 0;
  while (k--) {

    ans = town[ans];
    if (start == 1)
      cnt++;
    else if (check[ans] == 1 && start == 0) {
      start = 1;
    }
    if (check[ans] == 2 && start == 1) {
      en = cnt;
      start = 2;
      if (k > en) {
        k %= en;
      }
      // cout <<k <<endl;
    }

    check[ans]++;
  }

  // cout << en << endl;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  long long int town[100000], i, start, en, ans, n, k, check[100000], cnt;
  cin >> n >> k;
  cnt = 0;
  for (i = 1; i <= n; i++) {
    cin >> town[i];
    check[i] = 0;
  }
  ans = town[1];
  k = k - 1;
  start = 0;
  while (k--) {

    ans = town[ans];
    if (start == 1)
      cnt++;
    else if (check[ans] == 1 && start == 0) {
      start = 1;
    }
    if (check[ans] == 2 && start == 1) {
      en = cnt;
      start = 2;
      if (k > en) {
        k %= en;
      }
    }

    check[ans]++;
  }

  // cout << en << endl;
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

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

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

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

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

  cout << L[K] << endl;

  return 0;
}


Error: 0

Fixed submission:

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

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

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

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

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

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

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

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

  cout << L[K] << endl;

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

#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
vector<long long int> adj[200001];
bool vis[200001];
#define ull unsigned long long int
#define ll long long int
#define MAX 1000000007
// 1e9+7
#define pll pair<ll, ll>
#define pii pair<int, int>
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  ll binary_lift[n + 1][61];
  for (ll i = 1; i <= n; i++) {
    cin >> binary_lift[i][0];
    // binary_lift[i][0]--;
  }

  for (ll j = 1; j <= 60; j++) {
    for (ll i = 1; i <= n; i++) {
      binary_lift[i][j] = binary_lift[binary_lift[i][j - 1]][j - 1];
      // cout<<binary_lift[i][j]<<"\\n";
      //  cout<<i<<" "<<j<<" "<<binary_lift[i][j]<<"\\n";
    }
  }

  ll pos = 1;
  for (ll jump = 60; jump >= 0; jump--) {

    ll m = (1LL << jump);
    // n=(1 << jump);
    // cout<<m<<" "<<n<<"\\n";
    if (m <= k) {
      pos = binary_lift[pos][jump];
      k -= m;
    }
  }
  cout << pos;

  return (0);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
vector<long long int> adj[200001];
bool vis[200001];
#define ull unsigned long long int
#define ll long long int
#define MAX 1000000007
// 1e9+7
#define pll pair<ll, ll>
#define pii pair<int, int>
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  ll binary_lift[n][61];
  for (ll i = 0; i < n; i++) {
    cin >> binary_lift[i][0];
    binary_lift[i][0]--;
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = 1; j <= 60; j++)
      binary_lift[i][j] = binary_lift[binary_lift[i][j - 1]][j - 1];
  }
  ll pos = 0;
  for (ll jump = 60; jump >= 0; jump--) {
    ll m = (1 << jump);
    if (m <= k) {
      pos = binary_lift[pos][jump];
      k -= m;
    }
  }
  cout << pos + 1;
  return (0);
}