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

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define MP make_pair
#define PB push_back
#define F first
#define S second

int main() {
  // your code goes here
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  ll a[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vector<ll> next(n + 1, -1);
  vector<ll> v;
  ll cur = 1;
  while (next[cur] == -1) {
    next[cur] = a[cur];
    cur = next[cur];
    v.PB(cur);
  }
  ll cyclebg = cur;
  ll len = 1;
  vector<ll> cycle;
  cycle.PB(cyclebg);
  while (next[cur] != cyclebg) {
    cur = next[cur];
    cycle.PB(cur);
    len++;
  }
  ll done = v.size() - len;
  ll rem = (k - done) % len;
  if (k <= v.size()) {
    cout << v[k - 1];
  } else {
    cur = cyclebg;
    while (rem > 0) {
      cur = next[cur];
      rem--;
    }
    cout << cur;
    // cout<<cycle[rem];
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define MP make_pair
#define PB push_back
#define F first
#define S second

int main() {
  // your code goes here
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  ll a[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> next(n, -1);
  vector<ll> v;
  ll cur = 1;
  while (next[cur] == -1) {
    next[cur] = a[cur];
    cur = next[cur];
    v.PB(cur);
  }
  ll cyclebg = cur;
  ll len = 1;
  vector<ll> cycle;
  cycle.PB(cyclebg);
  while (next[cur] != cyclebg) {
    cur = next[cur];
    cycle.PB(cur);
    len++;
  }
  ll done = v.size() - len;
  ll rem = (k - done) % len;
  if (k <= v.size()) {
    cout << v[k - 1];
  } else {
    cur = cyclebg;
    while (rem > 0) {
      cur = next[cur];
      rem--;
    }
    cout << cur;
    // cout<<cycle[rem];
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long a, b;
  cin >> a >> b;
  long c = 1;
  bool B = true;
  vector<int> v(a);
  for (int i = 0; i < a; i++)
    cin >> v[i];
  vector<int> v2(a, -1);
  v2[0] = 0;
  for (long i = 0; i < b; i++) {
    c = v[c - 1];
    if (B) {
      if (v2[c - 1] == -1)
        v2[c - 1] = i + 1;
      else {
        long e = i + 1 - v2[c - 1];
        long f = (b - i - 1) / e;
        i += e * f;
        B = false;
      }
    }
  }
  cout << c << endl;
}

Error: 0

Fixed submission:

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

int main() {
  long long n, k;
  long long z = 0;
  cin >> n >> k;
  vector<int> v(n);
  vector<int> v2(n, 0);
  vector<int> roop(n + 3);
  long long a = 1;
  int aa = 0;
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < k; i++) {
    v2[a - 1]++;
    roop[i] = a;
    if (v2[a - 1] == 2) {
      long long d = k - i;
      for (int l = 0; l < k; l++) {
        if (roop[l] == a) {
          z = l;
          break;
        }
      }
      long long y = d % (i - z - 1);
      if (y != 0)
        cout << roop[z + y - 1] << endl;
      else
        cout << roop[z + y + i - z - 1] << endl;
      aa++;
      break;
    }
    a = v[a - 1];
  }
  if (aa == 0)
    cout << a << endl;
  /*for(int i=0;i<k;i++){
      cout << roop[i] << endl;
  }*/
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> ipair;
typedef pair<ll, ll> lpair;
typedef tuple<int, int, int> ituple;

// const int INF = INT_MAX;
// const ll INF = LLONG_MAX;
// const int MOD = ((int)1e9 + 7);
// const ld EPS = (1e-10);
#define PI acosl(-1)
#define MAX_N (200000 + 2)
int n, a[MAX_N];
int firstVisited[MAX_N];
ll k;

void exec() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  int ans = 0, i = 1, current = 1;
  firstVisited[1] = i;
  bool isFindLoop = false;
  while (true) {
    if (k <= 0) {
      ans = current;
      break;
    }

    k--;
    i++;
    current = a[current - 1];
    // printf("loop %d: current = %d, k = %lld\\n", i, current, k);

    if (!isFindLoop && firstVisited[current]) {
      ll loop = i - firstVisited[current];
      if (loop > 0) {
        // printf("loop = %lld, k = %lld\\n", loop, k);
        k %= loop;
        // printf("after k = %lld\\n", k);
      }

      isFindLoop = true;
    }

    firstVisited[current] = i;
  }

  cout << ans << endl;
}

void solve() {
  int t = 1;
  // scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    exec();
  }
}

int main() {
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> ipair;
typedef pair<ll, ll> lpair;
typedef tuple<int, int, int> ituple;

// const int INF = INT_MAX;
// const ll INF = LLONG_MAX;
// const int MOD = ((int)1e9 + 7);
// const ld EPS = (1e-10);
#define PI acosl(-1)
#define MAX_N (100000 + 2)
int n, a[MAX_N];
int firstVisited[MAX_N];
ll k;

void exec() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  int ans = 0, i = 1, current = 1;
  firstVisited[1] = i;
  bool isFindLoop = false;
  while (true) {
    if (k <= 0) {
      ans = current;
      break;
    }

    k--;
    i++;
    current = a[current - 1];
    // printf("loop %d: current = %d, k = %lld\\n", i, current, k);

    if (!isFindLoop && firstVisited[current]) {
      ll loop = i - firstVisited[current];
      if (loop > 0) {
        // printf("loop = %lld, k = %lld\\n", loop, k);
        k %= loop;
        // printf("after k = %lld\\n", k);
      }

      isFindLoop = true;
    }

    firstVisited[current] = i;
  }

  cout << ans << endl;
}

void solve() {
  int t = 1;
  // scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    exec();
  }
}

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

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

using namespace std;

template <class T, class U> istream &operator>>(istream &is, pair<T, U> &pair) {
  is >> pair.first >> pair.second;
  return is;
}

template <class T> istream &operator>>(istream &is, vector<T> &collection) {
  for (auto &item : collection)
    is >> item;
  return is;
}

void solution() {
  size_t n, k;
  cin >> n >> k;
  vector<int> as(n);
  cin >> as;
  int t = 1;
  vector<int> distance(n);
  int t_prev = 1;
  int d = 0;
  while (!distance[t - 1]) {
    if (k == 0) {
      cout << t << '\\n';
      return;
    }
    distance[t - 1] = d;
    t_prev = t;
    t = as[t - 1];
    k -= 1;
    d += 1;
  }
  int loop_length = distance[t_prev - 1] - distance[t - 1] + 1;
  // cout << "loop: " << loop_length << '\\n';
  if (loop_length > 1) {
    k = k % loop_length;
    for (size_t i = 0; i < k; ++i) {
      t = as[t - 1];
    }
  }
  cout << t << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  solution();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

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

using namespace std;

template <class T, class U> istream &operator>>(istream &is, pair<T, U> &pair) {
  is >> pair.first >> pair.second;
  return is;
}

template <class T> istream &operator>>(istream &is, vector<T> &collection) {
  for (auto &item : collection)
    is >> item;
  return is;
}

void solution() {
  size_t n, k;
  cin >> n >> k;
  vector<int> as(n);
  cin >> as;
  int t = 1;
  if (k > n) {
    vector<bool> visited(n);
    int t_prev = 1;
    while (!visited[t - 1]) {
      visited[t - 1] = true;
      t_prev = t;
      t = as[t - 1];
      k -= 1;
    }
    int loop_length = t_prev - t;
    k = k % loop_length;
  }
  for (size_t i = 0; i < k; ++i) {
    t = as[t - 1];
  }
  cout << t << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  solution();
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const long long mod = 1e9 + 7;
//////////////////////////////////

int main() {
  long long n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  if (a[0] == 0) {
    cout << 1 << endl;
    return 0;
  }

  vector<int> x(n, -1);
  long long now = 0;
  for (long long i = 0; i < k; i++) {
    if (x[now] != -1) {
      long long d = i - x[now];
      long long p = (k - i) / d;
      if (p >= 1) {
        i += (p - 1) * d;
      }
    }
    x[now] = i;
    now = a[now];
  }

  cout << now + 1 << endl;

  return 0;
}

// EOF

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const long long mod = 1e9 + 7;
//////////////////////////////////

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

  vector<int> x(n, -1);
  int now = 0;
  for (long long i = 0; i < k; i++) {
    if (x[now] != -1) {
      long long d = i - x[now];
      long long p = (k - i) / d;
      i += (p - 1) * d;
    }
    x[now]++;
    now = a[now];
  }

  cout << now + 1 << endl;

  return 0;
}

// EOF
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N, K;
  cin >> N >> K;
  K++;
  long long loop;
  map<long long, long long> isFirst;
  vector<long long> A(N);
  for (loop = 0; loop < N; loop++) {
    cin >> A[loop];
  }
  long long now = 1, nowtime = 1;
  while (isFirst.count(now) == 0) {
    isFirst[now] = nowtime;
    now = A[now - 1];
    nowtime++;
  }
  long long now2 = now;
  vector<long long> Loops(nowtime - isFirst[now]);
  for (loop = 0; loop < nowtime - isFirst[now]; loop++) {
    Loops[loop] = now2;
    now2 = A[now2 - 1];
  }
  K -= (isFirst[now] - 1);
  if (K <= 0) {
    K += (isFirst[now] - 1);
    now = 1;
    for (loop = 1; loop < K; loop++) {
      now = A[now - 1];
    }
    cout << now << endl;
  } else {
    K = K % (nowtime - isFirst[now]);
    if (K) {
      cout << Loops[K - 1] << endl;
    } else {
      cout << Loops[nowtime - isFirst[now] - 1] << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  long long N, K;
  cin >> N >> K;
  K++;
  long long loop;
  map<long long, long long> isFirst;
  vector<long long> A(N);
  for (loop = 0; loop < N; loop++) {
    cin >> A[loop];
  }
  long long now = 1, nowtime = 1;
  while (isFirst.count(now) == 0) {
    isFirst[now] = nowtime;
    now = A[now - 1];
    nowtime++;
  }
  long long now2 = now;
  if (nowtime - isFirst[now] > 1) {
    vector<long long> Loops(nowtime - isFirst[now]);
    for (loop = 0; loop < nowtime - isFirst[now]; loop++) {
      Loops[loop] = now2;
      now2 = A[now2 - 1];
    }
    K -= (isFirst[now] - 1);
    K = K % (nowtime - isFirst[now]);
    if (K) {
      cout << Loops[K - 1] << endl;
    } else {
      cout << Loops[nowtime - isFirst[now] - 1] << endl;
    }
  } else {
    cout << A[now - 1] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

int teleport[200005];
int step[200005];
int destination[200005];

int main() {
  int N;
  ll K;
  scanf("%d%lld", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d", &teleport[i]);
    teleport[i]--;
  }
  memset(step, -1, sizeof(step));
  int number = 1;
  int position = 0;
  destination[number - 1] = position + 1;
  while (step[position] < 0) {
    step[position] = number++;
    position = teleport[position];
    destination[number - 1] = position + 1;
  }
  number--;
  int loopPoint = position;
  int nonLoopCount = step[position] - 1;
  int cyclesCount = number - nonLoopCount;
  if (K > nonLoopCount) {
    K = (K - nonLoopCount) % cyclesCount + nonLoopCount;
  }
  printf("%d\\n", destination[K]);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

int teleport[100005];
int step[100005];
int destination[100005];

int main() {
  int N;
  ll K;
  scanf("%d%I64d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d", &teleport[i]);
    teleport[i]--;
  }
  memset(step, -1, sizeof(step));
  int number = 1;
  int position = 0;
  destination[number - 1] = teleport[position] + 1;
  while (step[position] < 0) {
    step[position] = number++;
    position = teleport[position];
    destination[number - 1] = position + 1;
  }
  number--;
  int loopPoint = position;
  int nonLoopCount = step[position] - 1;
  int cyclesCount = number - nonLoopCount;
  if (K > nonLoopCount) {
    K = (K - nonLoopCount) % cyclesCount + nonLoopCount;
  }
  printf("%d\\n", destination[K]);
  // 2 - 3 - 4 - 5 - 6 - 3
  // 2
  // 3 => 3 - 2. 1
  // 10 = 10 - 2 % 4 + 2
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int n, k, t = 1, cnt = 0, mid, r;
int a[200005], now[200005], visited[200005];

inline int get_mod(int aa, int bb) {
  if (aa % bb == 0)
    return bb;
  else
    return aa % bb;
}

signed main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  for (int i = 1; i <= n * n; i++) {
    now[++cnt] = t;

    if (visited[t] == 0)
      visited[t] = cnt;
    else {
      mid = visited[t];
      r = cnt;
      break;
    }
    t = a[t];
  }
  r--;
  k++;

  if (k <= mid)
    cout << now[k] << endl;

  else {
    k = k - mid;
    k = get_mod(k, r - mid + 1);
    k = k + mid;
    cout << now[k] << endl;
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

int n, k, a[200010], i, cnt;
int *b = new int[20000000000];

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

  cin >> n >> k;

  for (i = 1; i <= n; i++)
    cin >> a[i];

  for (i = n; a[a[i]] != a[n]; i = a[i]) {
    cnt++;
    b[cnt] = a[i];
  }

  cnt++;
  b[cnt] = a[i];

  int ans = k % cnt + 1;
  cout << b[ans] << "\\n";

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int N;
  long long K;
  cin >> N >> K;
  int A[N];
  rep(i, N) cin >> A[i];
  //

  int m[N + 1];
  rep(i, N) m[i + 1] = A[i];

  pair<int, bool> visit[N + 1];
  rep(i, N + 1) visit[i] = make_pair(0, false);

  int now = 1;

  int roop = 1;
  int count = 1;
  rep(i, K) {
    if (visit[now].second == true) {
      roop = i - visit[now].first;
      count = i;
      break;
    }
    visit[now] = make_pair(i, true);
    now = m[now];
  }

  K -= (long long)count;
  roop = K % (long long)roop;

  rep(i, roop) now = m[now];

  cout << now << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int N;
  long long K;
  cin >> N >> K;
  int A[N];
  rep(i, N) cin >> A[i];
  //

  int m[N + 1];
  rep(i, N) m[i + 1] = A[i];

  pair<int, bool> visit[N + 1];
  rep(i, N + 1) visit[i] = make_pair(0, false);

  int now = 1;

  int roop;
  int count;
  rep(i, K) {
    if (visit[now].second == true) {
      roop = i - visit[now].first;
      count = i;
      break;
    }
    visit[now] = make_pair(i, true);
    now = m[now];
  }

  K -= (long long)count;
  roop = K % (long long)roop;

  rep(i, roop) now = m[now];

  cout << now << endl;

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

#include <iostream>
#include <vector>
using namespace std;

using ll = long long;
ll n, k, c, repeat_time, pre_repeat_time;
int main() {
  bool repeat_exist = false;
  std::cin >> n >> k;
  std::vector<ll> vec(n + 1, 0);
  std::vector<long long int> b(n + 1, -1);
  for (ll i = 1; i <= n; i++) {
    cin >> vec[i];
  }
  ll j = 1;
  c = 1;
  while (c <= k) {
    if (b[j] != -1) {
      repeat_time = c - b[j];
      pre_repeat_time = b[j] - 1;
      repeat_exist = true;
      break;
    }
    b[j] = c;
    j = vec[j];
    c++;
  }
  if (repeat_exist) {
    ll x = (k - pre_repeat_time) % repeat_time;
    for (ll p = 0; p < x; p++) {
      j = vec[j];
    }
    std::cout << j << std::endl;
  } else {
    std::cout << j << std::endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using ll = long long;

int n;
ll k, t;
std::vector<int> vec;
std::map<ll, ll> r;

int main() {
  ll b;
  std::cin >> n >> k;
  vec.assign(n + 1, 0);
  for (ll i = 1; i <= n; i++) {
    std::cin >> t;
    vec[i] = t;
  }
  ll q;
  ll x = 1;
  r[x]++;
  for (int p = 0; p < n; p++) {
    x = vec[x];
    r[x]++;
    if (r[x] == 2) {
      b = p;
      q = x;
      break;
    }
  }
  ll y = k % b;
  ll c = q;
  for (int i = 0; i < y; i++) {
    c = vec[c];
  }
  std::cout << c << std::endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, goal, pla = 1;
  int64_t K, KK;
  cin >> N >> K;
  KK = K;
  int arr[N];
  map<int, int> mp;
  int cnt = 0;
  int tele;
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }
  while (K > 0) {
    if (!(mp.count(pla))) {
      mp[pla] = cnt;
      pla = arr[pla - 1];
      cnt++;
      K--;
    } else {
      tele = cnt - mp[pla];
      KK = KK - mp[pla];
      KK = KK % tele;
      while (KK > 0) {
        pla = arr[pla - 1];
        KK--;
      }
      break;
    }
  }
  cout << pla << endl;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int N, goal, pla = 1;
  int64_t K;
  cin >> N >> K;
  int arr[N];
  map<int, int> mp;
  int cnt = 0;
  int tele;
  for (int i = 0; i < N; i++) {
    if (!(mp.count(pla))) {
      cin >> arr[pla - 1];
      mp[pla] = cnt;
      pla = arr[pla - 1];
      cnt++;
    } else {
      tele = cnt - mp[pla];
      K -= i;
      break;
    }
  }
  K = K % tele;
  while (K > 0) {
    pla = arr[pla - 1];
    K--;
  }
  cout << pla << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define rep2(i, n, m) for (long long i = n; i <= m; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  rep(i, n) A[i]--;
  vector<ll> B(n);
  ll loop = 0;
  rep2(i, 1, n) {
    if (i == 1) {
      B[i] = A[0];
    } else {
      B[i] = A[B[i - 1]];
    }
  }
  rep2(i, 1, n - 1) {
    loop++;
    if (B[n] == B[n - i])
      break;
  }
  ll ko = (k - n) % loop;
  if (k <= n)
    cout << B[k] + 1 << endl;
  else
    cout << B[n + ko - loop] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define rep2(i, n, m) for (long long i = n; i <= m; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  rep(i, n) A[i]--;
  vector<ll> B(n);
  ll loop = 0;
  rep2(i, 1, n + 1) {
    if (i == 1) {
      B[i] = A[0];
    } else {
      B[i] = A[B[i - 1]];
    }
    loop++;
    if (B[i] == 0)
      break;
  }
  ll ans = k % loop;
  cout << B[ans] + 1 << endl;
  return 0;
}
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 (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

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

  int c = 1, l = 0;
  int v = 1;
  while (ord.at(v) == -1) {
    ord.at(v) = s.size();
    s.push_back(v);
    v = A.at(v - 1);
  }

  c = s.size() - ord.at(v);
  l = ord.at(v);

  if (K < l)
    cout << s.at(K) << endl;
  else {
    K -= l;
    K %= c;
    cout << s.at(l + K) << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

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

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

  for (int i = 0; i < N; i++) {
    cin >> a;
    A.at(i) = a - 1;
  }

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

  int c = 1, l = 0;
  int v = 1;
  while (ord.at(v) == -1) {
    ord.at(v) = s.size();
    s.push_back(v);
    v = A.at(v - 1);
  }

  c = s.size() - ord.at(v);
  l = ord.at(v);

  if (K < l)
    cout << s.at(K) << endl;
  else {
    K -= l;
    K %= c;
    cout << s.at(l + K) << endl;
  }

  return 0;
}