Problem p03354 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define rep0(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define repU(i, bottom, ceiling)                                               \
  for (int(i) = (bottom); (i) <= (ceiling); (i)++)
#define repD(i, ceiling, bottom)                                               \
  for (int(i) = (ceiling); (i) >= (bottom); (i)--)
#define V vector
#define Vi vector<int>
#define VVi vector<vector<int>>
#define pub push_back
#define pob pop_back
#define pf printf
#define sf scanf
#define ci(a) cin >> a
#define lco(a) cout << a << endl
#define co(a) cout << a
#define Vci(v)                                                                 \
  for (int i = 0; i < v.size(); i++)                                           \
  cin >> v[i]
#define Vco(v)                                                                 \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i] << ' ';                                                       \
  }                                                                            \
  cout << endl
#define P pair
#define Pii pair<int, int>
#define Mp(a, b) make_pair(a, b)
#define fir first
#define sec second
#define Re return

typedef long long ll;
typedef double db;

const string alphabet("abcdefghijklmnopqrstuvwxyz");
const vector<vector<int>> Dir = {{0, 0},  {1, 0},   {1, 1},  {0, 1}, {-1, 1},
                                 {0, -1}, {-1, -1}, {0, -1}, {1, -1}};
const long double pai = 3.141592653589793238462643383279502884197;
const long double ena = 2.71828182845904523536;
const int Inf = (int)2e9 + 1e8;
const int Mod = (int)1e9 + 7;

template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir + r.fir, l.sec + r.sec};
}

template <typename T, typename U>
pair<T, U> operator-(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir - r.fir, l.sec - r.sec};
}

template <typename T, typename U>
pair<T, U> operator*(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir * r.fir, l.sec * r.sec};
}

template <typename T, typename U>
pair<T, U> operator/(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir / r.fir, l.sec / r.sec};
}

ll rem(ll n, ll m) {
  if (!m)
    Re - 1;
  Re(n < 0 ? n % m + m : n % m);
}

/*The main code follows below*/

int ParSet(int start, int *par) {
  if (start != par[start])
    return par[start] = ParSet(par[start], par);
  return start;
}

int main() {
  int ar[100005], p[100005], n, m, ans = 0;
  ci(n >> m);
  rep0(i, n) {
    ci(ar[i]);
    ar[i]--;
    p[i] = i;
  }
  rep0(i, m) {
    int x, y;
    ci(x >> y);
    x--;
    y--;
    ParSet(x, p);
    p[p[x]] = p[y];
  }
  rep0(j, n) ParSet(j, p);
  rep0(i, n) if (p[i] == p[ar[i]]) ans++;
  lco(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep0(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define repU(i, bottom, ceiling)                                               \
  for (int(i) = (bottom); (i) <= (ceiling); (i)++)
#define repD(i, ceiling, bottom)                                               \
  for (int(i) = (ceiling); (i) >= (bottom); (i)--)
#define V vector
#define Vi vector<int>
#define VVi vector<vector<int>>
#define pub push_back
#define pob pop_back
#define pf printf
#define sf scanf
#define ci(a) cin >> a
#define lco(a) cout << a << endl
#define co(a) cout << a
#define Vci(v)                                                                 \
  for (int i = 0; i < v.size(); i++)                                           \
  cin >> v[i]
#define Vco(v)                                                                 \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i] << ' ';                                                       \
  }                                                                            \
  cout << endl
#define P pair
#define Pii pair<int, int>
#define Mp(a, b) make_pair(a, b)
#define fir first
#define sec second
#define Re return

typedef long long ll;
typedef double db;

const string alphabet("abcdefghijklmnopqrstuvwxyz");
const vector<vector<int>> Dir = {{0, 0},  {1, 0},   {1, 1},  {0, 1}, {-1, 1},
                                 {0, -1}, {-1, -1}, {0, -1}, {1, -1}};
const long double pai = 3.141592653589793238462643383279502884197;
const long double ena = 2.71828182845904523536;
const int Inf = (int)2e9 + 1e8;
const int Mod = (int)1e9 + 7;

template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir + r.fir, l.sec + r.sec};
}

template <typename T, typename U>
pair<T, U> operator-(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir - r.fir, l.sec - r.sec};
}

template <typename T, typename U>
pair<T, U> operator*(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir * r.fir, l.sec * r.sec};
}

template <typename T, typename U>
pair<T, U> operator/(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir / r.fir, l.sec / r.sec};
}

ll rem(ll n, ll m) {
  if (!m)
    Re - 1;
  Re(n < 0 ? n % m + m : n % m);
}

/*The main code follows below*/

int ParSet(int start, int *par) {
  if (start != par[start])
    return par[start] = ParSet(par[start], par);
  return start;
}

int main() {
  int ar[100005], p[100005], n, m, ans = 0;
  ci(n >> m);
  rep0(i, n) {
    ci(ar[i]);
    ar[i]--;
    p[i] = i;
  }
  rep0(i, m) {
    int x, y;
    ci(x >> y);
    x--;
    y--;
    ParSet(x, p);
    ParSet(y, p);
    p[p[x]] = p[y];
  }
  rep0(j, n) ParSet(j, p);
  rep0(i, n) if (p[i] == p[ar[i]]) ans++;
  lco(ans);
  return 0;
}