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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#ifdef LOCAL
#include "_/debug.h"
#else
#define out(...) 69;
#endif

const int N = 2e5 + 5;

vector<int> g[N];
deque<int> ord;
int vis[N];
ll k;

bool found = false;

void dfs(int u, ll cnt = 0LL) {
  if (cnt == k) {
    found = true;
    cout << u << '\\n';
    return;
  }
  if (vis[u]) {
    while (ord.front() != u)
      ord.pop_front();
    k -= cnt;
    return;
  }
  vis[u] = 1;
  ord.push_back(u);
  for (int v : g[u]) {
    dfs(v, cnt + 1LL);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> k;
  for (int u = 1; u <= n; u++) {
    int v;
    cin >> v;
    g[u].push_back(v);
  }
  dfs(1);
  if (found)
    return 0;
  int node = k % (ll)ord.size();
  cout << ord[node] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#ifdef LOCAL
#include "_/debug.h"
#else
#define out(...) 69;
#endif

const int N = 2e5 + 5;

vector<int> g[N];
deque<int> ord;
int vis[N];
ll k;

int mod(string s, int m) {
  int n = (int)s.size();
  int x = 0;
  for (int i = 0; i < n; i++) {
    x = (x * 10 + (int)s[i] - '0') % m;
  }
  return x;
}

void dfs(int u, ll cnt = 0LL) {
  if (vis[u]) {
    while (ord.front() != u)
      ord.pop_front();
    k -= cnt;
    return;
  }
  vis[u] = 1;
  ord.push_back(u);
  for (int v : g[u]) {
    dfs(v, cnt + 1LL);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> k;
  for (int u = 1; u <= n; u++) {
    int v;
    cin >> v;
    g[u].push_back(v);
  }
  dfs(1);
  int node = mod(to_string(k), (int)ord.size());
  cout << ord[node] << '\\n';
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 1000000000000000
#define mod 1000000007
#define ld long double
#define pll pair<ll, ll>
using namespace std;
int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  ll up[n + 1][61];
  memset(up, 0, sizeof(up));
  for (ll i = 1; i <= n; i++)
    up[i][0] = a[i];
  for (ll j = 1; j < 61; j++)
    for (ll i = 1; i <= n; i++)
      up[i][j] = up[up[i][j - 1]][j - 1];
  ll strt = 1, ct = 0;
  for (ll i = 60; i > -1; i--)
    if (ct + (1ULL << i) <= k)
      strt = up[strt][i], ct = ct + (1ULL << i);
  cout << strt;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[200005];
int main() {
  int n, k, m = 1;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  while (k--) {
    m = a[m];
  }
  cout << m << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)

using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

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

  ll i, j, h;
  ll Q, M, K, P, A, B, C, N, X;

  long double x;

  string S, T;

  ll ans = 0;
  ll count = 0;
  ll flag = 0;
  ll temp = 0;

  vector<ll> aa, cc, hh, ff;
  vector<vector<ll>> bb;
  map<ll, ll> m;

  cin >> N >> K;
  rep(i, N) {
    cin >> temp;
    aa.push_back(temp);
  }
  cc.push_back(0);
  temp = 0;
  m[0] = 1;
  rep(i, K) {
    temp = aa[temp] - 1;
    if (m[temp] == 1) {
      repr(j, 0, cc.size()) {
        if (cc[j] == temp) {
          // cout<<"A"<<i<<"B"<<j<<endl;
          ans = cc[j + (((K - i) % (i - j + 1)) - 1 + i - j + 1) % (i - j + 1)];
          cout << ans + 1 << endl;
          return 0;
        }
      }
    }
    cc.push_back(temp);
    m[temp] = 1;
  }

  cout << temp + 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)

using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

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

  ll i, j, h;
  ll Q, M, K, P, A, B, C, N, X;

  long double x;

  string S, T;

  ll ans = 0;
  ll count = 0;
  ll flag = 0;
  ll temp = 0;

  vector<ll> aa, cc, hh, ff;
  vector<vector<ll>> bb;

  cin >> N >> K;
  rep(i, N) {
    cin >> temp;
    aa.push_back(temp);
  }
  cc.push_back(0);
  temp = 0;
  rep(i, K) {
    temp = aa[temp] - 1;
    repr(j, 0, cc.size()) {
      if (cc[j] == temp) {
        // cout<<"A"<<i<<"B"<<j<<endl;
        ans = cc[j + (((K - i) % (i - j + 1)) - 1 + i - j + 1) % (i - j + 1)];
        cout << ans + 1 << endl;
        return 0;
      }
    }
    cc.push_back(temp);
  }

  cout << temp + 1 << endl;

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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<ll> Vec;
typedef vector<pair<ll, ll>> Pair;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

// static const ll MOD = 1000000007;
// static const ll INF = 100000000;
// #define PI 3.14159265359

int main() {
  // input
  ll N, K;
  cin >> N >> K;
  Vec A(N);
  rep(i, N) {
    cin >> A[i];
    --A[i];
  }

  // process
  Vec is_visited(N, 0);
  ll now = 0, loop = 0, remaining_K = 0;
  rep(i, K) {
    if (is_visited[now] != 0) {
      loop = i - is_visited[now];
      remaining_K = K - i;
      break;
    }
    is_visited[now] = i;
    now = A[now];
  }

  if (remaining_K != 0) {
    remaining_K %= loop;
    rep(i, remaining_K) { now = A[now]; }
  }

  cout << now + 1 << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<ll> Vec;
typedef vector<pair<ll, ll>> Pair;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

// static const ll MOD = 1000000007;
// static const ll INF = 100000000;
// #define PI 3.14159265359

int main() {
  // input
  ll N, K;
  cin >> N >> K;
  Vec A(N);
  rep(i, N) {
    cin >> A[i];
    --A[i];
  }

  // process
  Vec is_visited(N, 0);
  ll now = 0, loop = 0, remaining_K = 0;
  rep(i, K) {
    if (is_visited[now] != 0) {
      loop = i - is_visited[now];
      remaining_K = K - i;
      break;
    }
    is_visited[now] = i;
    now = A[now];
  }

  remaining_K %= loop;
  rep(i, remaining_K) { now = A[now]; }

  cout << now + 1 << 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 < (n); i++)
#define in(n) cin >> (n)
#define out(ans) cout << (ans) << endl;
#define keta(n) cout << fixed << setprecision((n));
using ll = int64_t;
using v = vector<int>;

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

  bool roopjud = false;
  vector<bool> judlist(N, false);
  judlist.at(0) = true;

  ll gpoint = 0, npoint, rs = -1, mcount = 0, rcount = 0, ans;
  while (rs == -1 && mcount <= N) {
    npoint = tolist.at(gpoint);
    if (judlist.at(npoint)) {
      rs = gpoint;
      roopjud = true;
    } else {
      judlist.at(gpoint) = true;
      gpoint = npoint;
      mcount++;
    }
  }

  if (roopjud) {
    gpoint = rs;
    npoint = -1;
    while (npoint != rs) {
      npoint = tolist.at(gpoint);
      rcount++;
      gpoint = npoint;
    }

    if (K - mcount >= 0) {
      gpoint = rs;
      if ((K - mcount) % rcount == 0) {
        ans == rs;
      } else {
        for (ll i = 0; i < (K - mcount) % rcount; i++) {
          ans = tolist.at(gpoint);
          gpoint = ans;
        }
      }
    }

    else {
      gpoint = 0;
      for (ll i = 0; i < K; i++) {
        ans = tolist.at(gpoint);
        gpoint = ans;
      }
    }
  }

  else {
    gpoint = 0;
    for (ll i = 0; i < K; i++) {
      ans = tolist.at(gpoint);
      gpoint = ans;
    }
  }

  ans++;

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define in(n) cin >> (n)
#define out(ans) cout << (ans) << endl;
#define keta(n) cout << fixed << setprecision((n));
using ll = int64_t;
using v = vector<int>;

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

  bool roopjud = false;
  vector<bool> judlist(N, false);
  judlist.at(0) = true;

  ll gpoint = 0, npoint, rs = -1, mcount = 0, rcount = 0, ans;
  while (rs == -1 && mcount <= N) {
    npoint = tolist.at(gpoint);
    if (judlist.at(npoint)) {
      rs = gpoint;
      roopjud = true;
    } else {
      gpoint = npoint;
      mcount++;
    }
  }

  if (roopjud) {
    gpoint = rs;
    npoint = -1;
    while (npoint != rs) {
      npoint = tolist.at(gpoint);
      rcount++;
      gpoint = npoint;
    }

    if (K - mcount >= 0) {
      gpoint = rs;
      if ((K - mcount) % rcount == 0) {
        ans == rs;
      } else {
        for (ll i = 0; i < (K - mcount) % rcount; i++) {
          ans = tolist.at(gpoint);
          gpoint = ans;
        }
      }
    }

    else {
      gpoint = 0;
      for (ll i = 0; i < K; i++) {
        ans = tolist.at(gpoint);
        gpoint = ans;
      }
    }
  }

  else {
    gpoint = 0;
    for (ll i = 0; i < K; i++) {
      ans = tolist.at(gpoint);
      gpoint = ans;
    }
  }

  ans++;

  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define MOD 1000000007

void solve() {
  ll n, k;
  cin >> n >> k;
  ll arr[n + 1];
  rep(i, 1, n + 1) cin >> arr[i];
  vector<ll> v;
  ll tmp = 1;
  v.push_back(1);
  bool flag = 1;
  ll i, j = 0;
  bool brr[n + 1];
  rep(i, 1, n + 1) brr[i] = 0;
  while (1) {
    tmp = arr[tmp]; // cout<<tmp<<"@ ";
    if (brr[tmp] == 1) {
      flag = 0;
      break;
    }
    // for(auto x:v){

    //     if(tmp==x){
    //         flag=0;
    //         break;
    //     }
    // i++;
    //}
    if (flag == 1) {
      v.push_back(tmp);
      j++;
      brr[tmp] = 1;
    } else
      break;
  }
  i = 0;
  for (auto x : v) {
    if (x == tmp) {
      break;
    }
    i++;
  }
  // for (auto x: v)cout<<x<<" ";

  if (k < i)
    cout << v[k];
  else {
    cout << v[i + (k - i) % (j + 1 - i)];
  }

  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  rep(i, 0, t) { solve(); }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define MOD 1000000007

void solve() {
  ll n, k;
  cin >> n >> k;
  ll arr[n + 1];
  rep(i, 1, n + 1) cin >> arr[i];
  vector<ll> v;
  ll tmp = 1;
  v.push_back(1);
  bool flag = 1;
  ll i, j = 0;
  while (1) {
    i = 0;
    tmp = arr[tmp]; // cout<<tmp<<"@ ";
    for (auto x : v) {

      if (tmp == x) {
        flag = 0;
        break;
      }
      i++;
    }
    if (flag == 1) {
      v.push_back(tmp);
      j++;
    } else
      break;
  }
  // for (auto x: v)cout<<x<<" ";
  if (k < i)
    cout << v[k];
  else {
    cout << v[i + (k - i) % (j + 1 - i)];
  }

  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  rep(i, 0, t) { solve(); }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  ll now = 0;
  vector<ll> hist;
  set<ll> se;
  while (1) {
    hist.pb(now);
    se.insert(now);
    if (se.count(A[now]))
      break;
    now = A[now];
  }
  ll save = 0;
  rep(i, hist.size()) {
    if (hist[i] == A[now]) {
      save = i;
      break;
    }
  }
  vector<ll> new_hist;
  for (int i = save; i < hist.size(); i++) {
    new_hist.pb(hist[i]);
  }
  if (K < save) {
    cout << hist[K] + 1 << endl;
    return 0;
  }
  K -= save;
  ll len = new_hist.size();
  cout << new_hist[K % len] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  ll now = 0;
  vector<ll> hist;
  set<ll> se;
  while (1) {
    hist.pb(now);
    se.insert(now);
    if (se.count(A[now]))
      break;
    now = A[now];
  }
  ll save = 0;
  rep(i, hist.size()) {
    if (hist[i] == A[now]) {
      save = i;
      break;
    }
  }
  vector<ll> new_hist;
  for (int i = save; i < hist.size(); i++) {
    new_hist.pb(hist[i]);
  }
  K -= save;
  ll len = new_hist.size();
  cout << new_hist[K % len] + 1 << 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 < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int mod = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 1e6;

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

struct edge {
  int to, cost;
}; // 辺
// vector<edge> graph[MAX]; // 隣接リスト
bool visited[MAX]; // 訪問状況

int n, m; // 変数
ll k;
int a[MAX];

int main() {
  // input
  cin >> n >> k;
  rep(i, n + 1) {
    if (i > 0)
      cin >> a[i];
  }
  // solve
  int cnt = 1; // ループに突入するテレポート回数
  int start = 1;
  visited[start] = true;
  int next = a[start];
  while (not visited[next]) {
    cnt++;
    visited[next] = true;
    next = a[next];
  }
  int loop_start = next; // ループの始点
  // printf("ループに突入するテレポート回数%d\\n", cnt);
  // printf("ループの始点%d\\n", next);
  int ans;
  if (cnt < k) {
    next = 1;
    int d = 0; // ループ始点までに訪問する都市の数
    while (next != loop_start) {
      d++;
      next = a[next];
    }
    int loop_size = cnt - d;
    // printf("ループの大きさ%d\\n", loop_size);
    int pos = 1;
    rep(i, d) { pos = a[pos]; }
    if (loop_size == 0) {
      ans = pos;
    } else {
      rep(i, (k - d) % loop_size) { pos = a[pos]; }
      ans = pos;
    }
  } else {
    int pos = 1;
    rep(i, k) { pos = a[pos]; }
    ans = pos;
  }
  // output
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

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

int teleport(long long k,
             vector<int> city) { // 再起関数でk回移動後のテレポート先を求める
  if (k == 1) {
    return city.at(1);
  }
  return city.at(teleport(k - 1, city));
}

int main() {
  // int n = 6;
  int n;
  cin >> n;
  // long long k = 727202214173249351;
  long long k;
  cin >> k;
  vector<int> city(n + 1, 0);
  city.at(0) = 0;
  // city = {0,6,5,2,5,3,2};
  for (int i = 1; i < n + 1; i++) {
    cin >> city.at(i);
  }

  vector<int> roop;
  roop = {1};
  int roop_start; // ループに突入するテレポート回数
  int roop_dist;  // ループの間隔

  int x = city.at(1);
  while (1) {
    auto itr = find(roop.begin(), roop.end(), x);
    if (itr != roop.end()) {
      roop_start = distance(roop.begin(), itr);
      roop_dist = distance(itr, roop.end());
      break;
    } else {
      roop.push_back(x);
      x = city.at(x);
    }
  }

  // printf("%d\\n", roop_start);
  // printf("%d\\n", roop_dist);

  int ans;
  if (k >= roop_start) {
    k -= roop_start;
    k %= roop_dist;
    ans = roop.at(roop_start + k);
  } else {
    ans = teleport(k, city);
  }
  printf("%d\\n", ans);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n, 0);
  rep1(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<ll> town(n + 100, 0);
  rep2(i, 1, n + 100) { town[i] = a[town[i - 1]]; }
  ll num;
  vector<ll> visit(n + 100, -1);
  rep1(i, n + 100) {
    if (visit[town[i]] > -1) {
      num = i;
      break;
    }
    visit[town[i]] = i;
  }
  ll len = num - visit[town[num]];
  ll init = visit[town[num]];
  ll res = (k - init) % len;
  ll ans = town[min(k, res + init)];
  cout << ans + 1 << endl;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n, 0);
  rep1(i, n) {
    cin >> a[i];
    a[i]--;
  }
  vector<ll> town(n, 0);
  rep2(i, 1, n + 100) { town[i] = a[town[i - 1]]; }
  ll num;
  vector<ll> visit(n, -1);
  rep1(i, n + 100) {
    if (visit[town[i]] > -1) {
      num = i;
      break;
    }
    visit[town[i]] = i;
  }
  ll len = num - visit[town[num]];
  ll init = visit[town[num]];
  ll res = (k - init) % len;
  ll ans = town[res + init];
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long LL;
int T[200001];
bool vis[200001];
int ans, mulai, selesai;
vector<int> d;
LL K;

void dfs(int nod) {
  vis[nod] = 1;
  d.pb(nod);
  if (K == 0) {
    ans = nod;
    return;
  }
  if (!vis[T[nod]])
    K--, dfs(T[nod]);
  else {
    mulai = T[nod];
    selesai = nod;
  }
}

int main() {
  int N;
  scanf("%d%lld", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &T[i]);
  }
  dfs(1);
  if (K > 0) {
    int cycle;
    K--;
    int l1 = d.size();
    for (int i = 0; i < l1; i++) {
      if (d[i] == mulai) {
        for (int j = i; j < l1; j++) {
          if (d[j] == selesai) {
            cycle = j - i + 1;
            mulai = i;
            selesai = j;
            i = l1;
            break;
          }
        }
      }
    }
    K %= cycle;
    ans = d[mulai + K];
  }
  printf("%d\\n", ans);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long LL;
int T[200001];
bool vis[200001];
int ans, mulai, selesai;
vector<int> d;
LL K;

void dfs(int nod) {
  vis[nod] = 1;
  d.pb(nod);
  if (K == 0) {
    ans = nod;
    return;
  }
  if (!vis[T[nod]])
    K--, dfs(T[nod]);
  else {
    mulai = T[nod];
    selesai = nod;
  }
}

int main() {
  int N;
  scanf("%d%lld", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &T[i]);
  }
  dfs(1);
  if (K > 0) {
    int cycle;
    K--;
    int l1 = d.size();
    for (int i = 0; i < l1; i++) {
      if (d[i] == mulai) {
        for (int j = i + 1; j < l1; j++) {
          if (d[j] == selesai) {
            cycle = j - i + 1;
            mulai = i;
            selesai = j;
            i = l1;
            break;
          }
        }
      }
    }
    K %= cycle;
    ans = d[mulai + K];
  }
  printf("%d\\n", ans);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<long long> B(N), C(N);
  B = A;
  C = A;

  int times = 0;
  int place = 1;
  while (B.at(place - 1) != 0) {
    B.at(place - 1) = 0;
    place = A.at(place - 1);
    times++;
  }

  int times_again = 0;
  int place_again = place;
  while (C.at(place_again - 1) != 0) {
    C.at(place_again - 1) = 0;
    place_again = A.at(place_again - 1);
    times_again++;
  }

  if (times < K) {
    if ((K - times) % times_again == 0) {
      cout << place << endl;
    }

    else {
      for (int i = 0; i < (K - times) % times_again; i++) {
        place = A.at(place - 1);
      }
      cout << place << endl;
    }
  }

  else {
    int p = 1;
    for (int i = 0; i < K; i++) {
      p = A.at(p - 1);
    }
    cout << p << endl;
  }
}


Error: -8

Fixed submission:

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<long long> B(N), C(N);
  A = B;
  A = C;

  int times = 0;
  int place = 1;
  while (B.at(place - 1) != 0) {
    B.at(place - 1) = 0;
    place = A.at(place - 1);
    times++;
  }

  int times_again = 0;
  int place_again = place;
  while (C.at(place_again - 1) != 0) {
    C.at(place_again - 1) = 0;
    place_again = A.at(place_again - 1);
    times_again++;
  }

  for (int i = 0; i < (K - times) % times_again; i++) {
    place = A.at(place - 1);
  }

  cout << place << endl;
}