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 DEBBY
  ~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 ll long long
#define rep(i, a, b) for (int i = int(a); i <= int(b); i++)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pi>
#define fs first
#define sd second
#define pb push_back

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vi a(n + 1);
  rep(i, 1, n) cin >> a[i];
  bool visited[n + 1];
  memset(visited, false, sizeof(visited));
  deque<int> vis;
  int now = 1;
  bool repeat = false;
  // ll old_k = k;
  while (k > 0) {
    if (a[now] == now)
      break;
    if (visited[now] && !repeat) {
      int count = 1;
      while (vis.back() != now) {
        vis.pop_back();
        count++;
      }
      repeat = true;
      k = k % count;
      debug() << imie(count) imie(k) imie(now);
    } else {
      visited[now] = true;
      vis.push_back(now);
      debug() << imie(vis);
      now = a[now];
      k--;
    }
    // 1 3 4 1
  }
  cout << now;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}


Error: WA

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 DEBBY
  ~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 ll long long
#define rep(i, a, b) for (int i = int(a); i <= int(b); i++)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pi>
#define fs first
#define sd second
#define pb push_back

void solve() {
  int n;
  ll k;
  cin >> n >> k;
  vi a(n + 1);
  rep(i, 1, n) cin >> a[i];
  bool visited[n + 1];
  memset(visited, false, sizeof(visited));
  stack<int> vis;
  int now = 1;
  bool repeat = false;
  int old_k = k;
  while (k >= 0) {
    if (visited[now] && !repeat) {
      int count = 0;
      while (vis.top() != now) {
        vis.pop();
        count++;
      }
      repeat = true;
      k = old_k % count;
    } else {
      visited[now] = true;
      vis.push(now);
      now = a[now];
      k--;
    }
  }
  cout << now;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}