Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// vector<vector<int> > dp;
// vector<vector<vector<int> > > vvvi;
// dp=vector<vector<int> >(N, vector<int>(M,0));
// vector<pair<int,int> > v;
// v.push_back(make_pair(x,y));
// do {
//     print(v);
//   } while (std::next_permutation(v.begin(), v.end()));

class UnionFind {
public:
  vector<int> Parent;

  UnionFind(int N) { Parent = vector<int>(N, -1); }

  int root(int A) {
    if (Parent[A] < 0)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  int size(int A) { return -Parent[root(A)]; }

  bool unite(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);

    Parent[A] += Parent[B];
    Parent[B] = A;

    return true;
  }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  VI P(N);
  VI X(M);
  VI Y(M);
  UnionFind uni(N);
  REP(i, N) cin >> P[i];
  REP(i, M) {
    cin >> X[i] >> Y[i];
    X[i]--;
    Y[i]--;
    uni.unite(X[i], Y[i]);
  }
  int ans = 0;
  eFOR(i, 1, N) {
    if (uni.root(P[i - 1] - 1) == uni.root(i))
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// vector<vector<int> > dp;
// vector<vector<vector<int> > > vvvi;
// dp=vector<vector<int> >(N, vector<int>(M,0));
// vector<pair<int,int> > v;
// v.push_back(make_pair(x,y));
// do {
//     print(v);
//   } while (std::next_permutation(v.begin(), v.end()));

class UnionFind {
public:
  vector<int> Parent;

  UnionFind(int N) { Parent = vector<int>(N, -1); }

  int root(int A) {
    if (Parent[A] < 0)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  int size(int A) { return -Parent[root(A)]; }

  bool unite(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);

    Parent[A] += Parent[B];
    Parent[B] = A;

    return true;
  }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  VI P(N);
  VI X(M);
  VI Y(M);
  UnionFind uni(N);
  REP(i, N) cin >> P[i];
  REP(i, M) {
    cin >> X[i] >> Y[i];
    X[i]--;
    Y[i]--;
    uni.unite(X[i], Y[i]);
  }
  int ans = 0;
  REP(i, N) {
    if (uni.root(P[i] - 1) == uni.root(i))
      ans++;
  }
  cout << ans << endl;
  return 0;
}