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

#include <bits/stdc++.h>
using namespace std;
using LL = int64_t;
using LLpair = pair<LL, LL>;

// #define DEBUG

int main() {
  LL n, k;
  cin >> n >> k;

  vector<LL> a1(n);
  for (LL i = 0; i < n; ++i) {
    cin >> a1[i];
    a1[i] -= 1;
  }

  vector<vector<LL>> a; // move 1, 2, 4, 8, ...
  a.push_back(a1);
  LL j;
  for (j = (LL)1; j <= k; j *= (LL)2) {
    if (j == (LL)1) {
      // Do nothing. just for counter check.
      continue;
    }
    vector<LL> aj(n);

    const auto &a_prev = a.back();
    for (LL i = 0; i < n; ++i) {
      aj[i] = a_prev[a_prev[i]];
    }

    a.push_back(aj);
  }
  j /= 2; // j may be reused.

  LL pos = 0;
  for (auto kk = (LL)0;; kk++) {
    if (k == 0)
      break;
    if (k % (LL)2 == 1) {
      const auto &aa = a[kk];
      pos = aa[pos];
    }
    k /= (LL)2;

#ifdef DEBUG
    cout << "pos: " << pos << endl;
    cout << "k: " << k << endl;
    cout << "kk: " << kk << endl;
    cout << "--\\n";
#endif
  }

  cout << pos + 1 << endl;

#ifdef DEBUG
  cout << "--\\n";
  cout << "j = " << j << "\\n";
  for (LL i = 0; i < n; ++i) {
    cout << a1.at(i) << "\\n";
  }
  cout << "--\\n";
  auto a2 = a.at(2);
  for (LL i = 0; i < n; ++i) {
    cout << a2.at(i) << "\\n";
  }
  cout << "--\\n";
  auto aj = a.back();
  for (LL i = 0; i < n; ++i) {
    cout << aj.at(i) << "\\n";
  }
#endif
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using LL = int64_t;
using LLpair = pair<LL, LL>;

// #define DEBUG

int main() {
  LL n, k;
  cin >> n >> k;

  vector<LL> a1(n);
  for (LL i = 0; i < n; ++i) {
    cin >> a1[i];
    a1[i] -= 1;
  }

  vector<vector<LL>> a; // move 1, 2, 4, 8, ...
  a.push_back(a1);
  LL j;
  for (j = (LL)1; j <= k; j *= (LL)2) {
    if (j == (LL)1) {
      // Do nothing. just for counter check.
      continue;
    }
    vector<LL> aj(n);

    for (LL i = 0; i < n; ++i) {
      auto a_prev = a.back();
      aj[i] = a_prev[a_prev[i]];
    }

    a.push_back(aj);
  }
  j /= 2; // j may be reused.

  LL pos = 0;
  for (auto kk = (LL)0;; kk++) {
    if (k == 0)
      break;
    if (k % (LL)2 == 1) {
      const auto &aa = a[kk];
      pos = aa[pos];
    }
    k /= (LL)2;

#ifdef DEBUG
    cout << "pos: " << pos << endl;
    cout << "k: " << k << endl;
    cout << "kk: " << kk << endl;
    cout << "--\\n";
#endif
  }

  cout << pos + 1 << endl;

#ifdef DEBUG
  cout << "--\\n";
  cout << "j = " << j << "\\n";
  for (LL i = 0; i < n; ++i) {
    cout << a1.at(i) << "\\n";
  }
  cout << "--\\n";
  auto a2 = a.at(2);
  for (LL i = 0; i < n; ++i) {
    cout << a2.at(i) << "\\n";
  }
  cout << "--\\n";
  auto aj = a.back();
  for (LL i = 0; i < n; ++i) {
    cout << aj.at(i) << "\\n";
  }
#endif
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

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

  vector<int> tele(n);
  for (int i = 0; i < n; i++) {
    cin >> tele[i];
    tele[i]--;
  }

  // 周期とoffsetを出す
  vector<int> visited(n, 0);
  int cnt = 0;
  int next_pos = 0;
  while (visited[next_pos] == 0) {
    cnt++;
    visited[next_pos] = cnt;
    next_pos = tele[next_pos];
    if (cnt == k) {
      cout << next_pos + 1 << endl;
      return 0;
    }
  }
  int offset = visited[next_pos] - 1;
  int syuuki = cnt - offset;

  int ans = (k - offset) % syuuki;

  for (int i = 0; i < ans; i++) {
    next_pos = tele[next_pos];
  }

  cout << next_pos + 1 << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  vector<int> tele(n);
  for (int i = 0; i < n; i++) {
    cin >> tele[i];
    tele[i]--;
  }

  vector<int> visited(n, 0);
  int next_pos = 0;
  int cnt = 0;
  while (visited[next_pos] == 0) {
    cnt++;
    visited[next_pos] = cnt;
    next_pos = tele[next_pos];
    if (cnt == k) {
      cout << next_pos + 1 << endl;
      return 0;
    }
  }

  int offset = visited[next_pos] - 1;
  int ans = (k - offset) % (cnt - visited[next_pos]);

  cout << cnt << endl;
  cout << offset << endl;

  for (int i = 0; i <= ans; i++) {
    next_pos = tele[next_pos];
  }

  cout << next_pos + 1 << endl;

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

#include <bits/stdc++.h>
using namespace std;
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;
}
const ll MOD = 1e9 + 7;
const ll INF = 1ll << 60;

//
// 　定数, 変数 *********************************************
int N;
ll K;

// *********************************************************
int main() {

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

  int ct = 1; // current town = 1
  int loop = 0;
  ll loopzan = 0;
  int loop1st = 0;
  for (int i = 1; i <= K; i++) {
    if (B[A[ct]] == 0) { // not visited before
      B[A[ct]] = i + 1;  // (i+1)th town
      ct = A[ct];
      if (i == K) {
        loop1st = ct;
      }
    } else {
      loop = B[ct] - B[A[ct]] + 1;
      loop1st = A[ct];
      loopzan = K - i;
      break;
    }
  }

  if (loop != 0) {
    loopzan = loopzan % loop;
    for (ll i = 1; i <= loopzan; i++) {
      loop1st = A[loop1st];
    }
  }

  cout << loop1st << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
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;
}
const ll MOD = 1e9 + 7;
const ll INF = 1ll << 60;

//
// 　定数, 変数 *********************************************
int N;
ll K;

// *********************************************************
int main() {

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

  int ct = 1; // current town = 1
  int loop = 0;
  ll loopzan = 0;
  int loop1st;
  for (int i = 1; i <= K; i++) {
    if (B[A[ct]] == 0) { // not visited before
      B[A[ct]] = i + 1;  // (i+1)th town
      ct = A[ct];
    } else {
      loop = B[ct] - B[A[ct]] + 1;
      loop1st = A[ct];
      loopzan = K - i;
      break;
    }
  }

  loopzan = loopzan % loop;
  for (ll i = 1; i <= loopzan; i++) {
    loop1st = A[loop1st];
  }

  cout << loop1st << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i], A[i]--;
  ll now = 0, remainK = K;
  vector<int> rireki(0);
  bool t = true; // falseならループ済
  map<int, int> back{};
  while (0 < remainK) {
    remainK--;
    now = A[now]; /*
     auto result = std::find(rireki.begin(), rireki.end(), now);
     if(result!=rireki.end()&&t){
       t = false;
       remainK %= (rireki.size() - distance(rireki.begin(), result));
     }else{
       rireki.push_back(now);
     }*/

    if (back[now] == 1 && t) {
      auto result = std::find(rireki.begin(), rireki.end(), now);
      t = false;
      remainK %= (rireki.size() - distance(rireki.begin(), result));
    } else {
      back[now] = 1;
      rireki.push_back(now);
    }
  }
  cout << now + 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i], A[i]--;
  ll now = 0, remainK = K;
  vector<int> rireki(0);
  bool t = true; // falseならループ済
  while (0 < remainK) {
    remainK--;
    now = A[now];
    auto result = std::find(rireki.begin(), rireki.end(), now);
    if (result != rireki.end() && t) {
      t == false;
      int a = distance(rireki.begin(), result);
      remainK %= (rireki.size() - distance(rireki.begin(), result));
    } else {
      rireki.push_back(now);
    }
  }
  cout << now + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: 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;
    }
    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;
}


Error: 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;
    }
    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, verdict: Runtime Error

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

typedef long long ll;

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<int> v(n + 5), vis(n + 5, -1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  int i, ans = 1, cl = 0;
  vis[1] = 0;
  for (i = 1; i <= k; i++) {
    ans = v[ans];
    if (vis[ans] != -1) {
      cl = i - vis[ans];
      break;
    } else {
      vis[ans] = i;
    }
  }
  // cout << ans << " " << i << " " << cl << '\\n';
  // starting at ans move k - i
  if (cl) {
    k = (k - i) % cl;
    while (k--)
      ans = v[ans];
  }
  cout << ans << '\\n';
}

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

  solve();
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<int> v(n + 5), vis(n + 5, -1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  int i, ans = 1, cl = 0;
  vis[1] = 0;
  for (i = 1; i <= k; i++) {
    ans = v[ans];
    if (vis[ans] != -1) {
      cl = i - vis[ans];
      break;
    } else {
      vis[ans] = i;
    }
  }
  // cout << ans << " " << i << " " << cl << '\\n';
  // starting at ans move k - i
  k = (k - i) % cl;
  while (k--)
    ans = v[ans];
  cout << ans << '\\n';
}

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

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

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

#define int long long
#define INT int
#define MOD 1000000007

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
template <typename T> void print(T x) { std::cout << x << '\\n'; }
typedef long long ll;

int dbl[63][200005];

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);

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

  rep(i, n) { dbl[1][i] = a[i]; }

  for (int i = 2; i <= 62; i++) {
    rep(j, n) { dbl[i][j] = dbl[i - 1][dbl[i - 1][j]]; }
    if ((1ll << i) > k)
      break;
  }
  vector<int> f1;
  int s = 0;
  rep(i, 61) {
    int t = 1ll << i;
    if (s > k)
      break;
    if (k & t) {
      s += t;
      f1.pb(i + 1);
    }
  }

  int result = 0;

  for (int p : f1) {
    result = dbl[p][result];
  }
  cout << result + 1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define int long long
#define INT int
#define MOD 1000000007

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
template <typename T> void print(T x) { std::cout << x << '\\n'; }
typedef long long ll;

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

  vector<map<int, int>> index(63);

  rep(i, n) {
    index[0][i] = i;
    index[1][i] = a[i];
  }

  for (int i = 2; i <= 62; i++) {
    rep(j, n) { index[i][j] = index[i - 1][index[i - 1][j]]; }
    if ((1ll << i) > k)
      break;
  }
  vector<int> f1;
  int s = 0;
  rep(i, 61) {
    if (s > k)
      break;
    if (k & (1ll << i)) {
      s += (1ll << i);
      f1.pb(i + 1);
    }
  }

  int result = 0;

  for (int p : f1) {
    result = index[p][result];
  }
  cout << result + 1 << endl;

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

/*#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

#include <boost/rational.hpp>
*/
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using ll = long long;
using ld = long double;
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define rep3(i, n) for (ll i = 1; i < (ll)(n + 1); i++)
#define rep4(i, s, n) for (ll i = (s); i < (ll)(n + 1); i++)
#define repr(i, n) for (ll i = (n - 1); i >= 0; i--)
#define repr3(i, n) for (ll i = (n); i > 0; i--)
#define repr4(i, s, n) for (ll i = (n); i >= (s); i--)
#define stlen(s) ll s.size() - 1
#define all(v) v.begin(), v.end()
#define cout(n) cout << std::fixed << std::setprecision(n)
using Graph = vector<vector<int>>;
using Graphw = vector<vector<pair<ll, ll>>>;
#define INF1 INT_MAX;
#define INF2 LLONG_MAX;
#define PI 3.14159265358979323846;
#define MOD 1000000007;
/*
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部長が32の浮動小数点数型
using Real32 = mp::number<mp::cpp_dec_float<32>>;
// 仮数部長が1024の浮動小数点数型
using Real1024 = mp::number<mp::cpp_dec_float<1024>>;
// 有理数型
using Rat = boost::rational<Bint>;
*/
const int mod = 1000000007;

int main() {
  // 頂点数と辺数
  ll N, K;
  cin >> N >> K;

  // グラフ入力受取 (ここでは無向グラフを想定)
  Graph G(N + 1);
  for (int i = 1; i <= N; ++i) {
    int A;
    cin >> A;
    G[i].push_back(A);
  }

  // BFS のためのデータ構造
  vector<int> dist(N + 1, -1); // 全頂点を「未訪問」に初期化
  map<int, int> dist2;
  queue<int> que;
  int c, r = -1;
  dist2[0] = 1;

  // 初期条件 (頂点 0 を初期ノードとする)
  dist[1] = 0;
  que.push(1); // 0 を橙色頂点にする

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    // v から辿れる頂点をすべて調べる
    for (int nv : G[v]) {
      if (dist[nv] != -1) {
        c = dist[v] - dist[nv] + 1;
        r = dist[nv];
        break;
      }; // すでに発見済みの頂点は探索しない

      // 新たな白色頂点 nv について距離情報を更新してキューに追加する
      dist[nv] = dist[v] + 1;
      dist2[dist[nv]] = nv;
      que.push(nv);
    }
  }
  if (r <= K) {
    cout << dist2[(K - r) % c + r] << endl;
  } else {
    cout << dist2[K] << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#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++)
using ll = long long;
#define all(v) v.begin(), v.end()
int main() {
  ll N, K, now = 1, count = 0;
  vector<ll> A(N + 1), rec(N + 1, 0);
  cin >> N >> K;
  rep(i, N) { cin >> A[i + 1]; }
  while (true) {
    if (rec[now] == 0) {
      rec[now] = count;
      count++;
      now = A[now];
    } else {
      K = K % (count - rec[now]) + rec[now];
      break;
    }
  }
  now = 1;
  while (K > 0) {
    K--;
    now = A[now];
  }
  cout << now << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
using namespace std;
typedef long long ll;
const int INF = 1e9;
const ll INFl = 1e18;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll tab[200005];
ll odl[200005];
bool vis[200005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> tab[i];
  }
  if (tab[1] == 1) {
    cout << 1 << '\\n';
    return 0;
  }
  if (k <= n) {
    int v = 1;
    while (k) {
      v = tab[v];
      k--;
    }
    cout << v << '\\n';
    return 0;
  }

  ll cnt = 1;
  ll now = tab[1];
  vis[1] = true;
  odl[1] = 0;
  if (now != 1)
    odl[now] = 1;
  ll x = 1;
  while (true) {
    cnt++;
    vis[now] = true;
    x = now;
    now = tab[now];
    if (vis[now])
      break;
    odl[now] = odl[x] + 1;
  }
  k -= odl[now];
  cnt -= odl[now];
  k %= cnt;
  while (k > 0) {
    now = tab[now];
    k--;
  }

  cout << now << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
using namespace std;
typedef long long ll;
const int INF = 1e9;
const ll INFl = 1e18;
typedef pair<int, int> pii;
typedef vector<int> vi;

// int pkt[10000];
ll tab[1000005];
ll odl[1000005];
bool vis[1000005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // int n, m, x; cin >> n >> m >> x;
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> tab[i];
    odl[i] = INF;
  }
  ll cnt = 1;
  ll now = tab[1];
  vis[1] = true;
  odl[1] = 0;
  odl[now] = 1;
  ll x = 1;
  while (!vis[now]) {
    vis[now] = true;
    x = now;
    now = tab[now];
    if (odl[now] == INF) {
      odl[now] = odl[x] + 1;
    }
    cnt++;
  }
  // cout << now << ' ' << x << ' ' << cnt << '\\n';
  // cout << "odl " << ' ' << odl[now] << '\\n';
  if (odl[now] > 0) {
    k -= odl[now] + 1;
    cnt -= odl[now] + 1;
  } else {
    k -= odl[now];
    cnt -= odl[now];
  }
  k %= cnt;
  // cout << k << '\\n';
  // k -= (abs(1 - now));
  while (k > 0) {
    now = tab[now];
    k--;
  }

  cout << now << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;

int main() {
  int n;
  long long k;

  cin >> n;
  cin >> 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, 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: -11

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; i++)
vector<ll> to[300005];
vector<ll> memo;
vector<ll> done;
ll d = 0;
ll dfs(ll u, ll v) {
  if (done[u] != 0) {
    d = v;
    return u; // 繰り返しのスタートがu;その時v回目の到着だった。
  } else {

    memo[u] = v;
    done[u] = 1;
    return dfs(to[u][0], v + 1);
  }
}
int main() {
  ll n, k;
  cin >> n >> k;
  memo.resize(n, 0);
  done.resize(n, 0);
  rep(i, n) {
    ll a = 0;
    cin >> a;
    a--;
    to[i].push_back(a);
  }
  ll b = 0;
  b = dfs(
      0,
      0); // それぞれの頂点に何回テレポートしたらいけるかを記録する。最終的に0に戻ってきたら終了
  ll c = 0;
  c = d - memo[b]; // これが周期。
  ll h = 0;
  if (k < memo[b]) {
    h = k;
  } else {
    h = (k - memo[b]) % c + memo[b];
  }
  rep(i, n) {
    if (memo[i] == h) {
      cout << i + 1 << endl;
      break;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
vector<ll> to[20005];
vector<ll> memo;
ll n, k;
ll d;
ll dfs(ll u, ll v) {
  if (memo[u] != 0) {
    d = v;
    return u; // 繰り返しのスタートがu;その時v回目の到着だった。
  } else {

    memo[u] = v; // uにはv回でこれる。
    /* for (int h : to[u])
{
cout << u << endl;
if (h > n - 1)
{
cout << h << endl;
continue;
}
dfs(h, v + 1);
}*/

    return dfs(to[u][0], v + 1);
  }
}
int main() {

  cin >> n >> k;
  memo.resize(n);
  rep(i, n) {
    ll a = 0;
    cin >> a;
    a--;
    to[i].push_back(a);
  }
  ll b = dfs(
      0,
      0); // それぞれの頂点に何回テレポートしたらいけるかを記録する。最終的に0に戻ってきたら終了
  priority_queue<ll> pq;

  ll c = d - memo[b]; // これが周期。
  ll h = (k - memo[b]) % c + memo[b];
  rep(i, n) {
    if (memo[i] == h) {
      cout << i + 1 << endl;
      break;
    }
  }
}