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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int gcd(int a, int b) {
  if (b == 0)
    return a;

  return gcd(b, a % b);
}
int power(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;

    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int modInverse(int n, int p) { return power(n, p - 2, p); }

int main() {
  // your code goes here
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> v[i];
  }
  set<ll> st;
  vector<ll> already;
  ll pos = 1;
  k++;
  while (k > 0) {
    if (st.count(pos) == 0) {
      st.insert(pos);
      already.push_back(pos);
    } else {

      break;
    }
    pos = v[pos];
    k--;
  }
  for (ll i = 0; i < (ll)already.size(); i++) {
    if (already[i] == pos) {
      pos = i;
      break;
    }
  }
  if (k > 0) {
    k--;
    ll val = (ll)already.size() - pos;
    ll ans = already[pos + k % val];
    cout << ans;
  } else {
    cout << already[(ll)already.size() - 1];
  }
  // for(ll x:already)
  // cout<<x<<" ";

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int gcd(int a, int b) {
  if (b == 0)
    return a;

  return gcd(b, a % b);
}
int power(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;

    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int modInverse(int n, int p) { return power(n, p - 2, p); }

int main() {
  // your code goes here
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> v[i];
  }
  set<ll> st;
  vector<ll> already;
  ll pos = 1;
  while (k > 0) {
    if (st.count(pos) == 0) {
      st.insert(pos);
      already.push_back(pos);
    } else
      break;
    pos = v[pos];
    k--;
  }
  // k++;
  // cout<<k<<" ";
  for (ll i = 0; i < (ll)already.size(); i++) {
    if (already[i] == pos) {
      pos = i;
      break;
    }
  }
  // for(ll x:already)
  // cout<<x<<" ";
  ll val = (ll)already.size() - pos;
  ll ans = already[pos + k % val];
  cout << ans;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
long long dp[100010];
long long h[100010];
const int MOD = 1000000007;

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

  vector<int> s;
  vector<int> check(n + 1, -1);
  int v = 1, T = 0, l = 0;
  while (check[v] == -1) {
    check[v] = s.size();
    s.push_back(v);
    v = a[v - 1];
  }
  T = s.size() - check[v];
  l = check[v];

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= T;
    cout << s[l + k] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
long long dp[100010];
long long h[100010];
const int MOD = 1000000007;

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

  vector<ll> A(n);
  rep(i, n) { cin >> A[i]; }

  set<ll> b;
  vector<int> c(n);
  ll cur = 0;
  ll cnt = 0;
  ll che = 0;
  size_t index;
  rep(i, n) {
    b.insert(A[cur]);
    c[i] = A[cur];
    cur = A[cur] - 1;
    auto it = find(c.begin(), c.end(), A[cur]);
    index = distance(c.begin(), it);
    cnt++;
    if (index != c.size()) {
      break;
    }
  }

  int mod = k % n;
  // cout << index << endl;
  if (index != 0) {
    c.erase(c.begin() + index - 1);
    mod -= index;
  }
  c.resize(cnt - index);
  // rep(i, c.size()) cout << c[i] << endl;

  // cout << c.size() << endl;
  if (mod > c.size()) {
    // cout << "b" << endl;
    while (mod > c.size()) {
      mod -= c.size();
    }
    cout << c[mod - 1] << endl;
  } else {
    k -= c.size();
    mod = k % n;
    // cout << "a" << endl;
    cout << c[mod - 1] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
#define fios                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
typedef long long ll;
#ifdef LOCAL
mt19937 rng(unsigned(70));
#else
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif
#define rand rng

int main() {
  // fios;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &x : a) {
    cin >> x;
  }

  vector<ll> cyc;
  unordered_map<ll, ll> mp;
  cyc.push_back(1);
  mp[1]++;
  ll i = a[0];
  ll cyclen = 0, tail = 0;
  while (1) {
    if (mp.find(i) != mp.end()) {
      break;
    }
    mp[i]++;
    cyc.push_back(i);
    i = a[i - 1];
  }
  ll j = 0;
  for (; j < (ll)cyc.size(); j++) {
    if (cyc[j] == i) {
      cyclen = (ll)cyc.size() - j;
      break;
    }
  }
  tail = j;
  if (k < tail)
    cout << cyc[k] << endl;
  else {
    k -= tail;
    cout << cyc[tail + k % cyclen] << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define fios                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
typedef long long ll;
#ifdef LOCAL
mt19937 rng(unsigned(70));
#else
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif
#define rand rng

int main() {
  // fios;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &x : a) {
    cin >> x;
  }

  vector<ll> cyc;
  unordered_map<ll, ll> mp;
  cyc.push_back(1);
  mp[1]++;
  ll i = a[0];
  ll cyclen = 0, tail = 0;
  while (1) {
    if (mp.find(i) != mp.end()) {
      break;
    }
    mp[i]++;
    cyc.push_back(i);
    i = a[i - 1];
  }

  tail = i - 1;
  for (int j = 0; j < (int)cyc.size(); j++) {
    if (cyc[j] == i) {
      cyclen = (int)cyc.size() - j;
      break;
    }
  }
  if (k <= tail)
    cout << cyc[k - 1] << endl;
  else {
    k -= tail;
    cout << cyc[tail + k % cyclen] << endl;
  }
}
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 ll = long long;
using P = pair<int, int>;

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

  int pos;
  if (k <= n) {
    pos = 0;
    rep(i, k) pos = a[pos] - 1;
  } else {
    int count1 = 0;
    int start;
    pos = 0;
    rep(i, n) {
      if (r[pos] != 0) {
        start = pos;
        break;
      }
      ++count1;
      r[pos] = count1;
      pos = a[pos] - 1;
    }
    int count2 = 0;
    rep(i, n) {
      count2++;
      pos = a[pos] - 1;
      if (pos == start) {
        break;
      }
    }

    int rem = (k - (count1 - count2)) % count2;

    pos = start;
    rep(i, rem) { pos = a[pos] - 1; }
  }

  cout << pos + 1 << endl;

  return 0;
}


Error: -11

Fixed submission:

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

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

  int pos;
  if (k <= n) {
    pos = 0;
    rep(i, k) pos = a[pos] - 1;
  } else {
    int count1 = 0;
    int start;
    rep(i, n) {
      if (r[pos] != 0) {
        start = pos;
        break;
      }
      ++count1;
      r[pos] = count1;
      pos = a[pos] - 1;
    }
    int count2 = 0;
    rep(i, n) {
      count2++;
      pos = a[pos] - 1;
      if (pos == start) {
        break;
      }
    }
    int rem = (k - count1) % count2;
    pos = start;
    rep(i, rem) { pos = a[pos] - 1; }
  }

  cout << pos + 1 << endl;

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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  vector<bool> visited(N, false);
  vector<long long> F(N, 0);

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

  long long cnt1 = N > K ? K : N;
  long long now = 0;
  long long loop_start = 0;
  long long loop_start_at = 0;
  long long loop_length = 0;
  visited[0] = true;
  for (long long i = 0; i < cnt1; i++) {
    now = A[now];
    if (visited[now]) {
      if (loop_length == 0) {
        loop_length = i - F[now];
        if (now == 0)
          loop_length++;
        loop_start = F[now];
        if (now != 0)
          loop_start++;
        loop_start_at = now;
      }
    } else {
      F[now] = i;
      visited[now] = true;
    }
  }
  if (N >= K) {
    now++;
    cout << now << endl;
    return 0;
  }
  // cout << loop_length << endl;
  // cout << loop_start << endl;
  long long cnt2 = (K - loop_start) % loop_length;
  now = loop_start_at;
  for (long long i = 0; i < cnt2; i++) {
    now = A[now];
  }
  now++;
  cout << now << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  vector<bool> visited(N, false);
  vector<long long> F(N, 0);

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

  long long cnt1 = N > K ? K : N;
  long long now = 0;
  long long loop_start = 0;
  long long loop_start_at = 0;
  long long loop_length = 0;
  visited[0] = true;
  for (long long i = 0; i < cnt1; i++) {
    now = A[now];
    if (visited[now]) {
      if (loop_length == 0) {
        loop_length = i - F[now];
        if (now == 0)
          loop_length++;
        loop_start = F[now];
        if (now != 0)
          loop_start++;
        loop_start_at = now;
      }
    } else {
      F[now] = i;
      visited[now] = true;
    }
  }
  if (N > K) {
    now++;
    cout << now << endl;
  }
  // cout << loop_length << endl;
  // cout << loop_start << endl;
  long long cnt2 = (K - loop_start) % loop_length;
  now = loop_start_at;
  for (long long i = 0; i < cnt2; i++) {
    now = A[now];
  }
  now++;
  cout << now << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {

  int n;
  long long int k;
  cin >> n >> k;

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

  int b[n + 1] = {};
  vector<int> c;

  int d = 0;

  for (int i = 0; i < n + 3; i++) {
    b[d]++;
    c.push_back(d);
    if (b[d] == 2)
      break;
    d = a[d];
  }

  int p;
  p = c.size();
  int q = 0;

  for (int i = 0; i < p; i++) {
    if (c[i] == c[p - 1]) {
      q = i;
      break;
    }
  }

  if (k < p)
    cout << c[k] + 1 << endl;
  else
    cout << c[q + (k - p + 1) % (p - q - 1)] + 1 << endl;
}

Error: 0

Fixed submission:

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

using namespace std;

int main() {

  int n;
  long long int k;
  cin >> n >> k;

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

  int b[n + 1];
  vector<int> c;

  int d = 0;

  for (int i = 0; i < n + 3; i++) {
    b[d]++;
    c.push_back(d);
    if (b[d] == 2)
      break;
    d = a[d];
  }

  int p;
  p = c.size();
  int q = 0;

  for (int i = 0; i < p; i++) {
    if (c[i] == c[p - 1]) {
      q = i;
      break;
    }
  }

  if (k < p)
    cout << c[k] + 1 << endl;
  else
    cout << c[q + (k - p + 1) % (p - q - 1)] + 1 << endl;
}
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 ll = long long;
using P = pair<int, int>;

int main() {
  ll N, K;
  cin >> N >> K;

  vector<int> A(N);
  int a;
  rep(i, N) {
    cin >> a;
    A[i] = a - 1;
  }

  ll b = 0, c = 0;
  vector<int> visited(N, -1);
  visited[0] = 0;

  int pos = 0;
  rep(i, N) {
    int next_pos = A[pos];
    if (visited[next_pos] == -1) {
      visited[next_pos] = i + 1;
    } else {
      b = visited[next_pos];
      c = i + 1 - b;
      break;
    }
    pos = next_pos;
  }
  // cout << b << ' ' << c << endl;

  int ans = 0;
  if (K <= b) {
    rep(i, K) ans = A[ans];
  } else {
    rep(i, b) ans = A[ans];
    K -= b;
    K %= c;
    rep(i, K) ans = A[ans];
  }
  cout << ans + 1 << endl;
  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 ll = long long;
using P = pair<int, int>;

int main() {
  ll N, K;
  cin >> N >> K;

  vector<int> A(N);
  int a;
  rep(i, N) {
    cin >> a;
    A[i] = a - 1;
  }

  ll shuki = 0;
  rep(i, N) {
    if (A[i] == 0) {
      break;
    }
    shuki++;
  }

  K = K % shuki;
  // cout << shuki << ' ' << K << endl;
  int ans = 0;
  if (K > 0) {
    rep(i, K) {
      ans = A[ans];
      // cout << ans << endl;
    }
  }

  cout << ans + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = s; i < (int)n; i++)
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  mp[0]++;
  ll key = 0;
  ll time = 0;
  ll loop = 0;
  vector<ll> vec;
  vector<ll> vec0;
  vec0.push_back(0);
  rep(i, 2 * n + 10) {
    mp[a[key] - 1]++;
    if (mp[a[key] - 1] == 1) {
      time++;
      vec0.push_back(a[key]);
    }
    if (mp[a[key] - 1] == 2) {
      loop++;
      vec.push_back(a[key]);
    }
    key = a[key] - 1;
  }
  if (k > time && (k - time) % loop != 0)
    cout << vec[(k - time) % loop - 1] << endl;
  else if (k > time && (k - time) % loop == 0)
    cout << vec[loop - 1] << endl;
  else
    cout << vec0[k] << endl;
}


Error: 0

Fixed submission:

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

int main() {
  long long int n, k;
  cin >> n >> k;
  long long int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long int b[k + 1];
  b[0] = 1;
  for (int i = 1; i <= k; i++) {
    b[i] = a[b[i - 1]];
  }
  cout << b[k] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> lpii;
typedef unsigned long long ull;
const double eps = 1e-8;
const double pi = acos(-1.0);
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 2e5 + 10;
int vis[N];
int a[N];
int l1, l2, l;
int n;
ll k;
int d[N];
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  vis[1] = 1;
  l = 0;
  a[l] = 1;
  int x = d[1];
  while (!vis[x]) {
    vis[x] = 1;
    a[++l] = x;
    x = d[x];
  }

  for (int i = 0; i <= l; i++) {
    if (a[i] == x) {
      l1 = i;
      break;
    }
  }
  if (k <= 1ll * l1) {
    cout << a[(int)k] << endl;
  } else {
    k -= (1ll * l1);
    k %= (l - l1 + 1);
    cout << a[(int)k + l1] << endl;
  }
}

int main() {
#ifdef ONLINE_JUDGE
#else
  freopen("std1.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  // scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const int N = 100010;
int n, length;
int step;
int a[N], vis[N];
int p;
long long k;
int dfs(int x) {
  if (vis[x])
    return x;
  step++;
  vis[x] = 1;
  dfs(a[x]);
}

void dfs1(int x) {
  if (length && x == p)
    return;
  length++;
  dfs1(a[x]);
}

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

  p = dfs(1);
  if (k <= step) {
    int now = 1;
    for (int i = 1; i <= k; i++) {
      now = a[now];
    }
    cout << now << endl;
  } else {
    dfs1(p);
    int sstep = (k - step) % length;
    int now = p;
    for (int i = 1; i <= sstep; i++) {
      now = a[now];
    }
    cout << now << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

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

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

long long a[200005];
bool b[200005];
int main() {
  long long n;
  long long k;
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = 0;
  }
  long long i = 1, x = 0;
  while (i <= n && k--) {
    b[i] = 1;
    x = i;
    i = a[i];
    if (b[i] == 1) {
      k = k % (x - i);
    }
  }
  cout << i << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
using namespace std;
int main() {
  long long int k;
  int n;
  cin >> n >> k;
  int v[n];
  int town, count = 0, i = 0;
  int a[n];
  memset(v, -1, sizeof v);
  v[0] = 0;

  for (int j = 0; j < n; j++)
    cin >> a[j];
  if (k < n) {
    while (count < k) {
      i = a[i] - 1;
      count++;
    }
  } else {
    while (count < k) {
      i = a[i] - 1;
      count++;
      if (v[i] == -1) {
        v[i] = count;
      } else {
        town = i;
        break;
      }
    }

    int r = (k - v[town]) % (count - v[town]);
    i = town;
    while (r > 0) {
      i = a[i] - 1;
      r--;
    }
  }
  cout << i + 1;
  return 0;
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int main() {
  long long int k;
  int n;
  cin >> n >> k;
  int v[n];
  int town, count = 0, i = 0;
  int a[n];
  memset(v, -1, sizeof v);
  v[0] = 0;

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

  /*for(int i=0;i<n;i++)
  cout<<v[i]<<" ";
  cout<<endl;*/
  while (count < k) {
    // cout<<"i"<<i;
    i = a[i] - 1;
    // cout<<"i"<<i;
    count++;
    // cout<<"vi"<<v[i];
    if (v[i] == -1) {
      v[i] = count;
      // cout<<"in";
    } else {
      town = i;
      // cout<<"break";
      break;
    }
  }
  // cout<<count;
  // cout<<"town"<<town<<endl;
  int r = (k - v[town]) % (count - v[town]);
  i = town;
  while (r > 0) {
    i = a[i] - 1;
    r--;
  }
  cout << i + 1;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

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;
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (LL i = 0; i < (LL)(n); i++)

int main(void) {
  LL n, x, k;
  map<int, LL> check;

  cin >> n >> k;
  vector<LL> tel(n, 0);

  rep(i, n) { cin >> tel[i]; }

  x = 1;
  repl(i, k) {
    if (i > 0) {
      x = tel[x - 1];
    }
    // cout << check.count(x) << endl;
    if (check.count(x)) {
      LL rem = k - check[x];
      LL loop = i - check[x];
      // cout << rem << " " << loop << endl;
      rep(j, rem % loop) { x = tel[x - 1]; }
      cout << x << endl;
      return 0;
    }
    check[x] = i;
  }

  x = tel[x - 1];
  cout << x << endl;

  return 0;
};

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

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;
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (LL i = 0; i < (LL)(n); i++)

int main(void) {
  LL n, x, k;
  map<int, LL> check;

  cin >> n >> k;
  vector<LL> tel(n, 0);

  rep(i, n) { cin >> tel[i]; }

  x = 1;
  repl(i, k) {
    x = tel[x - 1];
    if (check[x] > 0) {
      LL rem = k - check[x];
      LL loop = i - check[x] - 1;
      // cout << rem << " " << loop << endl;
      rep(j, rem % loop) { x = tel[x - 1]; }
      break;
    }
    check[x] = i;
  }

  cout << x << endl;

  return 0;
};