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

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define read(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define print(x)                                                               \\
  for (int i = 0; i < (int)x.size(); i++)                                      \\
    cout << x[i] << " ";                                                       \\
  cout << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pi;

const ll INFLL = (ll)1e16;
const int INF = (int)1e8;

vector<bool> visited((int)2e5 + 5, false);
vector<ll> path;
vector<ll> teleport((int)2e5 + 5);
ll cycle_begins;

void dfs(int source) {
  if (visited[source]) {
    cycle_begins = source;
  } else {
    visited[source] = true;
    path.push_back(source);
    dfs(teleport[source]);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    ll t;
    cin >> t;
    teleport[i] = t;
  }
  dfs(1);

  ll non_cycle = 0;
  for (int i = 0; i < (int)path.size(); i++) {
    if (path[i] == cycle_begins)
      break;
    non_cycle++;
  }

  ll cycle = (ll)path.size() - non_cycle;
  // cout<<non_cycle<<" "<<cycle<<endl;
  if (k < non_cycle)
    cout << path[k] << endl;
  else {
    k -= non_cycle;
    cout << path[non_cycle + (k % cycle)] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define read(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define print(x)                                                               \\
  for (int i = 0; i < (int)x.size(); i++)                                      \\
    cout << x[i] << " ";                                                       \\
  cout << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pi;

const ll INFLL = (ll)1e16;
const int INF = (int)1e8;

vector<bool> visited((int)2e5 + 5, false);
vector<ll> path;
vector<ll> teleport((int)2e5 + 5);

ll dfs(int source) {
  if (visited[source])
    return source;
  visited[source] = true;
  path.push_back(source);
  dfs(teleport[source]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    ll t;
    cin >> t;
    teleport[i] = t;
  }
  int cycle_begins = dfs(1);

  ll non_cycle = 0;
  for (int i = 0; i < (int)path.size(); i++) {
    if (path[i] == cycle_begins)
      break;
    non_cycle++;
  }

  ll cycle = (ll)path.size() - non_cycle;
  // cout<<non_cycle<<" "<<cycle<<endl;
  if (k < non_cycle)
    cout << path[k] << endl;
  else {
    k -= non_cycle;
    cout << path[non_cycle + (k % cycle)] << endl;
  }

  return 0;
}