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

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int64_t N, K;
  cin >> N >> K;
  int32_t Ai[200001];
  int64_t Aiused[200001];
  int32_t realstate[200000];

  for (int i = 1; i <= N; ++i) {
    cin >> Ai[i];
    Aiused[i] = -1;
    realstate[i - 1] = 0;
  }
  int64_t mod = K + 1;
  int64_t offset = 0;

  Aiused[1] = 0;
  realstate[0] = 1;
  int32_t jump = Ai[1];
  int64_t i;
  for (i = 1; i <= K; i++) {
    if (Aiused[jump] < 0) {
      Aiused[jump] = i;
      realstate[i] = jump; /*current city*/
      jump = Ai[jump];     /*next city*/
    } else {
      offset = Aiused[jump];
      mod = i - offset;
      break;
    }
    //        cout<<i<<"city"<<realstate[i]<<"next"<<jump<<endl;
  }
  /*
  cout<<Ai[jump]<<endl;
  cout<<i<<endl;
  cout<<mod<<endl;
  cout<<offset<<endl;
  */
  uint64_t valnum = offset + ((K - offset) % mod);

  cout << realstate[valnum] << endl;
}


Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int64_t N, K;
  cin >> N >> K;
  int32_t Ai[100001];
  int64_t Aiused[100001];
  int32_t realstate[100000];

  for (int i = 1; i <= N; ++i) {
    cin >> Ai[i];
    Aiused[i] = -1;
    realstate[i - 1] = 0;
  }
  int64_t mod = K + 1;
  int64_t offset = 0;

  Aiused[1] = 0;
  realstate[0] = 1;
  int32_t jump = Ai[1];
  int64_t i;
  for (i = 1; i <= K; i++) {
    if (Aiused[jump] < 0) {
      Aiused[jump] = i;
      realstate[i] = jump; /*current city*/
      jump = Ai[jump];     /*next city*/
    } else {
      offset = Aiused[jump];
      mod = i - offset;
      break;
    }
    //        cout<<i<<"city"<<realstate[i]<<"next"<<jump<<endl;
  }
  /*
  cout<<Ai[jump]<<endl;
  cout<<i<<endl;
  cout<<mod<<endl;
  cout<<offset<<endl;
  */
  uint64_t valnum = offset + ((K - offset) % mod);

  cout << realstate[valnum] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error


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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int c = 1, l = 0;
  vector<int> s;
  vector<int> ord(N + 1, -1);
  {
    int 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] << endl;
  else {
    K -= l;
    K %= c;
    cout << s[l + K] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:


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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  vector<int> c;
  int t = 1;
  for (int i = 1; i <= N + 10; i++) {
    c.push_back(t);
    t = A[t];
  }
  vector<int> h(N + 1);
  bool f = true;
  int a = 0;
  int cy = 0;
  int l;
  while (f) {
    int i = c[a];
    if (h[i] == 0) {
      h[i] = a;
    } else {
      cy = i;
      l = a;
      f = false;
    }
    a++;
  }

  int k = K % (l - cy);
  k += a;
  cout << c[k] << endl;

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

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

const int mxN = 2e5 + 5;
int pointer[mxN];
bool visited[mxN];

int main() {
  int n;
  cin >> n;
  long long k;
  cin >> k;
  ++k;
  for (int i = 1, x; i <= n; ++i)
    scanf("%d", &x), pointer[i] = x;
  vector<int> path;
  int d = 1;
  while (!visited[d] && --k) {
    path.push_back(d);
    visited[d] = true;
    d = pointer[d];
  }
  if (!k)
    return cout << d, 0;
  long long last = -1;
  for (int i = 0; i < (int)path.size(); ++i) {
    if (path[i] == d) {
      last = i;
      break;
    }
  }
  --k;
  long long moves = k % (path.size() - last);
  cout << path[last + moves] << '\\n';
}


Error: 0

Fixed submission:

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

const int mxN = 1e5 + 5;
int pointer[mxN];
bool visited[mxN];

int main() {
  int n;
  cin >> n;
  long long k;
  cin >> k;
  ++k;
  for (int i = 1, x; i <= n; ++i)
    scanf("%d", &x), pointer[i] = x;
  vector<int> path;
  int d = 1;
  while (!visited[d] && --k) {
    path.push_back(d);
    visited[d] = true;
    d = pointer[d];
  }
  if (!k)
    return cout << d, 0;
  int last = -1;
  for (int i = 0; i < (int)path.size(); ++i) {
    if (path[i] == d) {
      last = i;
      break;
    }
  }
  --k;
  int moves = k % (path.size() - last);
  cout << path[last + moves] << '\\n';
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 200000;
const int X = 64;

int main() {
  unsigned long long N, K;
  int A[MAX][X];

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

  for (int i = 1; i < X; i++) {
    for (int j = 0; j < N; j++) {
      A[j][i] = A[A[j][i - 1]][i - 1];
    }
  }

  int ans = 0;
  int cnt = X - 1;

  while (K) {
    while (K >= (1ULL << cnt)) {
      ans = A[ans][cnt];
      K -= (1ULL << cnt);
    }
    cnt--;
  }

  cout << ans + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 2000;
const int X = 64;

int main() {
  unsigned long long N, K;
  int A[MAX][X];

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

  for (int i = 1; i < X; i++) {
    for (int j = 0; j < N; j++) {
      A[j][i] = A[A[j][i - 1]][i - 1];
    }
  }

  int ans = 0;
  int cnt = X - 1;

  while (K) {
    while (K >= (1ULL << cnt)) {
      ans = A[ans][cnt];
      K -= (1ULL << cnt);
    }
    cnt--;
  }

  cout << ans + 1 << endl;

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

#include <iostream>
using namespace std;
int a[3000005], f[3000005];
int main() {
  long long m, n;
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    f[i] = 0;
  }

  long long k = 1, x = a[1];
  f[1] = 1;
  while (f[x] == 0) {
    if (k == m) {
      cout << x << endl;
      return 0;
    }
    f[x] = ++k;
    x = a[x];
  }
  long long t = k + 1 - f[x];
  k = (m - f[x] + 1) % t;

  for (long long i = 0; i < k; i++) {
    x = a[x];
  }
  cout << x << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int sum[20], a[20][20], n, m, k, c[20];
int mini;
void check(int t) {
  for (int i = 0; i < m; i++) {
    if (sum[i] < k)
      return;
  }
  if (t < mini)
    mini = t;
}
void work(int dep, int t) {
  if (t > mini)
    return;
  if (dep >= n) {
    check(t);
    return;
  }
  for (int i = 0; i < m; i++)
    sum[i] += a[dep][i];
  work(dep + 1, t + c[dep]);
  for (int i = 0; i < m; i++)
    sum[i] -= a[dep][i];
  work(dep + 1, t);
}
int main() {
  mini = 2147483647;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  }

  work(0, 0);
  if (mini == 2147483647)
    cout << -1 << endl;
  else
    cout << mini << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

int edge[200010];
int cycle[200010];

int main() {
  int n;
  long k;
  scanf("%d%ld", &n, &k);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    edge[i] = a - 1;
  }
  int now = 0, cnt = 0;
  while (true) {
    if (cnt == k) {
      printf("%d\\n", now + 1);
      return 0;
    }
    if (cycle[now] != 0) {
      k -= cnt;
      int j = k % (cnt - cycle[now] + 1);
      for (int i = 0; i < j; i++)
        now = edge[now];
      printf("%d\\n", now + 1);
      return 0;
    }
    cycle[now] = ++cnt;
    now = edge[now];
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

int indeg[200010];
int edge[200010];

int main() {
  int n;
  long k;
  scanf("%d%ld", &n, &k);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    edge[i] = a - 1;
    indeg[a - 1]++;
  }
  int now = 0, pos = -1;
  long cnt = 0;
  while (true) {
    if (cnt == k) {
      printf("%d\\n", now + 1);
      return 0;
    }
    if (indeg[now] >= 2) {
      k -= cnt;
      pos = now;
      break;
    }
    cnt++;
    now = edge[now];
  }
  cnt = 0;
  vector<int> cycle;
  while (true) {
    cycle.push_back(now + 1);
    cnt++;
    now = edge[now];
    if (now == pos)
      break;
    if (cnt == k) {
      printf("%d\\n", now + 1);
      return 0;
    }
  }
  printf("%d\\n", cycle[k % cnt]);
}
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++)
#define ll long long
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a.at(i); }
  ll c, l;
  vector<ll> s;
  vector<ll> ord(n + 1, -1);
  ll v = 0;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.push_back(v);
    v = a.at(v) - 1;
  }
  l = ord[v];
  c = s.size() - ord[v];
  if (k < l) {
    cout << s.at(k) + 1;
  } else {
    k -= l;
    k %= c;
    cout << s.at(l + k) + 1;
  }
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a.at(i); }
  vector<bool> is(n, false);
  vector<ll> s(n);
  ll i = 0, p = 0;
  is.at(0) = true;
  ll cp = a.at(0);
  while (1) {
    if (is.at(cp - 1) == true) {
      p = s.at(cp - 1);
      break;
    }
    if (is.at(cp - 1) == false) {
      is.at(cp - 1) = true;
      s.at(cp - 1) = i + 1;
    }
    cp = a.at(cp - 1);
    i++;
  }
  ll q = 0, ans = 0;
  if (p > k) {
    for (ll j = 0; j < k; j++) {
      ans = a.at(q);
      q = a.at(ans - 1);
    }
    cout << ans;
    return 0;
  } else {
    k = k - p;
    k = k % (i + 1 - p);
    for (ll j = 0; j < k; j++) {
      ans = a.at(q);
      q = a.at(ans - 1);
    }
  }
  cout << ans;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
// ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
int a[65][200001];
int main() {
  ll n, k, p = 1, x = 0;
  cin >> n >> k;
  rep(i, 1, n + 1) cin >> a[0][i];
  rep(i, 1, 65) { rep(i2, 1, n + 1) a[i][i2] = a[i - 1][a[i - 1][i2]]; }
  while (k) {
    if (k & 1)
      p = a[x][p];
    k >>= 1;
    x++;
  }
  printf("%lld\\n", p);
  return 0;
}


Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
// ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
int a[60][200001];
int main() {
  ll n, k, p = 1, x = 0;
  cin >> n >> k;
  rep(i, 1, n + 1) cin >> a[0][i];
  rep(i, 1, 61) { rep(i2, 1, n + 1) a[i][i2] = a[i - 1][a[i - 1][i2]]; }
  while (k) {
    if (k & 1)
      p = a[x][p];
    k >>= 1;
    x++;
  }
  printf("%lld\\n", p);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  const int MAX_N = 2e5 + 10;
  ll N, K;
  int A[MAX_N], B[MAX_N];

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

  B[0] = 0;
  int v = 0, cost = 0;
  while (B[A[v]] == -1) {
    B[A[v]] = ++cost;
    v = A[v];
  }

  int offset = B[A[v]];
  int mod = ++cost - offset;

  int step = K <= offset ? K : (K - offset) % mod + offset;

  int res = 0;
  for (int i = 0; i < step; ++i) {
    res = A[res];
  }
  cout << res + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  const int MAX_N = 1e5 + 10;
  ll N, K;
  int A[MAX_N], B[MAX_N];

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

  B[0] = 0;
  int v = 0, cost = 0;
  while (B[A[v]] == -1) {
    B[A[v]] = ++cost;
    v = A[v];
  }

  int offset = B[A[v]];
  int mod = ++cost - offset;

  int step = K <= offset ? K : (K - offset) % mod + offset;

  int res = 0;
  for (int i = 0; i < step; ++i) {
    res = A[res];
  }
  cout << res + 1 << endl;

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

#include <iostream>
#include <vector>
using namespace std;
const int D = 60;
const int MAX_N = 200010;
int to[D][MAX_N];

int main() {
  long long N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> to[0][i];
    to[0][i]--;
  }

  for (int i = 0; i < D - 1; i++) {
    for (int j = 0; j < N; j++) {
      to[i + 1][j] = to[i][to[i][j]];
    }
  }

  int v = 0;
  for (int i = 0; i < D; i++) {
    if (K & (1LL << i)) {
      v = to[i][v];
    }
  }
  cout << v + 1 << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
const int D = 60;
const int MAX_N = 200010;
int to[D][MAX_N];

int main() {
  long long N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> to[0][i];
    to[0][i]--;
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < N; j++) {
      to[i + 1][j] = to[i][to[i][j]];
    }
  }

  int v = 0;
  for (int i = D - 1; i >= 0; i--) {
    long long l = 1LL < i;
    if (l <= K) {
      v = to[i][v];
      K -= l;
    }
  }
  cout << v + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define ll long long
using namespace std;

vector<ll> flag(300000, -1);
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 2);
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
  }
  ll unit;
  ll now = 1;
  flag[now] = 0;
  for (ll i = 1; i <= K; i++) {
    now = A[now];
    if (flag[now] != -1) {
      unit = i - flag[now];
      break;
    } else
      flag[now] = i;
  }
  if (unit != 0) {
    ll C = (K - flag[now]) / unit;
    for (ll i = flag[now] + C * unit + 1; i <= K; i++) {
      now = A[now];
    }
  }

  cout << now << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define ll long long
using namespace std;

vector<ll> flag(300000, -1);
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 2);
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
  }
  ll unit;
  ll now = 1;
  flag[now] = 0;
  for (ll i = 1; i <= K; i++) {
    now = A[now];
    if (flag[now] != -1) {
      unit = i - flag[now];
      break;
    } else
      flag[now] = i;
  }
  ll C = (K - flag[now]) / unit;
  for (ll i = flag[now] + C * unit + 1; i <= K; i++) {
    now = A[now];
  }
  cout << now << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  ll k;
  cin >> n >> k;

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

  deque<int> b;
  vector<bool> seen(n, false);
  int cur = 0;
  while (true) {
    if (seen[cur]) {
      while (b[0] != cur) {
        k--;
        b.pop_front();

        if (k == 0) {
          cout << b[0] + 1 << endl;
          return 0;
        }
      }
      break;
    }
    b.push_back(cur);
    seen[cur] = true;
    cur = a[cur];
  }

  cout << b[k % b.size()] + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  int64_t K;

  cin >> N >> K;

  int M = N;

  vector<int> A(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] == i + 1) {
      M--;
    }
  }

  int64_t J = K % M;

  int Ai = 1;

  while (J > 0) {
    Ai = A[Ai - 1];
    J--;
  }

  cout << Ai << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  long long K;
  int A[200100];
  cin >> N >> K;

  int nextTown = 1;
  vector<bool> visited(N + 1);
  vector<int> visitedOrder(N + 1);

  int period = 0;
  int margin = 0;
  bool periodic = false;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    visited[i] = false;
  }

  if (N < K) {
    for (int i = 0; i < N; i++) {
      if (visited[nextTown]) {
        periodic = true;
        period = i - visitedOrder[nextTown];
        margin = visitedOrder[nextTown];
        break;
      }
      visited[nextTown] = true;
      visitedOrder[nextTown] = i;
      nextTown = A[nextTown];
    }
  } else {
    for (int i = 0; i < K; i++) {
      nextTown = A[nextTown];
    }
  }

  if (periodic) {
    int surplus = (K - margin) % period;
    int count = 0;
    nextTown = 1;
    while (count < surplus + margin) {
      nextTown = A[nextTown];
      count++;
    }
  }

  cout << nextTown;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int N;
  long long K;
  int A[200100];
  cin >> N >> K;

  int nextTown = 1;
  vector<int> visited;
  int size;
  int period, margin;
  bool found = false;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < K; i++) {
    size = visited.size();
    if (size > 0) {
      for (int j = 0; j < size; j++) {
        if (nextTown == visited[j]) {
          period = i - j;
          margin = j;
          found = true;
          break;
        }
      }
    }
    if (found)
      break;
    visited.push_back(nextTown);
    nextTown = A[nextTown];
  }

  int surplus = (K - margin) % period;
  cout << visited[surplus + margin];
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

const int N = (2e5) + 7;
int n, res;
long long k;
int a[N];
bool vst[N];
vector<int> cycle;
deque<int> st;

void _dfs(int i) {
  if (vst[i]) {
    while (st.back() != i) {
      cycle.push_back(st.back());
      st.pop_back();
    }
    cycle.push_back(i);
    st.pop_back();
    return;
  }

  st.push_back(i);
  vst[i] = true;
  _dfs(a[i]);
}

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

void _solve() {
  _dfs(1);
  reverse(cycle.begin(), cycle.end());

  if (k < (long long)st.size()) {
    while (k > 0) {
      st.pop_front();
      --k;
    }
    res = st.front();
  } else {
    k -= (long long)st.size();
    res = cycle[k % ((long long)cycle.size())];
  }
}

void _output() { cout << res; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  //    freopen("input.inp", "r", stdin);

  _input();
  _solve();
  _output();

  return 0;
}


Error: WA

Fixed submission:

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

const int N = (2e5) + 7;
int n, res;
long long k;
int a[N];
bool vst[N];
vector<int> cycle;
stack<int> st;

void _dfs(int i) {
  if (vst[i]) {
    while (st.top() != i) {
      cycle.push_back(st.top());
      st.pop();
    }
    cycle.push_back(i);
    st.pop();
    return;
  }

  st.push(i);
  vst[i] = true;
  _dfs(a[i]);
}

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

void _solve() {
  _dfs(1);
  reverse(cycle.begin(), cycle.end());

  if (k <= (long long)st.size()) {
    while (k > 1)
      st.pop();
    res = st.top();
  } else {
    k -= (long long)st.size();
    res = cycle[k % ((long long)cycle.size())];
  }
}

void _output() { cout << res; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  //    freopen("input.inp", "r", stdin);

  _input();
  _solve();
  _output();

  return 0;
}