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

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define what_is(x) cerr << #x << " is " << x << endl;
typedef long long ll;
const int MOD = 998244353;
const double PI = 3.141592653589793;
const int INF = 1e9 + 7;
const int nax = 200005;
ll n, k;
vector<int> adj[nax];
vector<bool> vis(nax, false);
vector<bool> stk(nax, false);
vector<int> path;
stack<int> st;
int start;

// 1---->j---->i->j;

void dfs(int i) {
  stk[i] = true;
  vis[i] = true;
  st.push(i);
  for (int j : adj[i]) {
    if (stk[j]) {
      start = j;
      while (!st.empty()) {
        path.emplace_back(st.top());
        st.pop();
      }
      return;
    } else {
      if (!vis[j]) {
        dfs(j);
      }
    }
  }
  if (!st.empty())
    st.pop();
  stk[i] = false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  // cin >> tc;
  while (tc--) {
    int j;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      cin >> j;
      adj[i].emplace_back(j);
    }

    dfs(1);

    reverse(path.begin(), path.end());
    // for (int el : path) {
    // 	cout << el << " ";
    // }

    int cycle_len = 0, i = 0, ok = k;
    for (i = 0; i < (int)path.size(); i++) {
      if (path[i] == start) {
        break;
      }
      k--;
    }
    if (k < 0) {
      cout << path[ok];
      return 0;
    }

    for (; i < (int)path.size(); i++) {
      cycle_len++;
    }

    k %= cycle_len;

    if (k == 0) {
      cout << start;
      return 0;
    }
    queue<int> q;
    q.push(start);
    vector<int> lev(nax, -1);
    lev[start] = 0;

    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int v : adj[u]) {
        if (lev[v] == -1) {
          lev[v] = 1 + lev[u];
          if (lev[v] == k) {
            cout << v;
            return 0;
          }
          q.push(v);
        }
      }
    }
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define what_is(x) cerr << #x << " is " << x << endl;
typedef long long ll;
const int MOD = 998244353;
const double PI = 3.141592653589793;
const int INF = 1e9 + 7;
const int nax = 200005;
ll n, k;
vector<int> adj[nax];
vector<bool> vis(nax, false);
vector<bool> stk(nax, false);
vector<int> path;
stack<int> st;
int start;

// 1---->j---->i->j;

void dfs(int i) {
  stk[i] = true;
  vis[i] = true;
  st.push(i);
  for (int j : adj[i]) {
    if (stk[j]) {
      start = j;
      while (!st.empty()) {
        // cout << st.top() << endl;
        path.emplace_back(st.top());
        st.pop();
      }
      return;
    } else {
      if (!vis[j]) {
        dfs(j);
      }
    }
  }
  st.pop();
  stk[i] = false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  // cin >> tc;
  while (tc--) {
    int j;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      cin >> j;
      adj[i].emplace_back(j);
    }

    dfs(1);

    reverse(path.begin(), path.end());
    // for (int el : path) {
    // 	cout << el << " ";
    // }

    int cycle_len = 0, i = 0;
    for (i = 0; i < (int)path.size(); i++) {
      if (path[i] == start) {
        break;
      }
      k--;
    }

    for (; i < (int)path.size(); i++) {
      cycle_len++;
    }

    k %= cycle_len;

    if (k == 0) {
      cout << start;
      return 0;
    }
    queue<int> q;
    q.push(start);
    vector<int> lev(nax, -1);
    lev[start] = 0;

    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int v : adj[u]) {
        if (lev[v] == -1) {
          lev[v] = 1 + lev[u];
          if (lev[v] == k) {
            cout << v;
            return 0;
          }
          q.push(v);
        }
      }
    }
  }
  return 0;
}