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

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

// #include <bits/stdc++.h>
using namespace std;
#define print(x) cout << (x) << endl
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long int
#define pb push_back

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> a(N + 1);
  a[0] = 0;

  rep(i, N) { cin >> a[i + 1]; }

  // 周回する回数を見る
  ll cnt = 0;
  ll t = a[1];
  vector<bool> x(N + 1, true);
  x[1] = false;
  while (1) {
    cnt++;
    K--;
    if (K == 0) {
      print(t);
      return 0;
    }
    if (x[t]) {
      x[t] = false;
      t = a[t];
    } else {
      break;
    }
  }

  // tをはじめとしてループを探す
  ll s = t;
  vector<ll> b;
  b.push_back(t);
  t = a[t];
  while (s != t) {
    b.push_back(t);
    t = a[t];
  }

  ll b_size = b.size();
  K = K + b_size;
  ll y = K % b_size; // 0からb_size-1までの値が出る
  print(b[y]);
}


Error: 0

Fixed submission:

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

// #include <bits/stdc++.h>
using namespace std;
#define print(x) cout << (x) << endl
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long int
#define pb push_back

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> a(N + 1);
  a[0] = 0;

  rep(i, N) { cin >> a[i + 1]; }

  // 周回する回数を見る
  ll cnt = 0;
  ll t = a[1];
  vector<bool> x(N + 1, true);
  x[1] = false;
  while (1) {
    cnt++;
    if (x[t]) {
      x[t] = false;
      t = a[t];
    } else {
      break;
    }
  }

  // tをはじめとしてループを探す
  ll s = t;
  vector<ll> b;
  b.push_back(t);
  t = a[t];
  while (s != t) {
    b.push_back(t);
    t = a[t];
  }
  ll b_size = b.size();

  K = K - cnt + b_size;
  ll y = K % b_size; // 0からb_size-1までの値が出る
  print(b[y]);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long int

int32_t main() {

  int n, k;
  cin >> n >> k;
  int arr[n + 2];
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  int tel = 1;
  map<int, int> m;
  int count = 0;
  while (k > 0 && m[tel] == 0) {
    m[tel] = ++count;
    tel = arr[tel - 1];
    k--;
  }
  if (k == 0) {
    cout << tel << endl;

    return 0;
  }
  k = k % (count + 1 - m[tel]);

  for (int i = 0; i < k; i++)
    tel = arr[tel - 1];

  cout << tel << endl;
  return 0;
}

Error: WA

Fixed submission:

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

#define int long long int

int32_t main() {

  int n, k;
  cin >> n >> k;
  int arr[n + 2];
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  int tel = 1;
  map<int, int> m;
  int count = 0;
  while (k > 0 && m[tel] == 0) {
    m[tel] = ++count;
    tel = arr[tel - 1];
    k--;
  }
  if (k == 0)
    cout << tel << endl;
  k = k % (count - m[tel]);

  for (int i = 0; i < k; i++)
    tel = arr[tel - 1];

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

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

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, deque<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<vector<int>> next(60, vector<int>(N));
  for (int v = 0; v < N; ++v)
    cin >> next[0][v], --next[0][v];
  for (int d = 0; d + 1 < 60; ++d) {
    for (int v = 0; v < N; ++v) {
      next[d + 1][v] = next[d][next[d][v]];
    }
  }
  int v = 0;
  for (int d = 0; d < 60; ++d) {
    if (K & (1LL << d))
      v = next[d][v];
  }
  cout << v + 1 << endl;
}

Error: 0

Fixed submission:

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

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, deque<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}

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

  deque<int> a; // 繰り返しの部分
  set<int> se;  // 一度通った頂点
  int cur = 0;
  while (true) {
    // 一度通った頂点を見つけたときの処理
    if (se.count(cur)) {
      while (a[0] != cur) {
        // 最初の余計な数手分を除去する
        --K;
        a.pop_front();

        // 繰り返す前に K が限界になったらリターン
        if (K == 0)
          return a[0] + 1;
      }
      break;
    }
    // 最初は愚直にシミュレーションしつつ、履歴をメモしていく
    a.push_back(cur);
    se.insert(cur);
    cur = A[cur];
  }
  cout << a[K % a.size()] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
map<ll, ll> mp;
vector<ll> used(200005, -1);
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    mp[i] = x;
  }
  ll s = min(n, k), p = 0, q = 1, saved = 0, d = 0;
  used[1] = 0;
  while (s--) {
    p++;
    if (used[mp[q]] != -1) {
      d = p - used[mp[q]];
      saved = mp[q];
      break;
    } else {
      q = mp[q];
      used[q] = p;
    }
  }
  //  cout << saved << " "<< d << " " << k << endl;
  if (saved == 0) {
    saved = q;
  }
  if (p < k) {
    k = k - p;
    k = k % d;
    while (k--) {
      saved = mp[saved];
    }
  }
  cout << saved << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
map<ll, ll> mp;
int used[200005];
int main() {
  memset(used, sizeof(used), 0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    mp[i] = x;
  }
  ll s = min(n, k), p = 0, q = 1, saved = 0, d = 0;
  used[1] = true;
  while (s--) {
    p++;
    if (used[mp[q]] != 0) {
      d = p - used[mp[p]];
      saved = mp[q];
      break;
    } else {
      q = mp[q];
      used[q] = p;
    }
  }
  if (saved == 0) {
    saved = q;
  }
  if (p < k) {
    k = k - p;
    k = k % d;
    while (k--) {
      saved = mp[saved];
    }
  }
  cout << saved << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  cin >> v;
  for (auto &e : v)
    e--;
  vector<ll> dist(n, -1);
  ll now = 0;
  for (ll i = 0; i < k; ++i) {
    if (dist[now] == -1)
      dist[now] = i;
    ll next = v[now];
    if (dist[next] != -1) {
      int p = i + 1 - dist[next];
      k = (k - dist[next]) % p + dist[next];
      break;
    }
    now = next;
  }
  now = 0;
  for (int i = 0; i < k; ++i) {
    now = v[now];
  }
  cout << now + 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> v[i];
  int cnt = 1, pos = 1;
  while (1) {
    int nxt = v[pos];
    if (nxt == 1)
      break;
    cnt++;
    pos = nxt;
  }
  k %= cnt;
  int ans = 1;
  for (int i = 0; i < k; ++i) {
    ans = v[ans];
  }
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define Sort(a) sort(a.begin(), a.end())
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) { cin >> a[i]; }
  ll cnt = 0;
  ll m = 0;
  vi d;
  vi f(200500, false);
  int tmp = 0;
  while (1) {
    if (f[a[m]]) {
      if (m != 0)
        rep(i, d.size()) {
          if (d[i] == a[m])
            tmp = i;
        }
      break;
    }
    f[a[m]] = true;
    cnt++;
    d.push_back(a[m]);
    // cout << d[cnt-1] << " ";
    m = a[m] - 1;
    if (k == cnt) {
      cout << d[k - 1] << endl;
      return 0;
    }
  }
  d.push_back(d.at(tmp));
  rep(i, d.size()) {
    // cout << d[i] << " ";
  }
  // cout << endl;
  // cout << cnt << " " << tmp << endl;
  ll ans = (k - tmp - 1) % (cnt - tmp);
  // cout << ans << "  " << tmp << endl;
  // cout << cnt << " " << k% cnt << endl;
  cout << d[ans + tmp] << endl;
  return 0;
}

Error: 0

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 REP(i, n) for (int i = 1; i < (int)(n); i++)
#define Sort(a) sort(a.begin(), a.end())
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) { cin >> a[i]; }
  ll cnt = 0;
  ll m = 0;
  vi d;
  vi f(n, false);
  int tmp = 0;
  while (1) {
    if (f[m]) {
      if (m != 0)
        rep(i, d.size()) {
          if (d[i] == m + 1)
            tmp = i;
        }
      break;
    }
    f[m] = true;
    cnt++;
    d.push_back(a[m]);
    // cout << d[cnt-1] << " ";
    m = a[m] - 1;
  }
  d.push_back(a[m]);
  // cout << d[cnt] << endl;
  ll ans = k % cnt;
  // cout << ans << "  " << tmp << endl;
  // cout << cnt << " " << k% cnt << endl;
  cout << d[ans + tmp - 1] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<bool> undone(N + 1, false);
  vector<ll> undonec(N + 1, 0);
  vector<ll> t(N + 1);
  vector<ll> ans;
  REP(i, N) {
    ll a;
    cin >> a;
    t[i] = a;
  }
  ll a = t[1];
  ll roopc = 0;
  undone[1] = true;
  ans.push_back(1);
  REP(i, N) {
    if (undone[a] != true) {
      undone[a] = true;
      roopc++;
      ans.push_back(a);
      undonec[a] = roopc;
      a = t[a];
    } else {
      roopc -= undonec[a];
      break;
    }
  }
  if (K <= undonec[a]) {
    cout << ans[K];
  } else {
    cout << ans[(K - undonec[a]) % (roopc + 1) + undonec[a]] << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<bool> undone(N + 1, false);
  vector<bool> undonec(N + 1, 0);
  vector<int> t(N + 1);
  vector<int> ans;
  REP(i, N) {
    int a;
    cin >> a;
    t[i] = a;
  }
  int a = t[1];
  ll roopc = 0;
  undone[1] = true;
  ans.push_back(1);
  REP(i, N) {
    if (undone[a] != true) {
      undone[a] = true;
      roopc++;
      undonec[a] = roopc;
      ans.push_back(a);
      a = t[a];
    } else {
      roopc -= undonec[a];
      break;
    }
  }
  if (undonec[a] == 0) {
    cout << ans[(K - undone[a]) % (roopc + 1) + undone[a]] << endl;
  } else {
    cout << ans[(K - undone[a]) % (roopc) + undone[a]] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long k, num = 1, i;
  int n, curr;
  cin >> n >> k;
  int a[n + 1];
  int st[n + 1];
  memset(st, 0, sizeof(st));
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  curr = a[1];
  st[1]++;
  vector<int> v;
  v.push_back(1);
  /*4 5
3 2 4 1*/
  for (i = 2; i <= k; i++) {
    num++;
    //	cout<<curr<<" "<<st[curr]<<endl;
    if (st[curr])
      break;
    v.push_back(curr);
    st[curr] = num;
    //		cout<<"num"<<num<<" curr"<<curr<<" a[curr]"<<a[curr]<<" "<<endl
    //;
    curr = a[curr];
  }
  if (i >= k) {
    cout << curr << endl;
    return 0;
  }
  long long l = st[curr] - 1, t = num - st[curr];
  //	cout<<l<<" "<<t<<endl;
  //	vector<int>::iterator it;
  /*	for(it=v.begin();it!=v.end();it++)
                  cout<<*it<<" ";*/
  cout << v[(k - l) % t + l] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long k, num = 1;
  int n, curr;
  cin >> n >> k;
  int a[n + 1];
  int st[n + 1];
  memset(st, 0, sizeof(st));
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  curr = a[1];
  st[1] = 1;
  vector<int> v;
  v.push_back(1);
  int i;
  for (i = 2; i <= k; i++) {
    if (st[curr]) {
      break;
    }
    num++;
    v.push_back(curr);
    st[curr] = num;
    curr = a[curr];
  }
  if (i > k) {
    cout << curr << endl;
    return 0;
  }
  long long l = st[curr] - 1, t = num - st[curr];
  if (l == 0)
    cout << v[(k - l) % t + 1] << endl;
  else
    cout << v[(k - l) % t + l - 1] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long k, num = 1;
  int n, curr;
  cin >> n >> k;
  int a[n + 1];
  int st[n + 1];
  memset(st, 0, sizeof(st));
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  curr = a[1];
  st[1] = 1;
  vector<int> v;
  v.push_back(1);
  /*4 5
3 2 4 1*/
  int i;
  for (i = 2; i <= k; i++) {
    //	cout<<"num="<<num<<" curr="<<curr<<" a[curr]="<<a[curr]<<"
    //"<<"st[curr]="<<st[curr]<<endl	;
    num++;
    if (st[curr])
      break;
    v.push_back(curr);
    st[curr] = num;
    // cout<<"num="<<num<<" curr="<<curr<<" a[curr]="<<a[curr]<<"
    // "<<"st[curr]="<<st[curr]<<endl	;
    curr = a[curr];
  }
  if (i > k) {
    cout << curr << endl;
    return 0;
  }
  long long l = st[curr], t = num - st[curr];
  // cout<<"l,t="<<l<<" "<<t<<endl;
  // vector<int>::iterator it;
  // for(it=v.begin();it!=v.end();it++)
  //	cout<<*it<<" ";
  // cout<<endl;
  long long j = ((k - l) + 1) % t + l - 1;
  if (k <= l)
    j = k;
  // cout<<j<<endl;
  cout << v[j] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long k, num = 1;
  int n, curr;
  cin >> n >> k;
  int a[n + 1];
  int st[n + 1];
  memset(st, 0, sizeof(st));
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  curr = a[1];
  st[1] = 1;
  vector<int> v;
  v.push_back(1);
  for (long long i = 2; i <= k; i++) {
    if (st[curr]) {
      break;
    }
    num++;
    v.push_back(curr);
    st[curr] = num;
    curr = a[curr];
  }
  long long l = st[curr] - 1, t = num - st[curr];
  if (l == 0)
    cout << v[(k - l) % t + 1] << endl;
  else
    cout << v[(k - l) % t + l - 1] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

  ll n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, 0, n) cin >> v[i];

  vector<int> nums(n + 1, -1);
  vector<int> finished(n + 1, -1);

  int num = 1, now = 1, d = 0;
  while (finished[now] == -1) {
    nums[num] = now;
    finished[now] = num;
    now = v[d];
    d = v[d] - 1;
    num++;
  }

  if (k < num - 1)
    cout << nums[k + 1] << endl;
  else {
    k -= finished[now] - 1;
    int c = num - finished[now];
    int t = k % c;
    cout << nums[finished[now] + t] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

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

  vector<int> keiro = {0};
  vector<bool> vis(n, false);
  vis[0] = true;
  int x = 0, start = -1;
  bool is_loop = false;
  while (keiro.size() <= n) {
    if (vis[a[x]]) {
      if (a[x] == 0)
        start = 0;
      else
        start = a[x] + 1;
      is_loop = true;
      break;
    }
    keiro.push_back(a[x]);
    vis[a[x]] = true;
    x = a[x];
  }

  if (is_loop) {
    vector<int> vl;
    rep(i, start, keiro.size()) vl.push_back(keiro[i]);
    int other = keiro.size() - vl.size() - 1;

    cout << keiro[start + (k - other - 1) % vl.size()] + 1 << endl;
  } else {
    cout << keiro[k - 1] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  long long MAX_N = 200002;
  long long n;
  long long k;
  cin >> n >> k;

  vector<pair<long long, long long>> count(MAX_N, pair<int, int>(0, 0));
  vector<long long> move(n);
  for (long long i = 0; i < n; i++) {
    long long to;
    cin >> to;
    to--;
    move[i] = to;
  }

  bool flag = 0;              // サイクルに入ったら旗をあげる
  long long repCount = 0;     // リープ一周の移動数
  long long countToStsrt = 0; // ループが始まるまでの移動回数

  int now = 0;
  while (k > 0) {
    count[now].first++;
    if (count[now].first == 1)
      count[now].second = k;
    if (count[now].first == 2 && flag == 0) {
      flag = 1;
      repCount = count[now].second - k;
      k = k % repCount;
      for (int i = 0; i < k; i++) {
        now = move[now];
      }
      cout << now + 1 << endl;
      break;
    }

    k--; // 移動回数を減らす
    now = move[now];
    if (k == 0)
      cout << now + 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> move(n);

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

  int now = 0;
  long long count = 0;
  vector<int> a(n);
  bool flag = 0;
  int countsStart = 0;        // 繰り返しが始まる場所
  long long countToStart = 0; // 繰り返しが始まる場所までの移動回数

  for (int i = 0; i < n; i++)
    a[i] = 0;
  for (int i = 0; i < k; i++) {
    if (i > 0 && a[now] == 1 && count == 0) {
      flag = 1;
      countsStart = now;
    } else if (a[now] == 2) {
      break;
    }

    if (flag == 1)
      count++;
    if (count == 0)
      countToStart++;

    a[now]++;
    now = move[now];
  }

  countToStart -= count;
  now = countsStart;
  if (countsStart != 0) {
    for (int i = 0; i < k % count - countToStart; i++) {
      now = move[now];
    }
  } else {
    for (int i = 0; i <= k % count; i++) {
      now = move[now];
    }
  }

  cout << now + 1 << endl;
}