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

#include "bits/stdc++.h"
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define cool                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define pb push_back
#define fe first
#define lb lower_bound
#define ub upper_bound
#define pii pair<pair<int, int>, pair<int, int>>
#define se second
#define endl "\\n"
#define pi pair<int, int>
#define mi map<int, int>
#define mii map<pi, int>
#define vi vector<int>
#define vvi vector<vi>
#define bs binary_search
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, a, b) for (int i = a; i <= b; i++)
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)x.size()
#define PI 3.14159265358979323846

const int N = 2e5 + 5;

int mod = 1e9 + 7;
int dx[4] = {0, 0, +1, -1};
int dy[4] = {+1, -1, 0, 0};

int po(int a, int b) {
  a %= mod;
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

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

void solve() {
  cin >> n >> k;

  rep1(i, 1, n) { cin >> a[i]; }

  int c_ind = 1, ct = 1;
  while (k--) {
    c_ind = a[c_ind];
    if (vis[c_ind])
      k %= (ct - vis[c_ind]);
    vis[c_ind] = ct++;
  }

  //~ cout<<ct<<endl;
  cout << c_ind << endl;
}

int32_t main() {
  cool;
  int t = 1;

  // cin>>t;
  while (t--)
    solve();

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define cool                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define pb push_back
#define fe first
#define lb lower_bound
#define ub upper_bound
#define pii pair<pair<int, int>, pair<int, int>>
#define se second
#define endl "\\n"
#define pi pair<int, int>
#define mi map<int, int>
#define mii map<pi, int>
#define vi vector<int>
#define vvi vector<vi>
#define bs binary_search
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, a, b) for (int i = a; i <= b; i++)
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)x.size()
#define PI 3.14159265358979323846

const int N = 1e5 + 5;

int mod = 1e9 + 7;
int dx[4] = {0, 0, +1, -1};
int dy[4] = {+1, -1, 0, 0};

int po(int a, int b) {
  a %= mod;
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int len, n, k, a[N], vis[N], ct, tin[N];
vi adj[N];

//~ void dfs(int u) {
//~ vis[u]=1;
//~ tin[u]=++ct;
//~ for(auto to:adj[u]) {
//~ if(vis[to]) {
//~ len=ct-tin[to];
//~ continue;
//~ }
//~ else
//~ dfs(to);
//~ }
//~ }

void solve() {
  cin >> n >> k;

  rep1(i, 1, n) { cin >> a[i]; }
  vi ans;
  int ind = 1, nd = 0, pos = 0, ct = 0;
  ans.pb(1);
  vis[1] = 1;
  mi mp;
  mp[1] = 0;
  while (1) {
    if (vis[a[ind]] == 1) {
      nd = ind;
      pos = mp[a[ind]];
      break;
    }
    adj[ind].pb(a[ind]);
    vis[a[ind]] = 1;
    ind = a[ind];
    ans.pb(ind);
    mp[ind] = ++ct;
  }

  //~ memset(vis,0,sizeof(vis));
  //~ dfs(1);

  //~ for(auto to:ans)
  //~ cout<<to<<" ";
  //~ cout<<endl;
  int ts = sz(ans);
  if (k < ct) {
    cout << ans[k] << endl;
    return;
  }
  k = k - pos;
  //~ cout<<pos<<endl;
  ct -= pos;
  int l = sz(ans) - ct + 1;
  int mv = k % l;
  //~ cout<<mv<<endl;
  //~ cout<<ct+mv<<endl;
  cout << ans[ct + mv - 1] << endl;
}

int32_t main() {
  cool;
  int t = 1;

  // cin>>t;
  while (t--)
    solve();

  return 0;
}