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

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

using ll = long long;

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

#ifdef ARTHUR_LOCAL
  ifstream cin("input.txt");
#endif

  int n;
  ll k;
  cin >> n >> k;

  deque<int> path;
  vector<bool> vis(n + 1);

  vector<int> A = {-69};

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

  int cur = 1;

  while (!vis[cur]) {
    if (k == 0) {
      cout << cur << endl;
      return 0;
    }

    k--;

    vis[cur] = 1;
    path.push_back(cur);

    cur = A[cur];
  }

  while (path.front() != cur)
    path.pop_front();

  // cout << path.size() << endl;

  // while(!path.empty())
  // {
  // 	cout << path.front() << " ";
  // 	path.pop_front();
  // }

  // cout << k << endl;

  k %= ll(path.size());

  while (k--)
    path.pop_front();
  cout << path.front() << endl;
}

Error: 0

Fixed submission:

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

using ll = long long;

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

#ifdef ARTHUR_LOCAL
  ifstream cin("input.txt");
#endif

  int n;
  ll k;
  cin >> n >> k;

  deque<int> path;
  vector<bool> vis(n + 1);

  vector<int> A = {-69};

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

  int cur = 1;

  while (!vis[cur]) {
    if (k == 0)
      return cur;
    k--;

    vis[cur] = 1;
    path.push_back(cur);

    cur = A[cur];
  }

  while (path.front() != cur)
    path.pop_front();

  // while(!path.empty())
  // {
  // 	cout << path.front() << " ";
  // 	path.pop_front();
  // }

  // cout << k << endl;

  k %= ll(path.size());

  while (k--)
    path.pop_front();
  cout << path.front() << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;

// d[i][k] := ノード i から 2^(k) 移動してたどり着くノード
int d[200050][100];
const int u_b = 62;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &t : a) {
    cin >> t;
    --t;
  }
  for (int i = 0; i < n; ++i) {
    d[i][0] = a[i];
  }
  for (int j = 0; j < u_b; ++j) {
    for (int i = 0; i < n; ++i) {
      d[i][j + 1] = d[d[i][j]][j];
    }
  }
  priority_queue<int> cand;
  for (int i = 0; i < u_b; ++i) {
    if (k bitand (1ll << i)) {
      cand.push(i);
    }
  }
  int cur = 0;
  while (!cand.empty()) {
    cur = d[cur][cand.top()];
    cand.pop();
  }
  cout << cur + 1 << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;

// d[i][k] := ノード i から 2^(k) 移動してたどり着くノード
int d[100][200050];
const int u_b = 62;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &t : a) {
    cin >> t;
    --t;
  }
  for (int i = 0; i < n; ++i) {
    d[i][0] = a[i];
  }
  for (int i = 0; i < u_b; ++i) {
    for (int j = 0; j < n; ++j) {
      d[i + 1][j] = d[d[i][j]][j];
    }
  }
  priority_queue<int> cand;
  for (int i = 0; i < u_b; ++i) {
    if (k bitand (1ll << i)) {
      cand.push(i);
    }
  }
  int cur = 0;
  while (!cand.empty()) {
    cur = d[cand.top()][cur];
    cand.pop();
  }
  cout << cur + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
#define DEBUG(x) cout << x << '\\n'

int n, a[MAXN], vis[MAXN], pos[MAXN];
ll k;
int dfs(int u) {
  if (~vis[a[u]])
    return a[u];
  vis[a[u]] = vis[u] + 1;
  pos[vis[a[u]]] = a[u];
  return dfs(a[u]);
}
int main() {
#ifdef Irene
  freopen("in.txt", "r", stdin);
#endif // Irene
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i], vis[i] = -1;
  vis[1] = 0, pos[0] = 1;
  int x = dfs(1);
  int mx = *max_element(vis + 1, vis + n + 1) + 1;
  int ans;
  if (k < mx)
    ans = pos[k];
  else {
    int circle = mx - vis[x];
    k -= vis[x];
    k %= circle;
    ans = pos[vis[x] + k];
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
#define DEBUG(x) cout << x << '\\n'

int n, a[MAXN], vis[MAXN], pos[MAXN];
ll k;
int dfs(int u) {
  if (~vis[a[u]])
    return a[u];
  vis[a[u]] = vis[u] + 1;
  pos[vis[a[u]]] = a[u];
  dfs(a[u]);
}
int main() {
#ifdef Irene
  freopen("in.txt", "r", stdin);
#endif // Irene
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i], vis[i] = -1;
  vis[1] = 0, pos[0] = 1;
  int x = dfs(1);
  int mx = *max_element(vis + 1, vis + n + 1) + 1;
  int ans;
  if (k < mx)
    ans = pos[k];
  else {
    int circle = mx - vis[x];
    k -= vis[x];
    k %= circle;
    ans = pos[vis[x] + k];
  }
  cout << ans;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<ll>

int main() {

  ll N, K;
  cin >> N >> K;

  vl A;
  ll a;
  A.push_back(0);
  for (ll i = 0; i < N; ++i) {
    cin >> a;
    A.push_back(a);
  }

  vl B;
  set<ll> visited;
  B.push_back(1);
  visited.insert(1);
  ll next;
  ll loop_s = 0;
  ll loop_e = 0;
  for (ll i = 0; i < N + 1; ++i) {
    next = A[B[i]];
    set<ll>::iterator itr = visited.find(next);
    if (itr != visited.end()) {
      for (ll j = 0; j <= i; ++j) {
        if (B[j] == next) {
          loop_s = j;
          break;
        }
      }
      loop_e = i;
      break;
    } else {
      B.push_back(next);
      visited.insert(next);
    }
  }

  if (K <= loop_e) {
    cout << B[K] << endl;
  } else {
    K -= loop_s;
    K %= (loop_e - loop_s + 1);
    K += loop_s;
    cout << B[K] << endl;
  }

  // cout << "loop_s : " << loop_s << endl;
  // cout << "loop_e : " << loop_e << endl;
  // for (ll i = 0; i < B.size(); ++i) {
  //     cout << B[i] << " ";
  // }
  // cout << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<ll>

int main() {

  ll N, K;
  cin >> N >> K;

  vl A;
  ll a;
  A.push_back(0);
  for (int i = 0; i < N; ++i) {
    cin >> a;
    A.push_back(a);
  }

  vl B;
  set<ll> visited;
  B.push_back(1);
  visited.insert(1);
  ll next;
  ll loop_s;
  ll loop_e;
  for (int i = 0; i < N; ++i) {
    next = A[B[i]];
    set<ll>::iterator itr = visited.find(next);
    if (itr != visited.end()) {
      for (int j = 0; j < i; ++j) {
        if (B[j] == next) {
          loop_s = j;
          break;
        }
      }
      loop_e = i;
      break;
    } else {
      B.push_back(next);
      visited.insert(next);
    }
  }

  if (K <= loop_e) {
    cout << B[K] << endl;
  } else {
    K -= loop_s;
    K %= (loop_e - loop_s + 1);
    K += loop_s;
    cout << B[K] << endl;
  }

  // cout << "loop_s : " << loop_s << endl;
  // cout << "loop_e : " << loop_e << endl;
  // for (int i = 0; i < B.size(); ++i) {
  //     cout << B[i] << " ";
  // }
  // cout << endl;

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  unsigned long long n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  vector<int> b;
  set<int> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int i = 1;
  b.push_back(1);
  while (s.find(a[i]) == s.end()) {
    b.push_back(a[i]);
    s.insert(a[i]);
    i = a[i];
  }
  unsigned int d = distance(b.begin(), find(b.begin(), b.end(), a[i]));
  if (k < d)
    cout << b[k] << endl;
  else {
    k -= d;
    k = k % (b.size() - d);
    cout << b[d + k] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  unsigned long long n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  vector<int> b;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int i = 1;
  b.push_back(1);
  while (find(b.begin(), b.end(), a[i]) == b.end()) {
    b.push_back(a[i]);
    i = a[i];
  }
  unsigned int d = distance(b.begin(), find(b.begin(), b.end(), a[i]));
  if (k < d)
    cout << b[k] << endl;
  else {
    k -= d;
    k = k % (b.size() - d);
    cout << b[d + k] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define eb(t) emplace_back(t)
typedef long long ll;
typedef long long unsigned int llu;
ll INF = 1000000009;
ll MOD = 1000000007;

ll a[200100];

void solve() {

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

  set<ll> st;
  vector<ll> v;
  v.eb(0);
  st.insert(0);
  ll pointer = 0;
  ll t1 = 0;
  ll count1 = 1;
  while (true) {
    // cout << pointer << endl;
    decltype(st)::iterator it = st.find(a[pointer]);
    if (it != st.end()) { // 見つかった
      // cout << "find" << *it << std::endl;
      t1 = *it;
      break;
    }
    st.insert(a[pointer]);
    pointer = a[pointer];
    count1++;
    v.eb(pointer);
  }

  ll count2 = 1;
  ll tm = 0;
  while (true) {
    if (a[tm] != t1) {
      tm = a[tm];
      count2++;
    } else {
      break;
    }
  }

  /*
  for(auto e:v){
      cout << e << "!" << endl;
  }
  */

  // cout << count1 << " " << count2 << endl;

  ll y, x;
  if (count1 == count2) {
    y = count1;
    x = 0;
  } else {
    y = count1 - count2;
    x = count2;
  }

  if (k < x) {
    cout << v[k] + 1 << endl;
    return;
  }

  /*
  cout << v.size() << endl;
  cout << k << endl;
  cout << x << endl;
  cout << y << endl;

  cout << (k-x)%y+x << endl;
  */
  cout << v[(k - x) % y + x] + 1 << endl;
}

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define eb(t) emplace_back(t)
typedef long long ll;
typedef long long unsigned int llu;
ll INF = 1000000009;
ll MOD = 1000000007;

ll a[20010];

void solve() {

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

  set<ll> st;
  vector<ll> v;
  v.eb(0);
  st.insert(0);
  ll pointer = 0;
  ll t1 = 0;
  ll count1 = 1;
  while (true) {
    // cout << pointer << endl;
    decltype(st)::iterator it = st.find(a[pointer]);
    if (it != st.end()) { // 見つかった
      // cout << "find" << *it << std::endl;
      t1 = *it;
      break;
    }
    st.insert(a[pointer]);
    pointer = a[pointer];
    count1++;
    v.eb(pointer);
  }

  ll count2 = 1;
  ll tm = 0;
  while (true) {
    if (a[tm] != t1) {
      tm = a[tm];
      count2++;
    } else {
      break;
    }
  }

  /*
  for(auto e:v){
      cout << e << "!" << endl;
  }
  */

  // cout << count1 << " " << count2 << endl;

  ll y, x;
  if (count1 == count2) {
    y = count1;
    x = 0;
  } else {
    y = count1 - count2;
    x = count2;
  }

  if (k < x) {
    cout << v[k] + 1 << endl;
    return;
  }

  /*
  cout << v.size() << endl;
  cout << k << endl;
  cout << x << endl;
  cout << y << endl;

  cout << (k-x)%y+x << endl;
  */
  cout << v[(k - x) % y + x] + 1 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  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 < (int)(n); i++)
typedef long long ll;

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, 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];
  else {
    k -= l;
    k %= c;
    cout << s[l + k];
  }
}


Error: WA

Fixed submission:

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

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

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c = 1, l = 0, 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 < 1)
    cout << s[k];
  else {
    k -= l;
    k %= c;
    cout << s[l + k];
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int32_t main() {
  int n;
  int k;
  cin >> n >> k;
  vector<int> arr(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  if (k > n) {
    vector<int> kaha;
    kaha.push_back(0);
    set<int> st;
    int i;
    int prev = 1;
    for (i = 1; i <= n; i++) {
      if (st.find(arr[prev]) == st.end())
        kaha.push_back(arr[prev]);
      else
        break;

      st.insert(arr[prev]);
      prev = arr[prev];
    }

    int ele = arr[prev];
    int pos = find(kaha.begin(), kaha.end(), ele) - kaha.begin();
    vector<int> res;
    res.assign(kaha.begin() + pos, kaha.end());
    k = k - kaha.size();
    int index = k % res.size();
    cout << res[index] << endl;
  } else {
    int prev = 1;
    int pos = 1;
    for (int i = 1; i <= k; i++) {
      pos = arr[prev];
      prev = pos;
    }
    cout << pos << endl;
  }
}


Error: TLE

Fixed submission:

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

int32_t main() {
  int n;
  int k;
  cin >> n >> k;
  vector<int> arr(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int pos = 1;
  int prev = 1;
  while (k--) {
    pos = arr[prev];
    prev = pos;
  }
  cout << pos << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
bool vis[200009];
main() {
  vector<long long int> v;
  long long int n, i, j, k, l, m, cnt = -1;
  cin >> n >> k;
  long long int a[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  i = 1;
  while (cnt < k) {
    if (vis[i] == false) {
      v.push_back(i);
      vis[i] = true;
      i = a[i];
      cnt++;
    } else {
      break;
    }
  }
  if (cnt == k) {
    cout << v[k] << endl;
  } else {
    k -= (cnt + 1);
    // cout<<k<<endl;
    for (j = 0; j < v.size(); j++) {
      if (v[j] == i) {
        break;
      }
    }
    l = v.size() - j;
    // cout<<l<<endl;
    cout << v[j + (k % l)] << endl;
  }
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool vis[200009];
int main() {
  vector<long long int> v;
  long long int n, i, j, k, cnt = 0;
  cin >> n >> k;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  i = 0;
  v.push_back(1);
  while (cnt < k) {
    if (vis[i] == false) {
      vis[i] = true;
      v.push_back(a[i]);
      cnt++;
      i = a[i] - 1;
    } else {
      break;
    }
  }
  if (cnt == k) {
    cout << v[v.size() - 1] << endl;
  } else {
    k -= cnt;
    for (j = 0; j < v.size(); j++) {
      if (v[j] == i) {
        break;
      }
    }
    j = v.size() - j;
    j = k % j;
    cout << v[v.size() - 1 - j] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 1; i < (int)(n); i++)
#define dunk(n) cout << n << endl
#define all(a) (a).begin(), (a).end()
typedef pair<int, int> P;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> to(n);
  rep(i, n) {
    cin >> to[i];
    to[i]--;
  }
  vector<bool> been(n, false); // n番目の街に行ったことがあるか
  been[0] = true;
  ll now = 0, step = 0;
  vector<ll> ever;
  ever.push_back(0);
  ll re = 0;
  while (true) {
    step++;
    now = to[now];
    if (!been[now]) { // atteru
      been[now] = true;
      ever.push_back(now);
    } else { // tabunkoko
      rep(i, ever.size()) {
        if (ever[i] == now) {
          re = i;
          break;
        }
      }
      break;
    }
  }
  if (k < re) {
    cout << ever[k] + 1 << endl;
    return 0;
  }

  ll a = (k - re) % (step - re) + re;
  dunk(ever[a] + 1);
  return 0;
}


Error: -6
/usr/include/c++/12/debug/vector:442:
In function:
    std::debug::vector<_Tp, _Allocator>::reference std::debug::vector<_Tp, 
    _Allocator>::operator[](size_type) [with _Tp = long long int; _Allocator 
    = std::allocator<long long int>; reference = long long int&; size_type = 
    long unsigned int]

Error: attempt to subscript container with out-of-bounds index 4, but 
container only holds 3 elements.

Objects involved in the operation:
    sequence "this" @ 0x7ffe60cd14b0 {
      type = std::debug::vector<long long, std::allocator<long long> >;
    }


Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 1; i < (int)(n); i++)
#define dunk(n) cout << n << endl
#define all(a) (a).begin(), (a).end()
typedef pair<int, int> P;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> to(n);
  rep(i, n) {
    cin >> to[i];
    to[i]--;
  }
  vector<bool> been(n, false); // n番目の街に行ったことがあるか
  been[0] = true;
  ll now = 0, step = 0;
  vector<ll> ever;
  ever.push_back(0);
  ll re = 0;
  while (true) {
    step++;
    now = to[now];
    if (!been[now]) {
      been[now] = true;
      ever.push_back(now);
    } else {
      rep(i, ever.size()) {
        if (ever[i] == now) {
          re = i;
          break;
        }
      }
      break;
    }
  }
  if (k < re || re == 0) {
    cout << ever[k - 1] + 1 << endl;
    return 0;
  }
  ll a = (k - re) % (step - re) + re;
  dunk(ever[a] + 1);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, a[200002];
int vis[200002];
long long k;
int f(int i, int cnt) {
  if (cnt == k)
    return i;
  if (vis[i] != -1) {
    k -= vis[i];
    k %= (1LL * cnt - vis[i]);
    int ans = i;
    for (int j = 0; j < k; j++)
      ans = a[ans];
    return ans;
  }
  vis[i] = cnt;
  return f(a[i], cnt + 1);
}
int main() {
  memset(vis, -1, sizeof(vis));
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << f(1, 0);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, a[200002];
int vis[200002];
long long k;
int chk(int i, int cnt) {
  if (!cnt)
    return i;
  return chk(a[i], cnt - 1);
}
int f(int i, int cnt) {
  if (vis[i] != -1) {
    k -= 1LL * vis[i];
    k %= 1LL * (cnt - vis[i]);
    return chk(i, k);
  }
  vis[i] = cnt;
  f(a[i], cnt + 1);
}
int main() {
  memset(vis, -1, sizeof(vis));
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << f(1, 0);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

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

  map<ll, ll> mp;

  ll point = 1;
  ll loop, start;
  vector<ll> vec;
  for (int i = 0; i <= n + 3; i++) {
    if (mp.count(point)) {
      start = mp[point];
      loop = i - start;
      break;
    }
    // cout << point << endl;
    vec.push_back(point);
    mp[point] = i;
    point = a[point];
  }
  // cout << a[6] << endl;

  // for (ll x : vec) cout << x << endl;
  if (k < start) {
    cout << vec[k] << endl;
    return 0;
  }
  // cout << loop << endl;
  // cout << start << endl;
  k -= start;
  ll p = k % loop;
  ll ans = vec[start + p];
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

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

  map<ll, ll> mp;

  ll point = 1;
  ll loop, start;
  vector<ll> vec;
  for (int i = 0; i <= n + 3; i++) {
    if (mp.count(point)) {
      start = mp[point];
      loop = i - start;
      break;
    }
    // cout << point << endl;
    vec.push_back(point);
    mp[point] = i;
    point = a[point];
  }
  // cout << a[6] << endl;

  // for (ll x : vec) cout << x << endl;
  if (k < start) {
    cout << vec[k] << endl;
    return 0;
  }
  // cout << loop << endl;
  // cout << start << endl;
  k -= start;
  ll p = k % loop;
  ll ans = vec[start + p];
  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#pragma warning(disable : 4996)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793

using namespace std;
using ll = long long;

const int D = 60;
const int MAX_N = 200005;

int to[D][MAX_N];

int main() {

  ll n, k;
  cin >> n >> k;

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

  rep(i, D - 1) {
    rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; }
  }
  int v = 0;

  for (int i = D - 1; i >= 0; i--) {
    ll l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }
  cout << v + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#pragma warning(disable : 4996)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793

using namespace std;
using ll = long long;

const int D = 60;
const int MAX_N = 200005;

int to[D][MAX_N];

int main() {

  ll n, k;
  cin >> n >> k;

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

  rep(i, n) {
    rep(j, n) { to[i + 1][j] = to[i][to[i][j]]; }
  }
  int v = 0;

  for (int i = D - 1; i >= 0; i--) {
    ll l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }
  cout << v + 1 << 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 lint = long long;

int main() {
  int n;
  lint k;
  scanf("%d%lld", &n, &k);
  vector<int> to(n);
  rep(u, n) scanf("%d", &to[u]), to[u]--;

  static int dbl[60][200000];
  rep(u, n) dbl[0][u] = to[u];
  rep(t, 59) rep(u, n) dbl[t + 1][u] = dbl[t][dbl[t][u]];

  int pos = 0;
  rep(t, 60) if (k >> t & 1) pos = dbl[t][pos];
  printf("%d\\n", pos + 1);

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using lint = long long;

int main() {
  int n;
  lint k;
  scanf("%d%lld", &n, &k);
  vector<int> to(n);
  rep(u, n) scanf("%d", &to[u]), to[u]--;

  static int dbl[60][100000];
  rep(u, n) dbl[0][u] = to[u];
  rep(t, 59) rep(u, n) dbl[t + 1][u] = dbl[t][dbl[t][u]];

  int pos = 0;
  rep(t, 60) if (k >> t & 1) pos = dbl[t][pos];
  printf("%d\\n", pos + 1);

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

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

long long n, k, now = 1, cnt, a[210000], pre[210000];

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  while (k--) {
    now = a[now];
    if (pre[now])
      k %= cnt - pre[now];
    pre[now] = cnt++;
  }
  cout << now << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long k, now = 1, cum = 0, n, a[20005], b[20005];
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  while (k--) {
    now = a[now];
    if (b[now])
      k %= cum - 1;
    b[now] = cum++;
  }
  printf("%lld", now);
  return 0;
}