alexjercan/bugnet huggingface dataset:
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>;

const int INF = 100100100;

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; // cは周期、lは周期じゃない部分の長さ
  {
    int v = 1;             // 街の番号
    while (ord[v] == -1) { // 訪れてないなら回す
      ord[v] = s.size();
      s.push_back(v);
      v = a[v - 1];
    }
    c = s.size() - ord[v]; // ord[v]に入れようとした数-ord[v]
    l = ord[v];
  }

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l; // 周期じゃない部分の長さを引くのを忘れるな
    k %= c;
    cout << s[l + k] << endl; // l+kにするのを忘れるな
  }
  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60; // intじゃ扱えないことに注意！
#define mod 1000000007
#define rep(i, n)                                                              \\
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1);
  vector<bool> seen(N + 1, false);
  vector<ll> step(N + 1);
  step[1] = 0;
  ll roop;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<ll> arrive(K + 1);
  arrive[0] = 1;
  for (int i = 1; i <= K; i++) {
    arrive[i] = A[arrive[i - 1]];

    if (seen[arrive[i]] == true) {
      roop = step[arrive[i - 1]] + 1 - step[arrive[i]];
      K = (K - step[arrive[i]]) % roop;
      break;
    } else {
      seen[arrive[i]] = true;
      step[arrive[i]] = step[arrive[i - 1]]++;
    }
  }

  cout << arrive[K + 1] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

signed main() {
  lint n, k;
  cin >> n >> k;
  vector<lint> a(200000);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  lint loop = 0;
  lint curr = 0;
  lint interval = 0;
  lint isPast[200000] = {-1};
  for (lint i = 0; i < 200000; i++) {
    isPast[i] = -1;
  }
  lint looped = 0;
  isPast[0] = 0;
  do {
    curr = a[curr] - 1;
    looped++;
    if (isPast[curr] != -1) {
      interval = isPast[curr];
      loop = looped - interval;
      break;
    }
    isPast[curr] = looped;
    // looped++;
  } while (true);

  curr = 0;
  lint remain = 0;
  if (k > interval) {
    remain = (k - interval) % loop + interval;
  } else {
    remain = k;
  }
  for (lint i = 0; i < remain; i++) {
    curr = a[curr] - 1;
  }
  cout << curr + 1 << endl;
}

Error: TLE

Fixed submission:

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

signed main() {
  long long n, k;
  cin >> n >> k;
  vector<long> a(200000);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  long long loop = 0;
  int curr = 0;
  vector<int> past;
  past.push_back(0);
  bool isBreak = false;
  long long interval = 0;
  do {
    curr = a[curr] - 1;
    for (int i = 0; i < past.size(); i++) {
      if (curr == past[i]) {
        isBreak = true;
        interval = i;
        loop = past.size() - i;
        break;
      }
    }
    if (isBreak)
      break;
    past.push_back(curr);
  } while (true);

  curr = 0;
  long long remain = 0;
  if (k > interval) {
    remain = (k - interval) % loop + interval;
  } else {
    remain = k;
  }
  for (long long i = 0; i < remain; i++) {
    curr = a[curr] - 1;
  }
  cout << curr + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define INF 1000000000000000005

int main() {
  long long n, k;
  long long a[200005];

  cin >> n >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }

  if (k <= n) {
    long long dp1[200005];
    dp1[0] = 1;
    for (long long i = 1; i <= k; i++) {
      dp1[i] = a[dp1[i - 1]];
    }
    cout << dp1[k] << endl;
  }

  else {
    long long end = 0;
    long long sta;
    long long cycle;
    long long res;
    bool check[200005] = {0};
    long long dp2[200005];
    dp2[0] = 1;
    check[1] = true;
    for (long long i = 1; i <= n; i++) {
      dp2[i] = a[dp2[i - 1]];
      if (check[dp2[i]] == true) {
        end = i;
        break;
      }
      check[dp2[i]] = true;
    }

    for (int i = 0;; i++) {
      if (dp2[i] == dp2[end]) {
        sta = i;
        break;
      }
    }

    cycle = end - sta;
    if (cycle == 0) {
      cout << dp2[sta] << endl;
    } else {
      res = (k - sta) % cycle;
      cout << dp2[sta + res] << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define INF 1000000000000000005

int main() {
  long long n, k;
  long long a[200005];

  cin >> n >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }

  set<long long> s;
  long long dp[200000];
  dp[0] = 1;
  s.insert(1);
  long long sta = -1;
  long long div;
  long long i = 1;
  while (1) {
    dp[i] = a[dp[i - 1]];
    if (s.find(dp[i]) != s.end()) {
      sta = dp[i];
      div = i;
      break;
    }
    s.insert(dp[i]);
    i++;
  }

  long long en;
  long long j = 1;
  while (1) {
    if (dp[j] == sta) {
      en = j;
      break;
    }
    j++;
  }

  if (k > n && div - en != 0) {
    long long cycle = div - en;
    long long re = k - en;
    long long res = re % cycle;

    cout << dp[en + res] << endl;
  } else {
    dp[0] = 1;
    for (int i = 1; i <= k; i++) {
      dp[i] = a[dp[i - 1]];
    }
    cout << dp[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;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int start, loop;
  vector<int> x;
  set<int> se;
  int now = 0;
  while (1) {
    x.push_back(now);
    if (se.find(now) != se.end())
      break;
    se.insert(now);
    now = a[now];
  }
  int m = x.size();
  rep(i, m - 1) {
    if (x[i] == x[m - 1])
      start = i, loop = m - 1 - i;
  }
  if (k < start)
    cout << x[k] + 1 << endl;
  else
    cout << x[start + (k - start) % loop] + 1 << endl;
}

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;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int start, loop;
  vector<int> x;
  int now = 0;
  while (1) {
    x.push_back(now);
    if (count(x.begin(), x.end(), now) >= 2)
      break;
    now = a[now];
  }
  int m = x.size();
  rep(i, m - 1) {
    if (x[i] == x[m - 1])
      start = i, loop = m - 1 - i;
  }
  if (k < start)
    cout << x[k] + 1 << endl;
  else
    cout << x[start + (k - start) % loop] + 1 << 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;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int now = a[0];
  vector<int> ans;
  set<int> se;
  ans.push_back(0);
  se.insert(0);
  ans.push_back(now);
  se.insert(now);
  while (1) {
    now = a[now];
    ans.push_back(now);
    if (se.find(now) != se.end())
      break;
    se.insert(now);
  }
  int start, loop, as = ans.size();
  rep(i, as - 1) {
    if (ans[i] == ans[as - 1]) {
      start = i;
      loop = as - 1 - i;
    }
  }
  if (k <= start)
    cout << ans[k] + 1 << endl;
  else {
    k -= start;
    cout << ans[start + k % loop] + 1 << 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;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int now = a[0];
  vector<int> ans;
  ans.push_back(0);
  ans.push_back(now);
  while (1) {
    now = a[now];
    ans.push_back(now);
    if (count(ans.begin(), ans.end(), now) == 2)
      break;
  }
  int start, loop, as = ans.size();
  rep(i, as - 1) {
    if (ans[i] == ans[as - 1]) {
      start = i;
      loop = as - 1 - i;
    }
  }
  if (k <= start)
    cout << ans[k] + 1 << endl;
  else {
    k -= start;
    cout << ans[start + k % loop] + 1 << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int n;
ll k;
vector<int> a, b, c;

int main() {
  cin >> n >> k;
  a.resize(n);
  c.resize(n, -1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  b.push_back(0);
  int p = 0;
  ll i = 0;
  while (true) {
    i++;
    if (i == k) {
      cout << a[p] + 1;
      return 0;
    }

    if (c[p] >= 0) {
      vector<int>::iterator it = find(b.begin(), b.end(), a[p]);
      int pos = it - b.begin();
      int m = (k - pos) % (b.size() - pos);
      cout << b[pos + m] + 1;
      return 0;
    }

    c[p] = a[p];
    b.push_back(a[p]);
    p = a[p];
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int n;
ll k;
vector<int> a, b;

int main() {
  cin >> n >> k;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  b.push_back(0);
  int p = 0;
  vector<int>::iterator it;
  for (ll i = 0; i < k; i++) {
    p = a[p];
    it = find(b.begin(), b.end(), p);
    if (it != b.end())
      break;
    b.push_back(p);
  }
  if (it != b.end()) {
    int pos = it - b.begin();
    int q = (k - pos) % (b.size() - pos);
    cout << b[pos + q] + 1;
  } else
    cout << b.back() + 1;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
const ll INF = 1LL << 60;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> a(N);
  rep(i, N) {
    int num;
    cin >> num;
    num--;
    a[i] = num;
  }
  // ループの始まりを見つける
  map<int, int> m;
  int maxnum = 0;
  int nowpos = 0;
  rep(i, 500000) {
    m[nowpos]++;
    maxnum = max(m[nowpos], maxnum);
    nowpos = a[nowpos];
  }
  int s_index = 0;
  for (auto x : m) {
    if (maxnum == x.second) {
      s_index = x.first;
      break;
    }
  }
  nowpos = 0;
  int f = -1, loop = 0;
  rep(i, N * 10) { // 1ループいくつか測定
    if (f != -1 && nowpos == s_index) {
      loop = i - f;
      break;
    } else if (f == -1 && nowpos == s_index) {
      f = i;
    }
    nowpos = a[nowpos];
    if (K == i + 1) {
      cout << nowpos + 1 << endl;
      return 0;
    }
  }
  // cout << "loop " << loop << " s_index: " << s_index<< " K: " << K<< endl;
  if (loop == 0) {
    nowpos = 0;
    rep(i, N) {
      nowpos = a[nowpos];
      if (K == i + 1) {
        cout << nowpos + 1 << endl;
        return 0;
      }
    }
    cout << nowpos + 1 << endl;
    return 0;
  }
  K -= f;
  K %= loop;
  nowpos = s_index;

  rep(i, K) { nowpos = a[nowpos]; }
  cout << nowpos + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
const ll INF = 1LL << 60;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> a(N);
  rep(i, N) {
    int num;
    cin >> num;
    num--;
    a[i] = num;
  }
  // ループの始まりを見つける
  map<int, int> m;
  int maxnum = 0;
  int nowpos = 0;
  rep(i, 500000) {
    m[nowpos]++;
    maxnum = max(m[nowpos], maxnum);
    nowpos = a[nowpos];
  }
  int s_index = 0;
  for (auto x : m) {
    if (maxnum == x.second) {
      s_index = x.first;
      break;
    }
  }
  nowpos = 0;
  int f = -1, loop = 0;
  rep(i, N) { // 1ループいくつか測定
    if (f == -1 && nowpos == s_index) {
      f = i;
    } else {
      if (f != -1 && nowpos == s_index) {
        loop = i - f;
        break;
      }
    }
    nowpos = a[nowpos];
    if (K == i + 1) {
      cout << nowpos + 1 << endl;
      return 0;
    }
  }
  K -= f;
  K %= loop;
  nowpos = s_index;
  // cout << "loop " << loop << " s_index: " << s_index<< " K: " << K<< endl;
  rep(i, K) { nowpos = a[nowpos]; }
  cout << nowpos + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  long long k;
  int x;
  scanf("%d %lld", &n, &k);

  vector<int> next(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x);
    next[i] = x - 1;
  }

  vector<bool> visited(n);
  vector<int> chain;

  visited[0] = true;
  chain.push_back(0);

  while (k > 0) {
    int nn = next[chain[chain.size() - 1]];
    --k;

    chain.push_back(nn);

    if (visited[nn]) {
      break;
    }
    visited[nn] = true;
  }

  if (k == 0) {
    printf("%d\\n", chain[chain.size() - 1] + 1);
    return 0;
  }

  // Find cycle

  int index = 0;
  while (chain[index++] != chain[chain.size() - 1])
    ;

  vector<int> whole_cycle(chain.begin() + index, chain.end());

  --k;

  k %= whole_cycle.size();

  printf("%d", whole_cycle[k] + 1);

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <functional>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  long long k;
  int x;
  scanf("%d %lld", &n, &k);

  vector<int> next(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x);
    next[i] = x - 1;
  }

  vector<bool> visited(n);
  vector<int> chain;

  visited[0] = true;
  chain.push_back(0);

  while (k > 0) {
    int nn = next[chain[chain.size() - 1]];
    --k;

    chain.push_back(nn);

    if (visited[nn]) {
      break;
    }
    visited[nn] = true;
  }

  if (k == 0) {
    printf("%d\\n", chain[chain.size() - 1] + 1);
  }

  // Find cycle

  int index = 0;
  while (chain[index++] != chain[chain.size() - 1])
    ;

  vector<int> whole_cycle(chain.begin() + index, chain.end());

  --k;

  k %= whole_cycle.size();

  printf("%d", whole_cycle[k] + 1);

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

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> p;
  vector<int> v;
  rep(i, n) {
    int a;
    scanf("%d", &a);
    p.push_back(make_pair(i + 1, a));
  }
  // for(auto c : p) cout << c.first << " " << c.second << endl;
  ll start = p[0].second;
  v.push_back(1);
  set<int> S;
  ll counter;
  while (true) {
    ll size_before = S.size();
    v.push_back(start);
    S.insert(start);
    if (S.size() == size_before) {
      counter = S.size();
      break;
    }
    start = p[start - 1].second;
  }
  ll turn = v[counter + 1];
  ll xx;
  rep(i, v.size()) {
    if (v[i] == turn) {
      xx = i;
      break;
    }
  }
  // cout << xx << endl;
  ll h = counter - xx + 1;
  // cout << "h= " << h << endl;
  if (k >= xx) {
    k -= xx;
    ll rem = k % h;
    // rep(i,20) cout << v[i] << " ";
    // cout << endl;
    cout << v[rem + xx] << "\\n";
  } else {
    cout << v[k] << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> p;
  vector<int> v;
  rep(i, n) {
    int a;
    scanf("%d", &a);
    p.push_back(make_pair(i + 1, a));
  }
  // for(auto c : p) cout << c.first << " " << c.second << endl;
  int start = p[0].second;
  v.push_back(1);
  rep(i, n + 1) {
    v.push_back(start);
    start = p[start - 1].second;
  }
  int counter = 0;
  for (auto x : p) {
    if (x.first == x.second)
      counter++;
  }
  int h = n - counter;
  ll rem = k % h;
  // rep(i,n) cout << v[i] << " ";
  // cout << endl;
  cout << v[rem] << "\\n";
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

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

  vector<int> used;
  int index = 0;
  int loop_size;
  int loop_begin;
  vector<bool> checked(n, false);
  while (1) {
    checked[index] = true;
    used.push_back(index);
    k--;
    index = a[index];
    if (k == 0) {
      cout << index + 1 << endl;
      return 0;
    }

    if (checked[index]) {
      auto itr = find(used.begin(), used.end(), index);
      loop_size = used.size() - (itr - used.begin());
      loop_begin = index;
      break;
    }
  }

  k %= (ll)loop_size;
  int res = loop_begin;
  for (int i = 0; i < k; ++i) {
    res = a[res];
  }
  cout << res + 1 << endl;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

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

  vector<int> used;
  int index = 0;
  int loop_size;
  int loop_begin;
  while (1) {
    used.push_back(index);
    k--;
    index = a[index];
    if (k == 0) {
      cout << index + 1 << endl;
      return 0;
    }

    auto itr = find(used.begin(), used.end(), index);
    if (itr != used.end()) {
      loop_size = used.size() - (itr - used.begin());
      loop_begin = index;
      break;
    }
  }

  k %= (ll)loop_size;
  int res = loop_begin;
  for (int i = 0; i < k; ++i) {
    res = a[res];
  }
  cout << res + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define MOD_NUMBER 1000000007

// ベクトル要素の列挙
template <class T> void vector_print(vector<T> vec) {
  for (unsigned int i = 0; i < vec.size(); i++) {
    cout << vec.at(i);
    if (i < vec.size() - 1) {
      cout << " ";
    } else if (i == vec.size() - 1) {
      cout << endl;
    }
  }
}

// エラトステネスの篩による素数配列の出力
vector<int> Eratosthenes(int N) {
  vector<int> p = {2};
  int sqrtN = sqrt(N);
  for (int i = 3; i <= N; i++) {
    p.push_back(i);
    for (int j = 0; p.at(j) <= sqrtN; j++) {
      if (i % p.at(j) == 0) {
        p.pop_back();
        break;
      }
    }
  }
  return p;
}

int main() {
  ll n, k, x = 1, size = 0, spare;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> b(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < n; i++) {
    if (a.at(x - 1) <= 0) {
      break;
    }
    spare = x;
    x = a.at(x - 1);
    a.at(spare - 1) = -size;
    b.at(i) = x;
    size++;
  }
  if (k <= size) {
    cout << b.at(k - 1) << endl;
    return 0;
  }
  k += a.at(x - 1);
  k %= size + a.at(x - 1);
  k -= a.at(x - 1);
  if (k == 0) {
    k = size;
  }
  cout << b.at(k - 1) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define MOD_NUMBER 1000000007

// ベクトル要素の列挙
template <class T> void vector_print(vector<T> vec) {
  for (unsigned int i = 0; i < vec.size(); i++) {
    cout << vec.at(i);
    if (i < vec.size() - 1) {
      cout << " ";
    } else if (i == vec.size() - 1) {
      cout << endl;
    }
  }
}

// エラトステネスの篩による素数配列の出力
vector<int> Eratosthenes(int N) {
  vector<int> p = {2};
  int sqrtN = sqrt(N);
  for (int i = 3; i <= N; i++) {
    p.push_back(i);
    for (int j = 0; p.at(j) <= sqrtN; j++) {
      if (i % p.at(j) == 0) {
        p.pop_back();
        break;
      }
    }
  }
  return p;
}

int main() {
  ll n, k, x = 1, size = 0, spare;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> b(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < n; i++) {
    if (a.at(x - 1) <= 0) {
      break;
    }
    spare = x;
    x = a.at(x - 1);
    a.at(spare - 1) = -size;
    b.at(i) = x;
    size++;
  }
  if (k <= size) {
    cout << b.at(k - 1) << endl;
    return 0;
  }
  k += a.at(x - 1);
  k %= size - a.at(x - 1);
  k -= a.at(x - 1);
  if (k == 0) {
    k = size;
  }
  cout << b.at(k - 1) << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;
const int N = 2e5 + 1;
long long k;
int n, a[N];
bool avail[N];
vector<int> V;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  V.push_back(0);
  int start = 1;
  bool run = false;
  while (avail[a[start]] == false) {
    start = a[start];
    avail[start] = true;
    V.push_back(start);
  }
  // V.push_back(a[start]);
  // for (int i = 1; i < V.size(); i++) cout << V[i] << " "; cout << "\\n";
  int d, c = V.size() - 1;
  for (int i = 1; i < V.size(); i++)
    if (a[start] == V[i])
      d = i;
  // cout << d << " " << c << "\\n";
  if (k < d)
    cout << V[k];
  else {
    k = k - d + 1;
    k = k % (c - d + 1);
    if (k == 0)
      cout << V[c];
    else
      cout << V[d + k - 1];
  }
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
const int N = 2e5 + 1;
long long k;
int n, a[N];
bool avail[N];
vector<int> V;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  V.push_back(0);
  int start = 1;
  bool run = false;
  while (avail[a[start]] == false) {
    start = a[start];
    avail[start] = true;
    V.push_back(start);
  }
  // V.push_back(a[start]);
  // for (int i = 1; i < V.size(); i++) cout << V[i] << " "; cout << "\\n";
  int d, c = V.size() - 1;
  for (int i = 1; i < V.size() - 1; i++)
    if (a[start] == V[i])
      d = i;
  // cout << d << " " << c << "\\n";
  if (k < d)
    cout << V[k];
  else {
    k = k - d + 1;
    k = k % (c - d + 1);
    if (k == 0)
      cout << V[c];
    else
      cout << V[d + k - 1];
  }
  return 0;
}