Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define watch(x)                                                               \
  ;                                                                            \
  cout << #x << "=" << x << endl;
#define watch2(x, y)                                                           \
  ;                                                                            \
  cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define endl "\n"
#define int long long
using namespace std;

int n, k;
vector<int> adj[200005];
vector<char> color;
vector<int> parent;
int cycle_start, cycle_end;

bool dfs(int v) {
  color[v] = 1;
  for (int u : adj[v]) {
    if (color[u] == 0) {
      parent[u] = v;
      if (dfs(u))
        return true;
    } else if (color[u] == 1) {
      cycle_end = v;
      cycle_start = u;
      return true;
    }
  }
  color[v] = 2;
  return false;
}
vector<int> arr;
void find_cycle() {
  color.assign(n, 0);
  parent.assign(n, -1);
  cycle_start = -1;

  for (int v = 0; v < n; v++) {
    if (color[v] == 0 && dfs(v))
      break;
  }

  if (cycle_start == -1) {
    cout << "Acyclic" << endl;
  } else {
    vector<int> cycle;
    // cycle.push_back(cycle_start);
    for (int v = cycle_end; v != cycle_start; v = parent[v])
      cycle.push_back(v);
    cycle.push_back(cycle_start);

    reverse(cycle.begin(), cycle.end());
    int cnt = 0;
    int i = 1;
    int start = cycle[0];
    vector<int> path;
    while (i != start) {
      i = arr[i];
      path.push_back(i);
      cnt++;
    }
    // for(auto it:path)cout<<it<<" ";
    // cout<<endl;
    // watch(cnt);
    // cout << "Cycle found: ";
    // for (int v : cycle)
    //     cout << v << " ";
    // cout << endl;

    if (k <= cnt) {
      cout << path[k - 1];
    } else {
      k -= cnt;
      int cyclesize = cycle.size();
      // watch(cyclesize);
      // cout<<k<<endl;
      k = (k % cyclesize);
      // cout<<k<<endl;
      cout << cycle[k];
    }
  }
}

int32_t main() {

  fast
          // int n,k;
          cin >>
      n >> k;
  arr.resize(n + 1);

  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    adj[i].push_back(arr[i]);
  }
  find_cycle();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define watch(x)                                                               \
  ;                                                                            \
  cout << #x << "=" << x << endl;
#define watch2(x, y)                                                           \
  ;                                                                            \
  cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define endl "\n"
#define int long long
using namespace std;

int n, k;
vector<int> adj[200005];
vector<char> color;
vector<int> parent;
int cycle_start, cycle_end;

bool dfs(int v) {
  color[v] = 1;
  for (int u : adj[v]) {
    if (color[u] == 0) {
      parent[u] = v;
      if (dfs(u))
        return true;
    } else if (color[u] == 1) {
      cycle_end = v;
      cycle_start = u;
      return true;
    }
  }
  color[v] = 2;
  return false;
}
vector<int> arr;
void find_cycle() {
  color.assign(n + 1, 0);
  parent.assign(n + 1, -1);
  cycle_start = -1;

  for (int v = 0; v < n; v++) {
    if (color[v] == 0 && dfs(v))
      break;
  }

  if (cycle_start == -1) {
    cout << "Acyclic" << endl;
  } else {
    vector<int> cycle;
    // cycle.push_back(cycle_start);
    for (int v = cycle_end; v != cycle_start; v = parent[v])
      cycle.push_back(v);
    cycle.push_back(cycle_start);

    reverse(cycle.begin(), cycle.end());
    int cnt = 0;
    int i = 1;
    int start = cycle[0];
    vector<int> path;
    while (i != start) {
      i = arr[i];
      path.push_back(i);
      cnt++;
    }
    // for(auto it:path)cout<<it<<" ";
    // cout<<endl;
    // watch(cnt);
    // cout << "Cycle found: ";
    // for (int v : cycle)
    //     cout << v << " ";
    // cout << endl;

    if (k <= cnt) {
      cout << path[k - 1];
    } else {
      k -= cnt;
      int cyclesize = cycle.size();
      // watch(cyclesize);
      // cout<<k<<endl;
      k = (k % cyclesize);
      // cout<<k<<endl;
      cout << cycle[k];
    }
  }
}

int32_t main() {

  fast
          // int n,k;
          cin >>
      n >> k;
  arr.resize(n + 1);

  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    adj[i].push_back(arr[i]);
  }
  find_cycle();

  return 0;
}