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

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

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long

const int N = 2e5 + 5;

int n, k;
int a[N];
bool vis[N];

int32_t main() {
  IOS;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int take = min(k, 200000LL);
  k -= take;
  int cur = 1;
  while (take > 0) {
    cur = a[cur];
    take--;
  }
  if (k == 0) {
    cout << cur;
    return 0;
  }
  int len = 0;
  while (!vis[cur]) {
    vis[cur] = 1;
    len++;
    cur = a[cur];
  }
  k %= len;
  while (k > 0) {
    cur = a[cur];
    k--;
  }
  cout << cur;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <list>
#define ll long long int
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define rep(a, b, c) for (a = b; a < c; a++)
#define repj(a, b, c) for (a = b; a < c; a++)
#define repr for (i = n - 1; i >= 0; i--)
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define inf 100000001
const double pie = 3.141592653589793238463;
#define mod 1000000007
#define lb lower_bound
#define up upper_bound
#define rep1 for (i = 1; i <= n; i++)
#define vr vector
#define ef else if
#define all(x) (x).begin(), (x).end()
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define lenth length()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define t4(x, y, z, a)                                                         \\
  cout << "x is " << x << "    y is " << y << "    z is " << z << "    a is "  \\
       << a << endl;
#define t3(x, y, z)                                                            \\
  cout << "x is " << x << "    y is " << y << "    z is " << z << endl;
#define t2(x, y) cout << "x is " << x << "    y is " << y << endl;
#define t1(x) cout << "x is " << x << endl;
using namespace std;
bool vis[200005];
int main() {
  // fastio
  ll t, i, j, k, l, m, n, a, b, x, y, z, c, d, e, p, nl, np, q, r;
  string str2, str3;
  ll mx = INT_MIN;
  ll mn = INT_MAX;
  ll ans = 0, s1 = 0, s2 = 0;
  string s;
  cin >> n >> k;
  ll arr[n];
  rep(i, 0, n) { cin >> arr[i]; }
  ll take = min(k, 200000LL);
  k -= take;
  ll cur = 1;
  while (take > 0) {
    cur = arr[cur];
    take--;
  }
  if (k == 0) {
    cout << cur;
    return 0;
  }
  ll len = 0;
  while (!vis[cur]) {
    vis[cur] = 1;
    len++;
    cur = arr[cur];
  }
  k = k % len;
  while (k > 0) {
    cur = arr[cur];
    k--;
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

// #define DIV 1000000007

using namespace std;
using ll = long long;
using ldb = long double;

typedef struct {
  int to = 0;
  vector<ll> dis;
  ll loop = 1;
} node_t;
vector<node_t> node;

int main() {
  ll N, K;
  cin >> N >> K;
  node = vector<node_t>(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> node[i].to;
  }

  int lst = 0;
  queue<int> q;
  // node[1].dis.push_back(0);
  q.push(1);
  for (int i = 1; i <= N + 1; i++) {
    int x = q.front();
    q.pop();
    int y = node[x].to;
    node[y].dis.push_back(i);
    q.push(y);
    int sz = node[y].dis.size();
    if (sz == 2) {
      node[y].loop = node[y].dis[sz - 1] - node[y].dis[sz - 2];
      if (lst == 0)
        lst = y;
    }
  }
  // cout << lst << endl;
  if (K <= node[lst].dis[0]) {
    for (int i = 1; i <= N; i++) {
      if (node[i].dis.size() <= 0)
        continue;
      if (K == node[i].dis[0]) {
        cout << i << endl;
        break;
      }
    }
  }

  else {
    ll more = (K - node[lst].dis[0]) % node[lst].loop;
    queue<int> L;
    L.push(lst);
    for (ll i = 1; i <= more; i++) {
      int x = L.front();
      L.pop();
      L.push(node[x].to);
    }
    cout << L.front() << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define DIV 1000000007

using namespace std;
using ll = long long;
using ldb = long double;

typedef struct {
  int to = 0;
  vector<ll> dis;
  ll loop = 1;
} node_t;
vector<node_t> node;
int main() {
  ll N, K;
  cin >> N >> K;
  node = vector<node_t>(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> node[i].to;
  }

  int lst = 0;
  queue<int> q;
  // node[1].dis.push_back(0);
  q.push(1);
  for (int i = 1; i <= N + 1; i++) {
    int x = q.front();
    q.pop();
    int y = node[x].to;
    node[y].dis.push_back(i);
    q.push(y);
    int sz = node[y].dis.size();
    if (sz == 2) {
      node[y].loop = node[y].dis[sz - 1] - node[y].dis[sz - 2];
      if (lst == 0)
        lst = y;
    }
  }

  if (K <= node[lst].dis[0]) {
    for (int i = 1; i <= N; i++) {
      if (K == node[i].dis[0]) {
        cout << i << endl;
        break;
      }
    }
  }

  else {
    ll more = (K - node[lst].dis[0]) % node[lst].loop, ans;
    queue<int> L;
    L.push(lst);
    for (ll i = 1; i <= more; i++) {
      int x = L.front();
      L.pop();
      L.push(node[x].to);
    }
    cout << L.front() << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const double pi = acos(-1);
const int mod = 100007;
const double eps = 1e-8;
const ll inf = 1e18 + 9;
int a[maxn];
int road[maxn];
int vis[maxn];

int main() {
  memset(vis, 0, sizeof(vis));
  int n;
  ll k;
  scanf("%d %lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int cnt = 1;
  int t = 1;
  vis[1] = 1;
  road[cnt++] = 1;
  int en = 0;
  while (1) {
    int x = a[t];
    if (vis[x]) {
      en = x;
      break;
    } else {
      vis[x] = 1;
      road[cnt++] = x;
      t = x;
    }
  }
  //	for(int i = 1;i < cnt;i++){
  //		printf("%d ",road[i]);
  //	}
  //	cout << endl;
  ll y = 0;
  for (ll i = 1; i < cnt; i++) {
    if (road[i] == en) {
      y = i;
      break;
    }
  }
  ll s = cnt - y;
  if (k <= y) {
    printf("%d", a[road[k]]);
  } else {
    k -= y;
    k %= s;
    printf("%d", a[road[y + k]]);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> PAIR;
const int N = 1e5 + 5;
const ll M = 11092019;

ll c[15], p[15][15], temp[15];

inline bool check(int m, ll x) {
  for (int i = 1; i <= m; ++i) {
    if (temp[i] < x)
      return 0;
  }
  return 1;
}

int main() {
  int n, m, x;
  scanf("%d%d%d", &n, &m, &x);

  for (int i = 0; i < n; ++i) {
    scanf("%lld", &c[i]);
    for (int j = 1; j <= m; ++j)
      scanf("%lld", &p[i][j]);
  }

  ll ans = 2e18;
  for (int base = 0; base < (1 << n); ++base) {
    ll cnt = 0;
    memset(temp, 0, sizeof(temp));
    for (int i = 0; i < n; ++i) {
      if ((1 << i) & base) {
        cnt += c[i];
        for (int j = 1; j <= m; ++j)
          temp[j] += p[i][j];
      }
    }
    // printf("%d %lld\\n",base,cnt);
    if (check(m, x))
      ans = min(ans, cnt);
  }

  if (ans == 2e18)
    printf("-1\\n");
  else
    printf("%lld\\n", ans);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define endl "\\n"
#define int long long int
#define mod 1000000007
#define INT_BITS 16
#define inf 1000000099
#define watch(x) cout << (#x) << "=" << (x) << "\\n"
//__builtin_popcount()

// int loop(int i)
// {
//     if(visited[i]==1)
//       return i;
//     for(auto x:g[i])
//     {
//         visited[x]=1;
//         loop(x);
//     }

// }

// int looplength(int i)
// {
//     int count=1;
//     int x=g[i];
//     while(x!=i)
//     {
//       x=g[x];
//       count++;
//     }
//     return count;
// }
int32_t main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  int visited[n];
  memset(visited, 0, sizeof(visited));
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
    // g[i].push_back(a[i]);
  }
  int i = 0;
  while (visited[i] != 1 && k > 0) {
    visited[i] = 1;
    i = a[i];
    k--;
  }
  if (k == 0) {
    cout << i + 1 << endl;
    return 0;
  }
  // watch(i);
  int count = 1;
  int x = a[i];
  while (x != i) {
    x = a[x];
    count++;
  }
  // watch(count);
  // watch(k);
  k %= count;
  x = i;

  while (k > 0) {
    x = a[x];
    k--;
  }
  cout << x + 1 << endl;
  // int x=loop(1);
  // memset(visited,0,sizeof(visited));
  // int length=looplength(x);
  // watch(x);
  // watch(length);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define endl "\\n"
#define int long long int
#define mod 1000000007
#define INT_BITS 16
#define inf 1000000099
#define watch(x) cout << (#x) << "=" << (x) << "\\n"
//__builtin_popcount()
vector<int> g[100001];
int visited[100001];
// int loop(int i)
// {
//     if(visited[i]==1)
//       return i;
//     for(auto x:g[i])
//     {
//         visited[x]=1;
//         loop(x);
//     }

// }

// int looplength(int i)
// {
//     int count=1;
//     int x=g[i];
//     while(x!=i)
//     {
//       x=g[x];
//       count++;
//     }
//     return count;
// }
int32_t main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  memset(visited, 0, sizeof(visited));
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
    g[i].push_back(a[i]);
  }
  int i = 0;
  while (visited[i] != 1 && k > 0) {
    visited[i] = 1;
    i = a[i];
    k--;
  }
  if (k == 0) {
    cout << i + 1 << endl;
    return 0;
  }
  // watch(i);
  int count = 1;
  int x = a[i];
  while (x != i) {
    x = a[x];
    count++;
  }
  // watch(count);
  // watch(k);
  k %= count;
  x = i;

  while (k > 0) {
    x = a[x];
    k--;
  }
  cout << x + 1 << endl;
  // int x=loop(1);
  // memset(visited,0,sizeof(visited));
  // int length=looplength(x);
  // watch(x);
  // watch(length);
  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;
typedef long long ll;

int main() {

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

  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }

  vector<int> c(n);
  int x = 1;
  int count = 0;
  int l = 0;
  c[0] = 1;

  REP(i, k) {
    x = a[x - 1];
    c[x - 1]++;
    if (c[x - 1] == 2)
      count++;
    if (c[x - 1] == 3) {
      l = (k - i - 1) % count;
      break;
    }
  }

  REP(i, l) { x = a[x - 1]; }

  cout << x << endl;
}


Error: 0

Fixed submission:

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

int main() {

  long int n, k;
  cin >> n >> k;
  long int m = n;

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    if (i + 1 == a.at(i))
      m--;
  }

  k -= (k / m) * m;

  int x = a.at(0);
  if (k == 0)
    x = 1;
  else {
    for (int i = 0; i < k - 1; i++) {
      x = a.at(x - 1);
    }
  }
  cout << x << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

ll n, k, a[200005], v[200005];

int main() {
  ll i, j;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (i = 1, j = 1; !v[i] && k; i = a[i], j++, k--)
    v[i] = j;
  k %= (j - v[i]);
  for (; k; i = a[i], k--)
    ;
  cout << i;
  return 0;
}

Error: WA

Fixed submission:

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

ll n, k, a[100005], v[100005];

int main() {
  ll i, j;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (i = 1, j = 1; !v[i]; i = a[i], j++, k--)
    v[i] = j;
  k %= (j - v[i]);
  for (; k; i = a[i], k--)
    ;
  cout << i;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<bool> flag(n);
  vector<ll> store;
  vector<ll> stock;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    flag[i] = false;
  }
  vector<bool> f(n);
  for (ll i = 0; i < n; i++) {
    f[i] = false;
  }

  flag[0] = true;
  ll next = a[0];
  ll step = 0;
  stock.pb(next);
  while (1) {
    if (!flag[next - 1]) {
      flag[next - 1] = true;
      next = a[next - 1];
      stock.pb(next);
      step++;
    } else {
      ll ni = next - 1;
      store.pb(a[ni]);
      f[ni] = true;
      ni = a[ni] - 1;
      while (1) {
        if (!f[ni]) {
          f[ni] = true;
          store.pb(a[ni]);
          ni = a[ni] - 1;
        } else {
          break;
        }
      }
      break;
    }
  }
  if (k <= stock.size()) {
    cout << stock[k - 1] << endl;
  } else {
    ll num = (k - (stock.size() + 1 - store.size())) % store.size();
    cout << store[num] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<bool> flag(n);
  vector<ll> store;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    flag[i] = false;
  }
  vector<bool> f(n);
  for (ll i = 0; i < n; i++) {
    f[i] = false;
  }

  flag[0] = true;
  ll next = a[0];
  ll step = 0;
  while (1) {
    if (!flag[next - 1]) {
      k--;
      flag[next - 1] = true;
      next = a[next - 1];
    } else {
      ll ni = next - 1;
      store.pb(a[ni]);
      f[ni] = true;
      ni = a[ni] - 1;
      while (1) {
        if (!f[ni]) {
          f[ni] = true;
          store.pb(a[ni]);
          ni = a[ni] - 1;
        } else {
          break;
        }
      }
      ll num = (k - step + store.size() + 1) % store.size();
      cout << store[num] << endl;
      return 0;
    }
    if (k <= 0) {
      cout << next << endl;
      break;
    }
  }
  ll num = (k - step + store.size() + 1) % store.size();
  cout << store[num] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
#define rep(i, N) for (int i = 0; i < N; i++)

int main() {
  i64 N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i], A[i]--;

  vector<i64> visit_f(N, -1);
  vector<i64> visit_s(N, -1);

  int now = 0;
  for (int i = 0; i < N + 1; i++) {
    if (visit_f[now] != -1) {
      visit_s[now] = i;
      break;
    }
    visit_f[now] = i;
    now = A[now];
  }

  if (K >= visit_f[now]) {
    K -= visit_f[now];
    K %= (visit_s[now] - visit_f[now]);
  } else {
    now = 0;
  }
  while (K--)
    now = A[now];

  cout << now + 1 << endl;
}


Error: -6
16973247-db28-4dfa-98b7-0429f62356fe.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02684/C++/s510437675.cpp:27: int main(): Assertion `d != -1' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
#define rep(i, N) for (int i = 0; i < N; i++)

int main() {
  i64 N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i], A[i]--;

  vector<i64> visit_f(N, -1);
  vector<i64> visit_s(N, -1);

  int now = 0;
  for (int i = 0; i < N + 1; i++) {
    if (visit_f[now] != -1) {
      visit_s[now] = i;
      break;
    }
    visit_f[now] = i;
    now = A[now];
  }

  int d = -1;
  for (auto &c : visit_s)
    if (c != 1)
      d = c;
  assert(d != -1);

  if (K >= visit_f[now]) {
    K -= visit_f[now];
    K %= (visit_s[now] - visit_f[now]);
  }
  while (K--)
    now = A[now];

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

#include <bits/stdc++.h>
#include <math.h>
#include <string>
using namespace std;
typedef long long int ll;
vector<vector<int>> ug;
vector<bool> visited;
vector<int> tims;
vector<int> path;
int tim = 0;
int dfs(int u) {
  if (visited[u])
    return tims[u];
  visited[u] = true;
  path.push_back(u);
  tims[u] = tim;
  tim++;
  return dfs(ug[u][0]);
}

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

  long long int n, k;
  cin >> n >> k;
  ug.resize(n + 1);
  visited.assign(n + 1, false);
  tims.resize(n + 1);

  for (int i = 1; i < n + 1; i++) {
    ug[i].resize(1);
    cin >> ug[i][0];
  }
  int c_s = dfs(1);
  long long int l = path.size();
  if (k < l) {
    cout << path[k];
  } else {
    k -= c_s;
    int c_len = path.size() - c_s;
    cout << path[k % c_len + c_s];
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#include <string>
using namespace std;
typedef long long int ll;
vector<vector<int>> ug;
vector<bool> visited;
vector<int> tims;
vector<int> path;
int tim = 0;
int dfs(int u) {
  if (visited[u])
    return tims[u];
  visited[u] = true;
  path.push_back(u);
  tims[u] = tim;
  tim++;
  dfs(ug[u][0]);
}

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

  long long int n, k;
  cin >> n >> k;
  ug.resize(n + 1);
  visited.assign(n + 1, false);
  tims.resize(n + 1);

  for (int i = 1; i < n + 1; i++) {
    ug[i].resize(1);
    cin >> ug[i][0];
  }
  int c_s = dfs(1);
  if (k < path.size()) {
    cout << path[k];
  } else {
    k -= c_s;
    int c_len = path.size() - c_s;
    cout << path[k % c_len + c_s];
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long> firstpath;
  vector<long long> roop;
  long long N;
  long long K;
  ;
  cin >> N >> K;
  long long A[N], visited[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
    visited[i] = 0;
  }
  long long now, roopstart;
  now = A[0];
  visited[0] = 1;
  while (1) {
    if (visited[now] > 0) {
      roopstart = now;
      break;
    } else {
      visited[now] = 1;
      now = A[now];
    }
  }
  now = 0;
  while (1) {
    if (now == roopstart) {
      break;
    } else {
      firstpath.push_back(now);
      now = A[now];
    }
  }
  now = roopstart;
  roop.push_back(roopstart);
  while (1) {
    now = A[now];
    if (now == roopstart)
      break;
    else {
      roop.push_back(now);
    }
  }
  long long ans;
  if (K < firstpath.size()) {
    ans = firstpath.at(K);
  } else {
    K -= firstpath.size();
    K %= roop.size();
    ans = roop.at(K);
  }
  cout << ans + 1;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long> firstpath;
  vector<long long> roop;
  long long N;
  long long K;
  ;
  cin >> N >> K;
  long long A[N], visited[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
    visited[i] = 0;
  }
  long long now, roopstart;
  now = A[0];
  visited[0] = 1;
  while (1) {
    if (visited[now] > 0) {
      roopstart = now;
      break;
    } else {
      visited[now] = 1;
      now = A[now];
    }
  }
  now = 0;
  while (1) {
    if (now == roopstart) {
      break;
    } else {
      firstpath.push_back(now);
      now = A[now];
    }
  }
  now = roopstart;
  roop.push_back(roopstart);
  while (1) {
    now = A[now];
    if (now == roopstart)
      break;
    else {
      roop.push_back(now);
    }
  }
  long long ans;
  if (K <= firstpath.size()) {
    ans = firstpath.at(K);
  } else {
    K -= firstpath.size();
    K %= roop.size();
    ans = roop.at(K);
  }
  cout << ans + 1;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using pii = pair<int, int>;
using pll = pair<LL, LL>;
using pld = pair<LD, LD>;

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

  int N;
  LL K;
  cin >> N >> K;

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

  vector<int> path(N + 1), last_idx(N + 1);
  for (int cur_nr = nrs[1], i = 1;; ++i) {
    if (i == K) {
      cout << cur_nr;
      return 0;
    }
    if (last_idx[cur_nr]) {
      int mod = i - last_idx[cur_nr];
      int res_idx, cur_mod;
      for (cur_mod = (res_idx = last_idx[cur_nr]) % mod; cur_mod != K % mod;
           cur_mod = (cur_mod + 1) % mod, ++res_idx)
        ;
      // cerr << "Res idx :) : " << res_idx << endl;
      cout << path[res_idx];
      return 0;
    }

    path[i] = cur_nr;
    last_idx[cur_nr] = i;
    cur_nr = nrs[cur_nr];
  }
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using pii = pair<int, int>;
using pll = pair<LL, LL>;
const int MAX_N = 2e5;
int last_ind_enc[MAX_N + 1];

int main() {
  cin.sync_with_stdio(0);

  int N;
  LL K;
  cin >> N >> K;

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

  int cur_rem = 0, cur_ind = 1;
  int mod_res[MAX_N];
  while (!last_ind_enc[cur_ind]) {
    last_ind_enc[cur_ind] = cur_rem + 1;
    mod_res[cur_rem++] = cur_ind;
    cur_ind = nrs[cur_ind];
  }

  if (K <= last_ind_enc[cur_ind]) {
    cout << mod_res[K];
  } else {
    // cerr << "cur rem: " << cur_rem << "\\n";
    int modulus = cur_rem - last_ind_enc[cur_ind];
    cout << mod_res[(K + 1) % modulus + modulus];
  }
}