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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <random>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
int main() {
  long long n, k, tmp;

  int cnt = 1;
  int move, begin = 0;
  cin >> n >> k;
  vector<int> a(1, 1);

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }

  vector<int> s;
  vector<int> ord(n + 1, -1);

  {
    int now = 1;

    while (ord[now] == -1) {
      ord[now] = s.size();
      s.push_back(now);
      now = a[now];
    }
    begin = ord[now];
    cnt = s.size() - ord[now];
  }

  if (k < begin) {
    cout << s[k];
  } else {
    move = (k - begin) % cnt;
    cout << s[move + begin];
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <random>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
int main() {
  long long n, k, tmp;
  vector<int> a, b, b_b;
  map<int, int> che;
  int now = 1;
  int cnt = 0;
  int move, begin = 0;
  cin >> n >> k;
  a = vector<int>(1, 1);
  vector<int> s, ord;
  ord = vector<int>(n + 1, -1);
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }

  for (;;) {
    now = a[now];
    if (ord[now] != -1) {
      begin = s.size();
      cnt = s.size() - ord[now];
      break;
    }
    s.push_back(now);
    ord[now] = s.size();
  }
  if (k < begin) {
    cout << s[k];
  } else {
    move = (k - begin) % cnt;
    cout << s[move + begin];
  }

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

#include <bits/stdc++.h>
#define INF 100000007
#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep2(i, j, k) for (ll i = j; i < k; i++)
#define chmax(x, y) x = max(x, y)
#define print(x) cout << x << "\\n"
#define printv(vec) rep(i, vec.size()) cout << vec[i] << " "
#define ALL(v) v.begin(), v.end()
#define SUM(v) accumulate(ALL(v), 0)
#define MAX(v) *max_element(ALL(v))
#define MIN(v) *min_element(ALL(v))
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
typedef long long ll;
using namespace std;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;

void solve() {

  ll N, K;
  ll ans = 0;
  cin >> N >> K;
  vvl nxt(60, vl(N));
  rep(i, N) cin >> nxt[0][i];
  rep(i, N) nxt[0][i]--;
  rep2(i, 1, 60) rep(j, N) nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
  rep(i, 60) if (K & (1LL << i)) ans = nxt[i][ans];
  print(ans + 1);
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 100000007
#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep2(i, j, k) for (ll i = j; i < k; i++)
#define chmax(x, y) x = max(x, y)
#define print(x) cout << x << "\\n"
#define printv(vec)                                                            \\
  rep(i, vec.size()) { cout << vec[i] << " "; }                                \\
  print("");
#define ALL(v) v.begin(), v.end()
#define SUM(v) accumulate(ALL(v), 0)
#define MAX(v) *max_element(ALL(v))
#define MIN(v) *min_element(ALL(v))
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
typedef long long ll;
using namespace std;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;

void solve() {

  ll N, K, ans, mod, cnt, nx, loop_cnt;
  cin >> N >> K;
  vl A(N);
  rep(i, N) cin >> A[i];
  rep(i, N) A[i] -= 1;

  vl visited(N, -1);
  vl v(0);
  cnt = 0;
  nx = 0;

  while (visited[nx] < 0) {
    v.push_back(nx);
    visited[nx] = cnt;
    nx = A[nx];
    cnt += 1;
  }

  loop_cnt = cnt - visited[nx];
  mod = (K - cnt) % loop_cnt;
  ans = v[visited[nx] + mod];
  print(ans + 1);
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();

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

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

typedef long long ll;

int n;
ll k;

int main() {
  cin >> n >> k;
  vector<int> a(n + 1);
  vector<int> mp(n + 1);
  vector<int> mp1(n + 1);
  vector<int> vis(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int cur = 1;
  int h = 0;
  int cnt = 0;
  while (vis[cur] == 0) {
    vis[cur] = 1;
    cnt++;
    mp1[cnt] = cur;
    cur = a[cur];
    h++;
  }
  fill(vis.begin(), vis.end(), 0);
  cnt = 0;
  while (vis[cur] == 0) {
    vis[cur] = 1;
    cnt++;
    mp[cnt] = cur;
    cur = a[cur];
  }
  mp[0] = mp[cnt];
  int ans = k + 1 > h ? mp[(k - h + 1) % cnt] : mp1[k + 1];
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

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

typedef long long ll;

int n;
ll k;

int main() {
  cin >> n >> k;
  vector<int> a(n + 1);
  vector<int> mp(n + 1);
  vector<int> vis(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int cur = 1;
  int h = 0;
  while (vis[cur] == 0) {
    vis[cur] = 1;
    cur = a[cur];
    h++;
  }
  fill(vis.begin(), vis.end(), 0);
  int cnt = 0;
  while (vis[cur] == 0) {
    vis[cur] = 1;
    cnt++;
    mp[cnt] = cur;
    cur = a[cur];
  }
  mp[0] = mp[cnt];
  int ans = mp[(k - h + 1) % cnt];
  cout << ans;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vvl = vector<vector<ll>>;
using vl = vector<ll>;
#define _GLIBCXX_DEBUG
#define acc_io                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define all(x) x.begin(), x.end()
#define rep(i, sta, end) for (ll i = sta; i < end; i++)
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
const ll MOD = 1000000007;
const double PI = acos(-1);
// name[i]   -> vl  name(i)
// name[i][j]-> vvl name(i,vl(j))
// or-> ||
// map<int,int>mp

ll n, k;
ll ans;
// string s;
signed main() {
  acc_io;
  ans = 0;
  cin >> n >> k;
  int a[200010];
  int pass[200010] = {0};
  rep(i, 1, n + 1) cin >> a[i];
  ll cyc = 1;
  ll sta = 0;
  map<int, int> mp;
  int next = 1;
  pass[1] = 1;
  while (1) {
    if (pass[a[next]]) {
      sta = pass[a[next]] - 1;
      cyc = cyc - pass[a[next]] + 1;
      break;
    } else {
      cyc++;
      pass[a[next]] = cyc;
      next = a[next];
    }
  }
  //	cout<<sta<<" "<<cyc<<" "<<endl;
  if (k > (sta + cyc)) {
    k = (k - sta) % cyc;
    k += sta;
    k += cyc;
  }
  //	cout<<k<<endl;
  int j = 1;
  rep(i, 0, k) {
    next = a[j];
    j = next;
    //		cout<<j<<" "<<next<<endl;
  }
  cout << next << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vvl = vector<vector<ll>>;
using vl = vector<ll>;
#define _GLIBCXX_DEBUG
#define acc_io                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define all(x) x.begin(), x.end()
#define rep(i, sta, end) for (ll i = sta; i < end; i++)
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
const ll MOD = 1000000007;
const double PI = acos(-1);
// name[i]   -> vl  name(i)
// name[i][j]-> vvl name(i,vl(j))
// or-> ||
// map<int,int>mp

// ll n,k;
ll n;
ll ans;
// string s;
signed main() {
  acc_io;
  ans = 0;
  unsigned long k;
  cin >> n >> k;
  int a[200000];
  rep(i, 0, n) cin >> a[i];
  ll cyc = 0;
  rep(i, 0, n) {
    if (a[i] == 1)
      break;
    cyc++;
  }
  k %= cyc;
  int next = 0;
  int j = 0;
  rep(i, 0, k) {
    next = a[j];
    j = next - 1;
  }
  cout << next << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n);
  rep(i, n) cin >> A.at(i);
  rep(i, n) A.at(i)--;
  map<ll, ll> where;
  ll now = 0;   // 今いる場所
  ll count = 0; // カウント
  ll two = 0;   // 二週目に入ったときの回数
  ll three = 0; // 三週目に入ったときの回数
  where[now] = 1;
  bool flag = true;
  while (1) {
    if (count == k) {
      cout << now + 1 << endl;
      return 0;
    } // 移動回数が十分小さい場合には，この段階で出力する．
    now = A[now];
    where[now]++;
    count++;
    if (where[now] == 2 & flag) {
      two = count;
      flag = false;
    }
    if (where[now] == 3) {
      three = count;
      break;
    }
  }
  ll amari = (k - two) % (three - two);
  // 周期移動の開始時点から周期移動を考慮すること
  rep(i, amari) { now = A[now]; }
  cout << now + 1 << endl;
}


Error: 0

Fixed submission:

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

vector<ll> G[200005];
ll ans, counter;
ll n, k;
void dfs(ll i, ll j) {
  if (i == 0 && j != 0) {
    return;
  } else
    ans++;
  for (auto p : G[i])
    dfs(p, j + 1);
}

void dfs2(ll i, ll j, ll b) {
  if (j == b) {
    return;
  } else
    counter = i + 1;
  for (auto p : G[i])
    dfs2(p, j + 1, b);
}

int main() {
  cin >> n >> k;
  rep(i, n) {
    int a;
    cin >> a;
    a--;
    G[i].push_back(a);
  }
  ans = 0;
  dfs(0, 0);
  counter = 0;
  ll a = k % ans;
  dfs2(0, 0, a);
  cout << counter + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<vector<int>> t(60, vector<int>(N));
  for (int i = 0; i < N; i++)
    cin >> t[0][i], t[0][i]--;
  for (int k = 0; k < 59; k++)
    for (int i = 0; i < N; i++) {
      t[k + 1][i] = t[k][t[k][i]];
    }
  int ans = 0;
  for (ll i = 59; i >= 0; i--) {
    if (K & (1ll << i)) {
      ans = t[i][ans];
    }
  }
  cout << ans + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> A;
vector<unordered_map<ll, int>> mp;

int teleport(ll k, int x) {
  if (k == 0)
    return x;
  int f = mp[x].find(k / 2) == mp[x].end() ? mp[x][k / 2] = teleport(k / 2, x)
                                           : mp[x][k / 2];
  if (k % 2)
    return (mp[A[f]].find(k / 2) == mp[A[f]].end()
                ? mp[A[f]][k / 2] = teleport(k / 2, A[f])
                : mp[A[f]][k / 2]);
  else
    return (mp[f].find(k / 2) == mp[f].end() ? mp[f][k / 2] = teleport(k / 2, f)
                                             : mp[f][k / 2]);
}

int main() {
  ll N, K;
  cin >> N >> K;
  A.resize(N);
  mp.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A[i], A[i]--;
  cout << teleport(K, 0) + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

signed main() {
  int N, K;
  int A[200010] = {0};
  int An[200010] = {0};
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  int i = 1;
  int n = 1;
  K--;
  while (K >= 0) {
    if (An[n] != 0) {
      int diff = i - An[n];
      K %= diff;
    }
    An[n] = i;
    i++;
    K--;
    n = A[n];
  }

  cout << n << endl;
}


Error: 0

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

signed main() {
  int N, K;
  int A[20000] = {0};
  int An[20000] = {0};
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  int i = 1;
  int n = 1;
  K--;
  while (K >= 0) {
    if (An[n] != 0) {
      int diff = i - An[n];
      K %= diff;
    }
    An[n] = i;
    i++;
    K--;
    n = A[n];
  }

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

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <vector>

using namespace std;

#define LEN 200003

long long A[LEN];
long long P[LEN];
bool V[LEN];
int cycle_start = -1;
int ans = -1;

int visit(int node, int p, vector<int> &nodes, long long K, long long jumped) {

  if (K == jumped)
    ans = node;

  if (V[node]) {
    cycle_start = node;
    return 1;
  }

  P[node] = p;
  V[node] = true;
  nodes.push_back(node);
  return 1 + visit(A[node], node, nodes, K, jumped + 1);
}

int main() {

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

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    V[i] = false;
    P[i] = -1;
  }

  vector<int> nodes;
  long long before_cycle = visit(1, -1, nodes, K, 0) - 1;

  for (int i = 1; i <= N; i++) {
    V[i] = false;
    P[i] = -1;
  }
  nodes.clear();

  if (before_cycle <= K) {
    long long cycle_length = visit(cycle_start, -1, nodes, K, 0) - 1;
    long long pos = (K - before_cycle) % (cycle_length);
    assert(nodes.size() == cycle_length);
    cout << nodes[pos] << endl;
  } else {
    visit(1, -1, nodes, K, 0);
    assert(ans != -1);
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <vector>

using namespace std;

#define LEN 200003

long long A[LEN];
long long P[LEN];
bool V[LEN];
int cycle_start = -1;

int visit(int node, int p, vector<int> &nodes) {
  if (V[node]) {
    cycle_start = node;
    return 1;
  }

  P[node] = p;
  V[node] = true;
  nodes.push_back(node);
  return 1 + visit(A[node], node, nodes);
}

int main() {

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

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    V[i] = false;
    P[i] = -1;
  }

  vector<int> nodes;
  long long before_cycle = visit(1, -1, nodes) - 1;

  for (int i = 1; i <= N; i++) {
    V[i] = false;
    P[i] = -1;
  }
  nodes.clear();

  long long cycle_length = visit(cycle_start, -1, nodes) - 1;

  long long pos = (K - before_cycle) % (cycle_length);

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

  assert(nodes.size() == cycle_length);

  cout << nodes[pos] << endl;

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

#include <bits/stdc++.h>
using namespace std;
long long dp[60][200005];
int main() {
  long long n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> dp[0][i];
    dp[0][i]--;
  }
  for (int i = 0; i < 59; i++) {
    for (int j = 0; j < n; j++) {
      dp[i + 1][j] = dp[i][dp[i][j]];
    }
  }
  long long ans = 0;
  for (int i = 59; i >= 0; i--) {
    long long l = 1ll << i;
    if (l <= k) {
      k -= l;
      ans = dp[i][ans];
    }
  }
  cout << ans + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long dp[60][200005];
int main() {
  long long n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> dp[0][i];
    dp[0][i]--;
  }
  for (int i = 0; i < 60; i++) {
    for (int j = 0; j < n; j++) {
      dp[i + 1][j] = dp[i][dp[i][j]];
    }
  }
  long long ans = 0;
  for (int i = 59; i >= 0; i--) {
    long long l = 1ll << i;
    if (l <= k) {
      k -= l;
      ans = dp[i][ans];
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int> tp(N + 1);
  tp[0] = 1;

  vector<int> visited(N + 1, -1);
  visited[1] = 0;

  ll cycle = 0;
  ll first = 0;
  for (ll i = 1; i <= N; i++) {
    tp[i] = A[tp[i - 1]];
    if (visited[tp[i]] != -1) {
      first = visited[tp[i]];
      cycle = i - visited[tp[i]];
      break;
    } else {
      visited[tp[i]] = i;
    }
  }
  ll n;
  if (K > first)
    n = ((K - first) % cycle) + first;
  else
    n = K;
  cout << tp[n] << endl;
}


Error: WA

Fixed submission:

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

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

  vector<int> tp(N + 1);
  tp[0] = 1;

  vector<int> visited(N + 1, -1);
  visited[1] = 0;

  ll cycle = 0;
  ll first = 0;
  for (ll i = 1; i <= N; i++) {
    tp[i] = A[tp[i - 1]];
    if (visited[tp[i]] != -1) {
      first = visited[tp[i]];
      cycle = i - visited[tp[i]];
      break;
    } else {
      visited[tp[i]] = i;
    }
  }
  ll n;
  if (K > first)
    n = ((K - first) % cycle) + first;
  else
    n = K;
  cout << tp[K] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1001001001;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) 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: TLE

Fixed submission:

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

const int INF = 1001001001;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    int x;
    cin >> x;
    x--;
    a[i] = x;
  }
  vector<int> loop;
  int now = 0;
  do {
    loop.push_back(now);
    now = a[now];
  } while (now != 0);
  int x = k % (loop.size());
  int ans = loop[x] + 1;
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;

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

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c = 0, 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 <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;

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

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c = 0, 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 <stdio.h>
#define MAXN 200005
int t[MAXN];
int u[MAXN];
int r[MAXN];
int main() {
  int a[MAXN], n, cnt = 0, p, m, total = 0;
  long long k;
  scanf("%d %lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  p = 1;
  u[1] = 1;
  t[0] = 1;
  do {
    cnt++;
    p = a[p];
    t[cnt] = p;
    if (u[p] == 0) {
      u[p] = 1;
    } else
      break;
  } while (true);
  for (int i = 0; i < cnt; i++) {
    if (t[i] == p) {
      m = i;
      break;
    }
  }
  if (k < m) {
    printf("%d\\n", t[k]);
  } else {
    k = k - m;
    for (int i = m; i < cnt; i++) {
      r[total++] = t[i];
    }
    k = k % total;
    printf("%d", r[k]);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
#define MAXN 200005
int t[MAXN];
int u[MAXN];
int r[MAXN];
int main() {
  int a[MAXN], n, cnt = 0, p, m, total = 0;
  long long k;
  scanf("%d %lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  p = 1;
  u[1] = 1;
  do {
    cnt++;
    p = a[p];
    t[cnt] = p;
    if (u[p] == 0) {
      u[p] = 1;
    } else
      break;
  } while (true);
  for (int i = 1; i < cnt; i++) {
    if (t[i] == p) {
      m = i;
    }
  }
  k = k - m;
  for (int i = m; i < cnt; i++) {
    r[total++] = t[i];
  }
  k = k % total;
  printf("%d", r[k]);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

const int N = 2e5 + 50;
typedef long long ll;

ll n;
ll k;
ll a[N], b[N];  // b[N]为模拟一个循环数组
map<ll, ll> mp; // 用于查找循环节

int main() {
  //	freopen("input.txt","r",stdin);

  scanf("%lld%lld", &n, &k);
  for (ll i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  ll loc = 1; // loc为当前国王所处位置
  b[1] = 1;
  for (ll i = 1; i <= n + 10; i++) // 寻找r
  {
    loc = a[loc];
    b[i + 1] = loc;
  }
  ll pre, l, r; // pre为前缀，l、r为第一个循环节的左右下标
  for (ll i = 1; i <= n + 10; i++) // 确认r的位置
  {
    mp[b[i]]++;
    if (mp[b[i]] > 1) {
      r = i - 1;
      break;
    }
  }
  for (ll i = 1; i <= n + 10; i++) {
    if (b[i] == b[r + 1]) {
      l = i;
      break;
    }
  }
  //	for(ll i=1;i<=n+10;i++) cout<<b[i]<<" "<<endl;
  pre = l - 1;
  ll c = r - l + 1;
  if (k <= pre - 1)
    cout << b[k + 1];
  else {
    k -= pre;
    k %= c;
    cout << b[l + k];
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

const int N = 2e5 + 10;
typedef long long ll;

int n;
ll k;
int a[N], b[N];   // b[N]为模拟一个循环数组
map<int, int> mp; // 用于查找循环节

int main() {
  //	freopen("input.txt","r",stdin);

  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int loc = 1; // loc为当前国王所处位置
  b[1] = 1;
  for (int i = 1; i <= n + 10; i++) // 寻找r
  {
    loc = a[loc];
    b[i + 1] = loc;
  }
  //	for(int i=1;i<=n+10;i++) cout<<b[i]<<endl;
  int pre, l, r; // pre为前缀，l、r为第一个循环节的左右下标
  for (int i = 1; i <= n + 10; i++) // 确认r的位置
  {
    mp[b[i]]++;
    if (mp[b[i]] > 1) {
      r = i - 1;
      break;
    }
  }
  for (int i = 1; i <= n + 10; i++) {
    if (b[i] == b[r + 1]) {
      l = i;
      break;
    }
  }
  pre = l - 1;
  //	cout<<pre<<" "<<l<<" "<<r<<endl;
  int c = r - l + 1;
  int mod = (k - pre) % c;
  //	cout<<mod;
  cout << a[l + mod];

  return 0;
}