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

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

#define IOS                                                                    \\
  std::cin.tie(0);                                                             \\
  std::ios_base::sync_with_stdio(false)

#define ll long long
#define Fi first
#define Se second
#define Pb push_back
#define vi vector<int>
#define vp vector<pair<int, int>>
#define vvi vector<vector<int>>
#define vvp vector<vector<pair<int, int>>>

#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define output_s(s) printf("%s\\n", (s).c_str())
#define END cout << "\\n"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define RepS(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RRepS(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define SRepS(i, n, m) for (int i = (int)(n); i * i <= (int)(m); i++)
#define RepA(x) for (auto &item : (x))

void CINT() {} //@shibh308's template
template <class Head, class... Tail> void CINT(Head &&head, Tail &&...tail) {
  cin >> head;
  CINT(move(tail)...);
}
#define CIN(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  CINT(__VA_ARGS__)
#define LCIN(...)                                                              \\
  ll __VA_ARGS__;                                                              \\
  CINT(__VA_ARGS__)
#define SCIN(...)                                                              \\
  string __VA_ARGS__;                                                          \\
  CINT(__VA_ARGS__)
#define VCIN(x)                                                                \\
  for (auto &item : x)                                                         \\
  cin >> item

void OUT() {}
template <class Head, class... Tail> void OUT(Head &&head, Tail &&...tail) {
  cout << head << (sizeof...(tail) ? " " : "\\n");
  OUT(move(tail)...);
}
#define COUT(...) OUT(__VA_ARGS__)

template <typename TypeInt> TypeInt GCD(TypeInt a, TypeInt b) {
  return a % b == 0 ? b : GCD(b, a % b);
} // Greatest Common Divisor
template <typename TypeInt> TypeInt LCM(TypeInt a, TypeInt b) {
  return a * b / GCD(a, b);
} // Least Common Multiple
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool comp(T a, T b) { return abs(a) < abs(b); } // 比較関数

void YES(bool f) { printf(f ? "YES\\n" : "NO\\n"); }
void Yes(bool f) { printf(f ? "Yes\\n" : "No\\n"); }
void Possible(bool f) { printf(f ? "Possible\\n" : "Impossible\\n"); }
void POSSIBLE(bool f) { printf(f ? "POSSIBLE\\n" : "IMPOSSIBLE\\n"); }

//---------------------Template------------------------//

// Prime Factorization
// The result is stored in the array
void PrimeFactorize(long long num, vector<long long> &ret) {
  vector<long long> prime(num + 1, 0);
  while (num % 2 == 0) {
    num /= 2;
    prime[2]++;
  }
  for (long long i = 3; i <= num; i++) {
    while (num % i == 0) {
      num /= i;
      prime[i]++;
    }
  }
  copy(prime.begin(), prime.end(), back_inserter(ret));
}

// Judge the prime number
bool IsPrime(int x) {
  if (x < 2)
    return false;
  else if (x == 2)
    return true;
  else if (x % 2 == 0)
    return false;
  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

// Meguru binary search
// left < right
template <typename T> class BinarySearch {
  T _l;
  T _r;
  function<bool(T)> _f;

public:
  BinarySearch(T left, T right, function<bool(T)> func) {
    this->_l = left;
    this->_r = right;
    this->_f = func;
  }
  ~BinarySearch() = default;
  T calc() {
    while ((this->_r - this->_l) > 1) {
      T middle = (this->_l + this->_r) / 2;
      if (this->_f(middle)) {
        this->_r = middle;
      } else {
        this->_l = middle;
      }
    }
    return this->_r;
  }
};

// Dijkstra
// type->long long int
class Graph {
  struct edge {
    ll to;
    ll cost;
  };
  ll v; // vertex
  const ll INF = 1e18;
  vector<list<edge>> G;

public:
  Graph(ll _v) : G(_v) { this->v = _v; };
  ~Graph() { vector<list<edge>>().swap(G); };
  /**
   * @param _f    from
   * @param _t    to
   * @param _c    cost (default = 1)
   */
  void addEdge(ll _f, ll _t, ll _c = 1) { G[_f].push_back(edge{_t, _c}); }
  /**
   * Dijkstra
   * @param _f Start position
   * @param _t Destination
   */
  ll shortestPath(ll _f, ll _t) {
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
        que;
    vector<ll> dist(v, INF);

    que.push(make_pair(0, _f));
    dist[_f] = 0;

    while (!que.empty()) {
      ll from = que.top().second;
      que.pop();

      list<edge>::iterator it;
      for (it = G[from].begin(); it != G[from].end(); it++) {
        ll to = it->to;
        ll cost = it->cost;
        if (dist[to] > dist[from] + cost) {
          dist[to] = dist[from] + cost;
          que.push(make_pair(dist[to], to));
        }
      }
    }
    return dist[_t];
  }
  bool isCycle(ll _f) {
    vector<bool> seen(v, false);
    vector<vector<ll>> root(v);

    dfs(0, _f, root, seen);

    for (ll it = 0; it < (ll)root.size() - 1; it++) {
      if (root[it][0] == 0)
        return true;
    }
    return false;
  }
  // direcrted cycle graph or directed acyclic graph root
  vector<vector<ll>> Root(ll _f) {
    vector<bool> seen(v, false);
    vector<vector<ll>> root(v);

    dfs(0, _f, root, seen);
    return root;
  }
  // preorder
  void dfs(ll d, ll f, vector<vector<ll>> &v, vector<bool> &s) {
    s[f] = true;
    v[f].push_back(d);
    list<edge>::iterator it;
    for (it = G[f].begin(); it != G[f].end(); it++) {
      if (s[it->to]) {
        v[it->to].push_back(d + it->cost);
        continue;
      }

      dfs(d + it->cost, it->to, v, s);
    }
  }
};

//----------------------Library------------------------//

const int dx4[] = {1, 0, -1, 0}, dx8[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy4[] = {0, 1, 0, -1}, dy8[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int NMAX = 55;
const int INF = 1e9;
const long long LINF = 1e18;
const double EPS = 1e-10;
const int MOD = 1e9 + 7;
bool flag = false;

//----------------------Global-------------------------//

signed main() {
  LCIN(N, K);
  Graph g(N);
  rep(i, N) {
    ll a;
    cin >> a;
    a--;
    g.addEdge(i, a);
  }
  vector<vector<ll>> vec(N);
  vec = g.Root(0);
  ll loop = 0, cur = 0;
  rep(i, N) {
    if (vec[i].empty())
      vec[i].push_back(N + 1);
    if (vec[i].size() == 2) {
      loop = vec[i][1] - vec[i][0];
      cur = vec[i][0];
    }
  }
  K++;
  if (K >= loop + cur) {
    K -= (loop + cur);
    K %= loop;
    if (K == 0)
      K += loop;
    K += cur;
  }
  K--;
  rep(i, N) if (vec[i][0] == K) COUT(i + 1);

  return 0;
}

Error: 0

Fixed submission:

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

#define IOS                                                                    \\
  std::cin.tie(0);                                                             \\
  std::ios_base::sync_with_stdio(false)

#define ll long long
#define Fi first
#define Se second
#define Pb push_back
#define vi vector<int>
#define vp vector<pair<int, int>>
#define vvi vector<vector<int>>
#define vvp vector<vector<pair<int, int>>>

#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define output_s(s) printf("%s\\n", (s).c_str())
#define END cout << "\\n"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define RepS(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RRepS(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define SRepS(i, n, m) for (int i = (int)(n); i * i <= (int)(m); i++)
#define RepA(x) for (auto &item : (x))

void CINT() {} //@shibh308's template
template <class Head, class... Tail> void CINT(Head &&head, Tail &&...tail) {
  cin >> head;
  CINT(move(tail)...);
}
#define CIN(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  CINT(__VA_ARGS__)
#define LCIN(...)                                                              \\
  ll __VA_ARGS__;                                                              \\
  CINT(__VA_ARGS__)
#define SCIN(...)                                                              \\
  string __VA_ARGS__;                                                          \\
  CINT(__VA_ARGS__)
#define VCIN(x)                                                                \\
  for (auto &item : x)                                                         \\
  cin >> item

void OUT() {}
template <class Head, class... Tail> void OUT(Head &&head, Tail &&...tail) {
  cout << head << (sizeof...(tail) ? " " : "\\n");
  OUT(move(tail)...);
}
#define COUT(...) OUT(__VA_ARGS__)

template <typename TypeInt> TypeInt GCD(TypeInt a, TypeInt b) {
  return a % b == 0 ? b : GCD(b, a % b);
} // Greatest Common Divisor
template <typename TypeInt> TypeInt LCM(TypeInt a, TypeInt b) {
  return a * b / GCD(a, b);
} // Least Common Multiple
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool comp(T a, T b) { return abs(a) < abs(b); } // 比較関数

void YES(bool f) { printf(f ? "YES\\n" : "NO\\n"); }
void Yes(bool f) { printf(f ? "Yes\\n" : "No\\n"); }
void Possible(bool f) { printf(f ? "Possible\\n" : "Impossible\\n"); }
void POSSIBLE(bool f) { printf(f ? "POSSIBLE\\n" : "IMPOSSIBLE\\n"); }

//---------------------Template------------------------//

// Prime Factorization
// The result is stored in the array
void PrimeFactorize(long long num, vector<long long> &ret) {
  vector<long long> prime(num + 1, 0);
  vector<long long>::iterator it;
  while (num % 2 == 0) {
    num /= 2;
    prime[2]++;
  }
  for (long long i = 3; i <= num; i++) {
    while (num % i == 0) {
      num /= i;
      prime[i]++;
    }
  }
  copy(prime.begin(), prime.end(), back_inserter(ret));
}

// Judge the prime number
bool IsPrime(int x) {
  if (x < 2)
    return false;
  else if (x == 2)
    return true;
  else if (x % 2 == 0)
    return false;
  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

// Meguru binary search
// left < right
template <typename T> class BinarySearch {
  T _l;
  T _r;
  function<bool(T)> _f;

public:
  BinarySearch(T left, T right, function<bool(T)> func) {
    this->_l = left;
    this->_r = right;
    this->_f = func;
  }
  ~BinarySearch() = default;
  T calc() {
    while ((this->_r - this->_l) > 1) {
      T middle = (this->_l + this->_r) / 2;
      if (this->_f(middle)) {
        this->_r = middle;
      } else {
        this->_l = middle;
      }
    }
    return this->_r;
  }
};

// Dijkstra
// 範囲外エラーが起きるので使用不可
template <typename T> class Dijkstra {
  T v; // vertex
  struct edge {
    T to;
    T cost;
  };
  const T INF = 1e9;

public:
  vector<vector<edge>> Graph;
  vector<T> dist;

  Dijkstra(T _v) : Graph(v), dist(v, INF) { this->v = _v; };
  virtual ~Dijkstra() { vector<vector<edge>>().swap(Graph); };
  /**
   * @param _f    from
   * @param _t    to
   * @param _c    cost (default = 1)
   */
  void addEdge(T _f, T _t, T _c = 1) { Graph[_f].push_back((edge){_t, _c}); }
  /**
   * @param _f Start position
   * @param _t Destination
   */
  T shortestPath(T _f, T _t) {
    priority_queue<pair<T, T>, vector<pair<T, T>>, greater<pair<T, T>>> que;

    que.push(make_pair(0, _f));
    dist[_f] = 0;

    while (!que.empty()) {
      T v = que.top().second;
      que.pop();

      for (int i = 0; i < Graph[v].size(); i++) {
        edge e = Graph[v][i];
        if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = dist[v] + e.cost;
          que.push(make_pair(dist[e.to], e.to));
        }
      }
    }
    return dist[_t];
  }
};

//----------------------Library------------------------//

const int dx4[] = {1, 0, -1, 0}, dx8[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy4[] = {0, 1, 0, -1}, dy8[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int NMAX = 55;
const int INF = 1e9;
const long long LINF = 1e18;
const double EPS = 1e-10;
const int MOD = 1e9 + 7;
bool flag = false;

//----------------------Global-------------------------//

signed main() {
  int N;
  ll K;
  vector<int> g(N + 1);
  cin >> N >> K;

  rep(i, N) { cin >> g[i + 1]; }
  vector<bool> v(N + 1, false);
  vector<int> root = {1};
  int now = 1;
  v[now] = true;
  while (1) {
    if (!v[g[now]]) {
      root.push_back(g[now]);
      v[g[now]] = true;
      now = g[now];
    } else {
      now = g[now];
      break;
    }
  }
  int cnt = root.size();
  rep(i, root.size()) {
    if (root[i] == now)
      break;
    else
      cnt--;
  }
  if (K >= root.size())
    K -= (root.size() - cnt);
  int tmp = K % cnt;
  COUT(root[sz(root) - cnt + tmp]);

  return 0;
}