Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

// struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;

template <class T> inline T sqr(T x) { return x * x; }

class UnionFind {
  struct UFNode {
    int par;
    int rank;
    UFNode() {}
  };
  vector<UFNode> nodes;

public:
  UnionFind(int n) : nodes(n) {
    for (int i = 0; i < n; i++) {
      nodes[i].par = i;
      nodes[i].rank = 0;
    }
  };
  int root(int x) {
    return nodes[x].par == x ? x : nodes[x].par = root(nodes[x].par);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (nodes[x].rank < nodes[y].rank) {
      nodes[x].par = y;
    } else {
      nodes[y].par = x;
      if (nodes[x].rank == nodes[y].rank)
        nodes[x].rank++;
    }
  }
  //~UnionFind() {cout << nodes.size() << endl;}
};

void solve() {
  LL n, m;
  cin >> n >> m;
  UnionFind u(n);
  VI p(n);
  REP(i, 0, n) cin >> p[i];
  int x, y;
  REP(i, 0, m) {
    cin >> x >> y;
    u.unite(x, y);
  }
  VI v(n);
  int cnt = 0;
  REP(i, 0, n) {
    if (u.same(i + 1, p[i]))
      ++cnt;
  }
  cout << cnt << endl;
}

int main() {
  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

// struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;

template <class T> inline T sqr(T x) { return x * x; }

class UnionFind {
  struct UFNode {
    int par;
    int rank;
    UFNode() {}
  };
  vector<UFNode> nodes;

public:
  UnionFind(int n) : nodes(n) {
    for (int i = 0; i < n; i++) {
      nodes[i].par = i;
      nodes[i].rank = 0;
    }
  };
  int root(int x) {
    // return nodes[x].par == x ? x : nodes[x].par = root(nodes[x].par);
    return nodes[x].par == x ? x : root(nodes[x].par);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (nodes[x].rank < nodes[y].rank) {
      nodes[x].par = y;
    } else {
      nodes[y].par = x;
      if (nodes[x].rank == nodes[y].rank)
        nodes[x].rank++;
    }
  }
  //~UnionFind() {cout << nodes.size() << endl;}
};

void solve() {
  LL n, m;
  cin >> n >> m;
  UnionFind u(n);
  VI p(n);
  REP(i, 0, n) cin >> p[i];
  int x, y;
  REP(i, 0, m) {
    cin >> x >> y;
    u.unite(x, y);
  }
  VI v(n);
  int cnt = 0;
  REP(i, 0, n) {
    if (u.same(i + 1, p[i]))
      ++cnt;
  }
  cout << cnt << endl;
}

int main() {
  solve();

  return 0;
}