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

#include <bits/stdc++.h>
#define range(i, l, r) for (long long int(i) = (l); (i) < (r); (i)++)
#define reversed_range(i, l, r) for (long long int(i) = (r)-1; (i) >= l; (i)--)
using namespace std;
template <typename T> using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;

template <typename S, typename T>
ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "(";
  os << p.first << ", " << p.second;
  return os << ")";
}

template <typename T> ostream &operator<<(ostream &os, vec<T> v) {
  os << "[";
  if (v.size() == 0)
    return os << "]";
  for (int i = 0; i < v.size() - 1; i++) {
    os << v.at(i) << ", ";
  }
  return os << v.at(v.size() - 1) << "]";
}

template <typename T> ostream &operator<<(ostream &os, set<T> &s) {
  os << "{";
  if (s.begin() == s.end())
    return os << "}";
  auto it_first_item = s.begin();
  os << *it_first_item;
  for (auto it = ++it_first_item; it != s.end(); it++) {
    os << ", " << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, unordered_set<T> &s) {
  os << "{";
  if (s.begin() == s.end())
    return os << "}";
  auto it_first_item = s.begin();
  os << *it_first_item;
  for (auto it = ++it_first_item; it != s.end(); it++) {
    os << ", " << *it;
  }
  return os << "}";
}

template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> m) {
  os << "{";
  if (m.begin() == m.end())
    return os << "}";
  auto it_first_item = m.begin();
  os << it_first_item->first << ": " << it_first_item->second;
  for (auto it = ++it_first_item; it != m.end(); it++) {
    os << ", " << it->first << ": " << it->second;
  }
  return os << "}";
}

template <typename K, typename V>
ostream &operator<<(ostream &os, unordered_map<K, V> m) {
  os << "{";
  if (m.begin() == m.end())
    return os << "}";
  auto it_first_item = m.begin();
  os << it_first_item->first << ": " << it_first_item->second;
  for (auto it = ++it_first_item; it != m.end(); it++) {
    os << ", " << it->first << ": " << it->second;
  }
  return os << "}";
}

lint pow(lint num, lint e, lint MOD) {
  lint res = 1;
  lint cur_num = num;
  while (e) {
    if (e & 1) {
      res *= cur_num;
      res %= MOD;
    }
    cur_num *= cur_num;
    cur_num %= MOD;
    e >>= 1;
  }
  return res;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  lint N, K;
  cin >> N >> K;
  vec<lint> A_list(N);
  range(i, 0, N) {
    cin >> A_list.at(i);
    A_list.at(i) -= 1;
  }
  deque<lint> cities_in_cycle;
  lint cur_city = 0;
  vec<bool> visited(N, false);
  while (!visited.at(cur_city)) {
    visited.at(cur_city) = true;
    cities_in_cycle.emplace_back(cur_city);
    cur_city = A_list.at(cur_city);
  }

  while (cities_in_cycle.front() != cur_city) {
    if (K == 0) {
      cout << cities_in_cycle.front() + 1 << "\\n";
      return 0;
    }
    cities_in_cycle.pop_front();
    K -= 1;
  }
  lint cycle_length = cities_in_cycle.size();
  K %= cycle_length;
  cout << cities_in_cycle.at(K) + 1 << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, l, r) for (long long int(i) = (l); (i) < (r); (i)++)
#define reversed_range(i, l, r) for (long long int(i) = (r)-1; (i) >= l; (i)--)
using namespace std;
template <typename T> using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;

template <typename S, typename T>
ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "(";
  os << p.first << ", " << p.second;
  return os << ")";
}

template <typename T> ostream &operator<<(ostream &os, vec<T> v) {
  os << "[";
  if (v.size() == 0)
    return os << "]";
  for (int i = 0; i < v.size() - 1; i++) {
    os << v.at(i) << ", ";
  }
  return os << v.at(v.size() - 1) << "]";
}

template <typename T> ostream &operator<<(ostream &os, set<T> &s) {
  os << "{";
  if (s.begin() == s.end())
    return os << "}";
  auto it_first_item = s.begin();
  os << *it_first_item;
  for (auto it = ++it_first_item; it != s.end(); it++) {
    os << ", " << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, unordered_set<T> &s) {
  os << "{";
  if (s.begin() == s.end())
    return os << "}";
  auto it_first_item = s.begin();
  os << *it_first_item;
  for (auto it = ++it_first_item; it != s.end(); it++) {
    os << ", " << *it;
  }
  return os << "}";
}

template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> m) {
  os << "{";
  if (m.begin() == m.end())
    return os << "}";
  auto it_first_item = m.begin();
  os << it_first_item->first << ": " << it_first_item->second;
  for (auto it = ++it_first_item; it != m.end(); it++) {
    os << ", " << it->first << ": " << it->second;
  }
  return os << "}";
}

template <typename K, typename V>
ostream &operator<<(ostream &os, unordered_map<K, V> m) {
  os << "{";
  if (m.begin() == m.end())
    return os << "}";
  auto it_first_item = m.begin();
  os << it_first_item->first << ": " << it_first_item->second;
  for (auto it = ++it_first_item; it != m.end(); it++) {
    os << ", " << it->first << ": " << it->second;
  }
  return os << "}";
}

lint pow(lint num, lint e, lint MOD) {
  lint res = 1;
  lint cur_num = num;
  while (e) {
    if (e & 1) {
      res *= cur_num;
      res %= MOD;
    }
    cur_num *= cur_num;
    cur_num %= MOD;
    e >>= 1;
  }
  return res;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  lint N, K;
  cin >> N >> K;
  vec<lint> A_list(N);
  range(i, 0, N) {
    cin >> A_list.at(i);
    A_list.at(i) -= 1;
  }
  deque<lint> cities_in_cycle;
  lint cur_city = 0;
  vec<bool> visited(N, false);
  while (!visited.at(cur_city)) {
    visited.at(cur_city) = true;
    cities_in_cycle.emplace_back(cur_city);
    cur_city = A_list.at(cur_city);
  }
  while (!cities_in_cycle.empty() and cities_in_cycle.front() != cur_city) {
    cities_in_cycle.pop_front();
    K -= 1;
  }
  // for (const auto& city : cities_in_cycle) cout << city << " ";
  // cout << '\\n';
  lint cycle_length = cities_in_cycle.size();
  K %= cycle_length;
  cout << cities_in_cycle.at(K) + 1 << "\\n";
}