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

#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;

int main(void) {
  int N;
  long long K;
  int a;
  cin >> N >> K;

  Graph G(N);
  for (int i = 0; i < N; i++) {
    cin >> a;
    G[i].push_back(a);
  }
  vector<int> seen;
  vector<int> ord(N + 1, -1);

  int now = 1;
  int l = 0;
  int c = 1;
  while (1) {
    if (ord[now] != -1) {
      break;
    }
    ord[now] = seen.size();
    seen.push_back(now);
    now = G[now - 1][0];
  }

  c = seen.size() - ord[now];
  l = ord[now];
  if (K < l) {
    cout << seen[K] << endl;
  } else {
    cout << seen[l + ((K - l) % c)] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;

int main(void) {
  int N;
  long long K;
  int a;
  cin >> N >> K;

  Graph G(N);
  for (int i = 0; i < N; i++) {
    cin >> a;
    G[i].push_back(a);
  }
  vector<int> seen;
  vector<int> ord(N + 1, -1);

  int now = 1;
  int l = 0;
  int c = 1;
  while (1) {
    if (ord[now] != -1) {
      break;
    }
    ord[now] = seen.size();
    seen.push_back(now);
    now = G[now][0];
  }

  c = seen.size() - ord[now];
  l = ord[now];
  if (K < l) {
    cout << seen[K] << endl;
  } else {
    cout << seen[l + ((K - l) % c)] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"

constexpr int MOD = 1000000007;
const int INF = 1 << 30;

int main() {
  lint n, k;
  cin >> n >> k;
  vec a(n);
  vector<unordered_map<int, bool>> g2(n);
  vec g[200050];
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  rep(i, n) {
    if (!g2[i][a[i]]) {
      g[i].push_back(a[i]);
    }
    g2[i][a[i]] = true;
  }

  vector<bool> iv(n, false);

  int cur = 0;

  while (!iv[cur]) {
    iv[cur] = true;
    cur = a[cur];
  }

  int l = 0;
  int s = cur;

  while (true) {
    if (l != 0 && cur == s)
      break;
    cur = a[cur];
    l++;
  }

  int s_to_cur = 0;
  cur = 0;

  while (cur != s) {
    cur = a[cur];
    s_to_cur++;
  }

  if (s_to_cur > k) {
    int res = 0;
    rep(i, k) res = a[res];
    res++;
    cout << res << endl;
  } else {
    k = s_to_cur + ((k - s_to_cur) % l);
    int res = 0;
    rep(i, k) res = a[res];
    res++;
    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"

constexpr int MOD = 1000000007;
const int INF = 1 << 30;

int main() {
  lint n, k;
  cin >> n >> k;
  vec a(n);
  vector<unordered_map<int, bool>> g2(n);
  vec g[100010];
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  rep(i, n) {
    if (!g2[i][a[i]]) {
      g[i].push_back(a[i]);
    }
    g2[i][a[i]] = true;
  }

  vector<bool> iv(n, false);

  int cur = 0;

  while (!iv[cur]) {
    iv[cur] = true;
    cur = a[cur];
  }

  int l = 0;
  int s = cur;

  while (true) {
    if (l != 0 && cur == s)
      break;
    cur = a[cur];
    l++;
  }

  int s_to_cur = 0;
  cur = 0;

  while (cur != s) {
    cur = a[cur];
    s_to_cur++;
  }

  if (s_to_cur > k) {
    int res = 0;
    rep(i, k) res = a[res];
    res++;
    cout << res << endl;
  } else {
    k %= l;
    int res = 0;
    rep(i, k) res = a[res];
    res++;
    cout << res << 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() {
  int N, x, d1 = -1, d2 = -1;
  ll K;
  set<int> s;
  s.insert(0);

  cin >> N >> K;
  vector<int> A(N), t(N, 0);
  rep(i, N) cin >> A.at(i);

  rep(i, N - 1) {
    t.at(i + 1) = A.at(t.at(i)) - 1;
    if (s.count(t.at(i + 1))) {
      x = t.at(i + 1);
      break;
    }
    s.insert(t.at(i + 1));
  }

  rep(i, N) if (t.at(i) == x) if (d1 == -1) { d1 = i; }
  else {
    d2 = i;
    break;
  }

  if (K <= d1)
    cout << t.at(K) + 1 << endl;
  else
    cout << t.at(((K - d1) % (d2 - d1)) + d1) + 1 << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N, K, x, d1 = -1, d2 = -1;
  set<int> s;
  s.insert(0);

  cin >> N >> K;
  vector<int> A(N), t(N, 0);
  rep(i, N) cin >> A.at(i);

  rep(i, N - 1) {
    t.at(i + 1) = A.at(t.at(i)) - 1;
    if (s.count(t.at(i + 1))) {
      x = t.at(i + 1);
      break;
    }
    s.insert(t.at(i + 1));
  }

  rep(i, N) if (t.at(i) == x) if (d1 == -1) { d1 = i; }
  else {
    d2 = i;
    break;
  }

  if (K <= d1)
    cout << t.at(K) + 1 << endl;
  else
    cout << t.at(((K - d1) % (d2 - d1)) + d1) + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <map>
#include <stdio.h>
using namespace std;

typedef long long ll;

map<ll, ll> M;
ll a[222222], p[222222];

int main(void) {
  ll n, k, i, cnt = 0;

  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);

  ++k;
  p[++cnt] = 1;
  M[1] = 1;

  ll turn = 0, rep = 0;
  while (1) {
    p[cnt + 1] = a[p[cnt]];
    ++cnt;

    if (cnt == k) {
      printf("%lld", p[k]);
      return 0;
    }

    if (M[p[cnt]] == 0)
      M[p[cnt]] = cnt;
    else {
      ll v = M[p[cnt]];
      turn = v - 1, rep = cnt - v;
    }

    if (rep)
      break;
  }

  if (k <= turn)
    printf("%lld", p[k]);
  else {
    k -= turn;
    printf("%lld", p[turn + (k % rep == 0 ? rep : k % rep)]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

typedef long long ll;

ll a[222222], log[222222];

int main(void) {
  ll n, k, i, cnt = 0;

  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);

  ++k;
  log[++cnt] = 1;

  ll turn = 0, rep = 0;
  while (1) {
    log[cnt + 1] = a[log[cnt]];
    ++cnt;

    if (cnt == k) {
      printf("%lld", log[k]);
      return 0;
    }

    for (i = 1; i < cnt; i++)
      if (log[i] == log[cnt]) {
        turn = i - 1;
        rep = cnt - i;
        break;
      }

    if (rep)
      break;
  }

  if (k <= turn)
    printf("%lld", log[k]);
  else {
    k -= turn;
    printf("%lld", log[turn + (k % rep == 0 ? rep : k % rep)]);
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#define MAXN 800010
using namespace std;
int n;
long long k;
int tel[MAXN], seq[MAXN];
bool vis[MAXN];
int lastpos[MAXN];
int loopseq[MAXN];
int s;

int check(int head, int tail) {
  int ring = tail - head - 1;
  int to = seq[tail];
  for (int i = 0; i < ring; i++) {
    to = tel[to];
    if (to != seq[head + i + 1])
      return -1;
  }
  s = head;
  for (int i = head; i < tail; i++) {
    loopseq[i - head + 1] = seq[i];
  }
  return ring + 1;
}
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &tel[i]);

  int mod = 0;
  int loop = 0;
  int i = 1;
  bool flag = 0;
  while (1) {
    seq[++loop] = i;
    if (k + 1 == loop) {
      flag = 1;
      break;
    }
    if (vis[i]) {
      mod = check(lastpos[i], loop);
      if (mod != -1)
        break;
    }
    vis[i] = 1;
    lastpos[i] = loop;
    i = tel[i];
  }

  if (flag)
    return !printf("%d", seq[loop]);
  if (k + 1 < s)
    return !printf("%d", seq[k]);
  int pos = ((k - s + 2) % mod == 0) ? mod : ((k - s + 2) % mod);
  return !printf("%d", loopseq[pos]);
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#define MAXN 20001
int n;
long long k;

struct Node {
  int data;
  int next;
} node[MAXN];

int findLoop() {
  int start = node[1].data;
  int tmp = 0, count = 0;
  int index = 1;
  while (tmp != start) {
    index = node[index].next;
    tmp = node[index].data;
    count++;
  }
  return count;
}

int main() {
  scanf("%d %lld", &n, &k);
  node[n] = {0};
  int temp, result;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &temp);
    node[i].data = i;
    node[i].next = temp;
  }
  int loop = findLoop();
  if (k >= loop) {
    k = k % loop; // k是最终进行的传递次数
  }
  int index = 1; // 从第一个town出发
  for (int i = 0; i < k; i++) {
    index = node[index].next;
    result = node[index].data;
  }
  printf("%d", result);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pbds                                                                   \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define mod 1000000007
#define deb(x) cerr << "\\n" << #x << "=" << x << "\\n";
#define deb2(x, y)                                                             \\
  cerr << "\\n" << #x << "=" << x << "\\n" << #y << "=" << y << "\\n";
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

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

  int n, k;
  cin >> n >> k;

  vector<int> v(n);
  for (int &i : v)
    cin >> i;
  int cnt = 0;
  int cur = 1;
  if (k <= n) {
    for (int i = 0; i < k; i++) {
      cur = v[cur - 1];
    }
    cout << cur;
    return 0;
  }
  bool vis[n + 1];
  memset(vis, 0, sizeof(vis));
  while (!vis[cur]) {
    vis[cur] = 1;
    cur = v[cur - 1];
    cnt++;
  }
  if (cur != 1) {
    int temp = cnt;
    cnt = 0;
    memset(vis, 0, sizeof(vis));
    while (!vis[cur]) {
      vis[cur] = 1;
      cur = v[cur - 1];
      cnt++;
    }
    k -= (temp - cnt);
  }
  k = k % cnt;
  for (int i = 0; i < k; i++) {
    cur = v[cur - 1];
  }
  cout << cur;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pbds                                                                   \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define mod 1000000007
#define deb(x) cerr << "\\n" << #x << "=" << x << "\\n";
#define deb2(x, y)                                                             \\
  cerr << "\\n" << #x << "=" << x << "\\n" << #y << "=" << y << "\\n";
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

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

  int n, k;
  cin >> n >> k;

  vector<int> v(n);
  for (int &i : v)
    cin >> i;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (v[i] != i + 1)
      cnt++;
  int cur = 1;
  k = k % cnt;
  for (int i = 0; i < k; i++) {
    cur = v[cur - 1];
  }

  cout << cur;

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

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

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll &x : a) {
    cin >> x;
    --x;
  }
  vector<ll> path;
  path.push_back(0);
  ll now = 0;
  ll count = 0;
  ll loop_start_index;
  set<ll> s;
  while (true) {
    if (s.find(a.at(now)) != s.end()) {
      auto loop_start = find(path.begin(), path.end(), a.at(now));
      loop_start_index = distance(path.begin(), loop_start);
      break;
    }
    now = a.at(now);
    path.push_back(now);
    s.insert(now);
    if (++count == k) {
      cout << ++now << endl;
      return 0;
    }
  }

  ll mod = path.size() - loop_start_index;
  ll remain = k - count;
  --remain;
  cout << ++path.at(loop_start_index + (remain % mod)) << endl;
}


Error: TLE

Fixed submission:

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

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll &x : a) {
    cin >> x;
    --x;
  }
  vector<ll> path;
  path.push_back(0);
  ll now = 0;
  ll count = 0;
  ll loop_start_index;

  while (true) {
    auto loop_start = find(path.begin(), path.end(), a.at(now));
    if (loop_start != path.end()) {
      loop_start_index = distance(path.begin(), loop_start);
      break;
    }
    now = a.at(now);
    path.push_back(now);
    if (++count == k) {
      cout << ++now << endl;
      return 0;
    }
  }

  ll mod = path.size() - loop_start_index;
  ll remain = k - count;
  --remain;
  cout << ++path.at(loop_start_index + (remain % mod)) << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

/*
AtCoderBeginnerContest167
Problem D - Teleporter /
Time Limit: 2 sec / Memory Limit: 1024 MB
Score:
400
points
*/

#include <iostream>
#include <vector>

int main() {
  int N;
  long K;
  std::vector<int> A;
  A.push_back(0);
  std::cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int currA;
    std::cin >> currA;
    A.push_back(currA);
  }
  std::vector<int> vis(N + 1, -1);
  std::vector<int> loc;
  int T = 0;
  int pos = 1;
  while (vis[pos] == -1) {
    vis[pos] = T;
    loc.push_back(pos);
    T++;
    pos = A[pos];
  }
  int prevT = vis[pos];
  if (K >= prevT) {
    long slack = K - prevT;
    int period = T - prevT;
    slack %= period;
    K = prevT + slack;
  }
  std::cout << loc[K] << "\\n";
}


Error: 0

Fixed submission:

/*
AtCoderBeginnerContest167
Problem D - Teleporter /
Time Limit: 2 sec / Memory Limit: 1024 MB
Score:
400
points
*/

#include <iostream>
#include <vector>

int main() {
  int N;
  long K;
  std::vector<int> A;
  A.push_back(0);
  std::cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int currA;
    std::cin >> currA;
    A.push_back(currA);
  }
  std::vector<int> vis(N + 1, -1);
  std::vector<int> loc;
  int T = 0;
  int pos = 1;
  while (vis[pos] == -1) {
    vis[pos] = T;
    loc.push_back(pos);
    T++;
    pos = A[pos];
  }
  int prevT = vis[pos];
  if (K >= prevT) {
    int slack = K - prevT;
    int period = T - prevT;
    slack %= period;
    K = prevT + slack;
  }
  std::cout << loc[K] << "\\n";
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long N;
  long K;
  cin >> N >> K;
  vector<long> A(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<long> st(N + 1, -1);
  vector<long> pos(N + 1, -1);
  long cnt = 0, now = 1;
  pos[0] = 1;
  st[1] = 0;
  bool path = false;
  while (true) {
    now = A[now];
    cnt++;
    if (cnt == K) {
      path = true;
      break;
    }
    if (st[now] != -1)
      break;
    pos[cnt] = now;
    st[now] = cnt;
  }
  if (path) {
    cout << now << endl;
  } else {
    long x = st[now] + (K - st[now]) % (cnt - st[now]);
    if (x < 0)
      x += cnt - st[now];
    cout << pos[x] << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  long K;
  cin >> N >> K;
  vector<int> A(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  vector<int> st(N + 1, -1);
  vector<int> pos(N + 1, -1);
  int cnt = 0, now = 1;
  pos[0] = 1;
  st[1] = 0;
  cnt++;
  while (true) {
    now = A[now];
    cnt++;
    if (st[now] != -1)
      break;
    pos[cnt] = now;
    st[now] = cnt;
  }
  cout << pos[(K - st[now]) % (cnt - st[now] - 1)] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
int nxt[65][200010] = {};
void solve() {
  ll N, K;
  cin >> N >> K;
  ll A[200010];
  rep(i, 0, N) {
    cin >> A[i];
    A[i]--;
  }
  rep(i, 0, N) { nxt[0][i] = A[i]; }

  rep(k, 0, 62) {
    rep(i, 0, N) {
      if (nxt[k][i] == -1) {
        nxt[k + 1][i] = -1;
      } else {
        nxt[k + 1][i] = nxt[k][nxt[k][i]];
      }
    }
  }
  ll pos = 0;

  rrep(b, 62, 0) {
    if ((K >> b) & 1) {
      pos = nxt[b][pos];
    }
  }
  print(pos + 1);
}

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


Error: 0

Fixed submission:

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
int nxt[62][200010] = {};
void solve() {
  ll N, K;
  cin >> N >> K;
  ll A[200010];
  rep(i, 0, N) {
    cin >> A[i];
    A[i]--;
  }
  rep(i, 0, N) { nxt[0][i] = A[i]; }

  rep(k, 0, 62) {
    rep(i, 0, N) {
      if (nxt[k][i] == -1) {
        nxt[k + 1][i] = -1;
      } else {
        nxt[k + 1][i] = nxt[k][nxt[k][i]];
      }
    }
  }
  ll pos = 0;

  rrep(b, 62, 0) {
    if ((K >> b) & 1) {
      pos = nxt[b][pos];
    }
  }
  print(pos + 1);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(n) begin(n), end(n)
using ll = long long;
const ll INF = 1e18;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, 0, N) cin >> A[i];

  int index = 0;
  vector<int> history_vec;
  history_vec.reserve(1e6);
  set<int> history_set;
  ll loop_begin_index, loop_end_index;
  bool flag_break = false;
  for (ll i = 0; i < min(K, ll(N)); ++i) {
    if (history_set.find(index) != history_set.end()) {
      for (ll j = history_vec.size() - 1; j >= 0; --j) {
        if (index == history_vec[j]) {
          loop_begin_index = j;
          loop_end_index = i;
          flag_break = true;
          break;
        }
      }
    }
    history_vec.push_back(index);
    history_set.insert(index);
    index = A[index] - 1; // next
    if (flag_break)
      break;
  }

  if (K <= N) {
    cout << index + 1 << endl;
  } else {
    if (loop_begin_index == loop_end_index) {
      cout << history_vec[loop_begin_index] << endl;
    } else {
      cout << history_vec[loop_begin_index +
                          (K - loop_begin_index) %
                              (loop_end_index - loop_begin_index)] +
                  1
           << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(n) begin(n), end(n)
using ll = long long;
const ll INF = 1e18;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, 0, N) cin >> A[i];

  int index = 0;
  vector<int> history_vec;
  history_vec.reserve(1e6);
  set<int> history_set;
  ll loop_begin_index, loop_end_index;
  bool flag_break = false;
  for (ll i = 0; i < min(K, ll(N)); ++i) {
    if (history_set.find(index) != history_set.end()) {
      for (ll j = history_vec.size() - 1; j >= 0; --j) {
        if (index == history_vec[j]) {
          loop_begin_index = j;
          loop_end_index = i;
          flag_break = true;
          break;
        }
      }
    }
    history_vec.push_back(index);
    history_set.insert(index);
    index = A[index] - 1; // next
    if (flag_break)
      break;
  }

  if (K < N) {
    cout << index + 1 << endl;
  }

  if (loop_begin_index == loop_end_index) {
    cout << history_vec[loop_begin_index] << endl;
  } else {
    cout << history_vec[loop_begin_index +
                        (K - loop_begin_index) %
                            (loop_end_index - loop_begin_index)] +
                1
         << endl;
  }

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

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<int> a(N);

  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i]--;
  }
  long now = 0;
  vector<int> v(N);
  while (K > 0) {
    now = a[now];
    K--;
    //  cout<<now+1<<endl;
    if (v[now] == 1) {
      v[now]++;
      break;
    } else
      v[now]++;
  }
  // cout<<"K"<<K<<endl;
  long loop = 0;
  while (K > 0) {
    now = a[now];
    K--;
    loop++;
    //  cout<<now+1<<endl;
    if (v[now] == 2) {
      v[now]++;
      break;
    } else
      v[now]++;
  }
  // cout<<"K"<<K<<endl;
  // cout<<loop<<endl;
  if (loop > 0)
    K %= loop;
  while (K > 0) {
    now = a[now];
    K--;
    //  cout<<now+1<<endl;
  }
  cout << now + 1 << endl;
}


Error: 0

Fixed submission:

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<int> a(N);

  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i]--;
  }
  long now = 0;
  vector<int> v(N);
  while (K > 0) {
    now = a[now];
    K--;
    //  cout<<now+1<<endl;
    if (v[now] == 1) {
      v[now]++;
      break;
    } else
      v[now]++;
  }
  // cout<<"K"<<K<<endl;
  long loop = 0;
  while (K > 0) {
    now = a[now];
    K--;
    loop++;
    //  cout<<now+1<<endl;
    if (v[now] == 2) {
      v[now]++;
      break;
    } else
      v[now]++;
  }
  // cout<<"K"<<K<<endl;
  // cout<<loop<<endl;
  K %= loop;
  while (K > 0) {
    now = a[now];
    K--;
    //  cout<<now+1<<endl;
  }
  cout << now + 1 << endl;
}