Problem p02684 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
#include <string>

#include <vector>

using namespace std;

class cycle_finder {
public:
  vector<vector<int>> adj;
  vector<int> visit_state;
  vector<int> parent;

  int cycle_start;
  int cycle_end;

  vector<vector<int>> cycle_list;

public:
  cycle_finder(int vertex_count)
      : adj(), visit_state(), parent(), cycle_start(-1), cycle_end(-1),
        cycle_list() {
    adj.assign(vertex_count, vector<int>());
    visit_state.assign(vertex_count, 0);
    parent.assign(vertex_count, -1);
  }

public:
  void process() {
    for (int i = 0; i < (int)visit_state.size(); i++) {
      if (visit_state[i] == 0 && inner_process(i)) {
        vector<int> cycle;
        for (int curr = cycle_end; curr != cycle_start; curr = parent[curr]) {
          visit_state[curr] = 2;
          cycle.push_back(curr);
        }
        visit_state[cycle_start] = 2;
        cycle.push_back(cycle_start);

        reverse(cycle.begin(), cycle.end());

        cycle_list.push_back(cycle);
      }
    }
  }

  bool inner_process(int curr) {
    visit_state[curr] = 1;
    for (int next : adj[curr]) {
      if (visit_state[next] == 0) {
        parent[next] = curr;
        if (inner_process(next))
          return true;
      } else if (visit_state[next] == 1) {
        cycle_end = curr;
        cycle_start = next;
        return true;
      }
    }
    visit_state[curr] = 2;
    return false;
  }
};

// cycle_finder cf(N);
// cf.adj[u - 1].push_back(v - 1);
// cf.process();
// int ret = cf.cycle_list.size();

const int threshold = 2e5;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  long long N, K;
  cin >> N >> K;

  cycle_finder cf(N);

  for (int i = 0; i < N; i++) {
    int port;
    cin >> port;

    cf.adj[i].push_back(port - 1);
  }

  cf.process();

  if (K <= threshold) {
    int result = 0;
    int curr = 0;
    for (int i = 0; i < K; i++) {
      result = cf.adj[curr][0];
      curr = result;
    }

    cout << result + 1;
  } else {
    int cycle_start = cf.cycle_list[0][0];
    int cycle_size = cf.cycle_list[0].size();

    int before_cycle = 0;
    for (int i = 0; i != cycle_start; i = cf.adj[i][0]) {
      before_cycle++;
    }

    K -= before_cycle;
    K %= cycle_size;

    int result = cycle_start;
    int curr = cycle_start;
    for (int i = 0; i < K; i++) {
      result = cf.adj[curr][0];
      curr = result;
    }

    cout << result + 1;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
#include <string>

#include <vector>

using namespace std;

class cycle_finder {
public:
  vector<vector<int>> adj;
  vector<int> visit_state;
  vector<int> parent;

  int cycle_start;
  int cycle_end;

  vector<vector<int>> cycle_list;

public:
  cycle_finder(int vertex_count)
      : adj(), visit_state(), parent(), cycle_start(-1), cycle_end(-1),
        cycle_list() {
    adj.assign(vertex_count, vector<int>());
    visit_state.assign(vertex_count, 0);
    parent.assign(vertex_count, -1);
  }

public:
  void process() {
    for (int i = 0; i < (int)visit_state.size(); i++) {
      if (visit_state[i] == 0 && inner_process(i)) {
        vector<int> cycle;
        for (int curr = cycle_end; curr != cycle_start; curr = parent[curr]) {
          visit_state[curr] = 2;
          cycle.push_back(curr);
        }
        visit_state[cycle_start] = 2;
        cycle.push_back(cycle_start);

        reverse(cycle.begin(), cycle.end());

        cycle_list.push_back(cycle);

        break;
      }
    }
  }

  bool inner_process(int curr) {
    visit_state[curr] = 1;
    for (int next : adj[curr]) {
      if (visit_state[next] == 0) {
        parent[next] = curr;
        if (inner_process(next))
          return true;
      } else if (visit_state[next] == 1) {
        cycle_end = curr;
        cycle_start = next;
        return true;
      }
    }
    visit_state[curr] = 2;
    return false;
  }
};

// cycle_finder cf(N);
// cf.adj[u - 1].push_back(v - 1);
// cf.process();
// int ret = cf.cycle_list.size();

const int threshold = 2e5;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  long long N, K;
  cin >> N >> K;

  cycle_finder cf(N);

  for (int i = 0; i < N; i++) {
    int port;
    cin >> port;

    cf.adj[i].push_back(port - 1);
  }

  cf.process();

  if (K <= threshold) {
    int result = 0;
    int curr = 0;
    for (int i = 0; i < K; i++) {
      result = cf.adj[curr][0];
      curr = result;
    }

    cout << result + 1;
  } else {
    int cycle_start = cf.cycle_list[0][0];
    int cycle_size = cf.cycle_list[0].size();

    int before_cycle = 0;
    for (int i = 0; i != cycle_start; i = cf.adj[i][0]) {
      before_cycle++;
    }

    K -= before_cycle;
    K %= cycle_size;

    int result = cycle_start;
    int curr = cycle_start;
    for (int i = 0; i < K; i++) {
      result = cf.adj[curr][0];
      curr = result;
    }

    cout << result + 1;
  }

  return 0;
}