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

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 9999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \\
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 10000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> N >> K;
  ;
  Vec(Q, lli, N, 0);
  rep(i, 0, N) cin >> Q[i];
  B = 5 * N;
  Vec(P, lli, N, 0);
  P[0] = 1;
  int a = 1;
  while (B > 0) {
    a = Q[a - 1];
    B--;
    P[a - 1]++;
  }
  rep(i, 0, N) if (P[i] > 1) num++;
  rep(i, 0, N) if (P[i] == 1) sum++;
  if (K > sum + num)
    B = sum + (K - sum + num) % num;
  else
    B = K;
  a = 1;
  while (B > 0) {
    a = Q[a - 1];
    B--;
  }
  Out(a);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 9999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \\
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 10000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> N >> K;
  UnionFind uf(N);
  Vec(Q, lli, N, 0);
  rep(i, 0, N) {
    cin >> Q[i];
    uf.unite(i, Q[i] - 1);
  }
  B = 2 * N;
  Vec(P, lli, N, 0);
  P[0] = 1;
  int a = 1;
  while (B > 0) {
    a = Q[a - 1];
    B--;
    P[a]++;
  }
  rep(i, 0, N) if (P[i] > 1) num++;
  A = uf.size(0);
  B = (K - A + num) % A;
  a = 1;
  while (B > 0) {
    a = Q[a - 1];
    B--;
  }
  Out(a);
}