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

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>*/
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
#define maxn 200005
using namespace std;
// using namespace __gnu_pbds;
const double PI = acos(-1.0);
const double eps = 1e-6;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
ll to[maxn];
ll vis[maxn];
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  ll n = read();
  ll k = read();
  for (ll i = 0; i <= maxn - 1; i++)
    vis[i] = -1;
  for (ll i = 1; i <= n; i++)
    to[i] = read();
  vis[1] = 0;
  ll p = 1;
  for (ll i = 1; i <= k; i++) {
    p = to[p];
    if (i == k) {
      printf("%lld\\n", p);
      return 0;
    } else if (vis[p] != -1) {
      if (i - vis[p] == 1) {
        cout << p << endl;
        return 0;
      }
      ll shen = k - i;
      ll pp = (i - vis[p]);
      ll nxt = shen % pp;
      // printf("p=%d\\n",p);
      // printf("nxt=%d\\n",nxt);
      if (nxt == 0) {
        cout << p << endl;
        return 0;
      } else {
        for (int j = i; j <= i + nxt; j++) {
          if (j == i + nxt) {
            cout << p << endl;
            return 0;
          }
          p = to[p];
        }
      }
    }
    vis[p] = i;
  }
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>*/
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
#define maxn 200005
using namespace std;
// using namespace __gnu_pbds;
const double PI = acos(-1.0);
const double eps = 1e-6;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
ll to[maxn];
ll vis[maxn];
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  ll n = read();
  ll k;
  cin >> k;
  for (ll i = 0; i <= maxn - 1; i++)
    vis[i] = -1;
  for (ll i = 1; i <= n; i++)
    to[i] = read();
  vis[1] = 0;
  ll p = 1;
  for (ll i = 1; i <= k; i++) {
    p = to[p];
    if (i == k) {
      printf("%lld\\n", p);
      return 0;
    } else if (vis[p] != -1) {
      ll x = i;
      ll x0 = vis[p];
      if ((k - x) % (x - x0) == 0) {
        printf("%lld\\n", p);
        return 0;
      } else if (x - x0 <= k - x)
        i = ((k - x) % (x - x0)) - 1;
    }
    vis[p] = i;
  }
  return 0;
}