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

/***************************************** START OF TEMPLATE
 * *********************************************/

#include <bits/stdc++.h>

using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\\n"
#define f first
#define s second
#define pb push_back
#define INPa(a, n) foo(i, 1, n) cin >> a[i];
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define deb1(a)                                                                \\
  cout << a << " ";                                                            \\
  newl;
#define deb2(a, b)                                                             \\
  cout << a << " " << b << " ";                                                \\
  newl;
#define deb3(a, b, c)                                                          \\
  cout << a << " " << b << " " << c << " ";                                    \\
  newl;
#define deb4(a, b, c, d)                                                       \\
  cout << a << " " << b << " " << c << " " << d;                               \\
  newl;
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>

const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int INFi = INT_MAX;
const ll N = 2e+5 + 8;

vll adj[N];
ll vis[N];
ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll test_cases = 1;
randomINIT;

/*********************************************************************************************************/
/***************************************** VARIABLE DECLARATIONS
 * *****************************************/
ll n, k, u, v, a[N], mark = 0;
stack<ll> sk;
vll path;
/***************************************** FUNCTION IMPLEMENTATIONS
 * **************************************/
void dfs(ll v, ll p, ll d) {
  vis[v] = 1;
  sk.push(v);
  if (d == k) {
    cout << v;
    exit(0);
  }
  for (auto to : adj[v]) {
    if (vis[to] == 1) {
      mark = d + 1;
      while (sk.top() != to) {
        path.pb(sk.top());
        sk.pop();
      }
      path.pb(sk.top());
      sk.pop();
      reverse(all(path));
    }

    if (mark > 0)
      return;
    if (vis[to] == 0)
      dfs(to, v, d + 1);
  }
  vis[v] = 2;
}

/***************************************** START OF MAIN FUNCTION
 * ****************************************/
void MAIN(ll tc) {
  cin >> n >> k;
  foo(i, 1, n) cin >> a[i];
  foo(i, 1, n) adj[i].pb(a[i]);
  dfs(1, -1, 0);
  ll sz = path.size();
  k -= mark;
  cout << path[k % sz];
}

int main() {
  SPEED;

  // cin>>test_cases;
  foo(i, 1, test_cases) { MAIN(i); }
}

Error: WA

Fixed submission:

/***************************************** START OF TEMPLATE
 * *********************************************/

#include <bits/stdc++.h>

using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\\n"
#define f first
#define s second
#define pb push_back
#define INPa(a, n) foo(i, 1, n) cin >> a[i];
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define deb1(a)                                                                \\
  cout << a << " ";                                                            \\
  newl;
#define deb2(a, b)                                                             \\
  cout << a << " " << b << " ";                                                \\
  newl;
#define deb3(a, b, c)                                                          \\
  cout << a << " " << b << " " << c << " ";                                    \\
  newl;
#define deb4(a, b, c, d)                                                       \\
  cout << a << " " << b << " " << c << " " << d;                               \\
  newl;
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>

const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int INFi = INT_MAX;
const ll N = 2e+5 + 8;

vll adj[N];
ll vis[N];
ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll test_cases = 1;
randomINIT;

/*********************************************************************************************************/
/***************************************** VARIABLE DECLARATIONS
 * *****************************************/
ll n, k, u, v, a[N], mark = 0;
stack<ll> sk;
vll path;
/***************************************** FUNCTION IMPLEMENTATIONS
 * **************************************/
void dfs(ll v, ll p, ll d) {
  vis[v] = 1;
  sk.push(v);
  if (d == k) {
    cout << v;
    exit(0);
  }
  for (auto to : adj[v]) {
    if (to == p)
      continue;
    if (vis[to] == 1) {
      mark = d + 1;
      while (sk.top() != to) {
        path.pb(sk.top());
        sk.pop();
      }
      path.pb(sk.top());
      sk.pop();
      reverse(all(path));
    }

    if (mark > 0)
      return;
    if (vis[to] == 0)
      dfs(to, v, d + 1);
  }
  vis[v] = 2;
}

/***************************************** START OF MAIN FUNCTION
 * ****************************************/
void MAIN(ll tc) {
  cin >> n >> k;
  foo(i, 1, n) cin >> a[i];
  foo(i, 1, n) adj[i].pb(a[i]);
  dfs(1, -1, 0);
  ll sz = path.size();
  k -= mark;
  cout << path[k % sz];
}

int main() {
  SPEED;

  // cin>>test_cases;
  foo(i, 1, test_cases) { MAIN(i); }
}