Problem p03354 (C++)

Buggy submission (status: Runtime Error):
/*
g++ -std=c++11 -o2 -Wall ABC097D.cpp -o ./out/ABC097D.out
./out/ABC097D.out
*/

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

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define tr(c, i) for (iter(c) != (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define mp make_pair

typedef long long ll;

class Union_Find {
private:
  vector<int> par, rank;

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

public:
  Union_Find(int Max_Node) {
    par.resize(Max_Node);
    rank.resize(Max_Node);
  }

  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return (find(x) == find(y)); }
};

int main() {
  freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> in(n);
  for (int i = 0; i < n; i++)
    cin >> in[i];
  Union_Find uf(n + 10);
  uf.init(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
    uf.unite(b, a);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (uf.same(in[i], i + 1)) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*
g++ -std=c++11 -o2 -Wall ABC097D.cpp -o ./out/ABC097D.out
./out/ABC097D.out
*/

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

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define tr(c, i) for (iter(c) != (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define mp make_pair

typedef long long ll;

class Union_Find {
private:
  vector<int> par, rank;

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

public:
  Union_Find(int Max_Node) {
    par.resize(Max_Node);
    rank.resize(Max_Node);
  }

  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return (find(x) == find(y)); }
};

int main() {
  //    freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> in(n);
  for (int i = 0; i < n; i++)
    cin >> in[i];
  Union_Find uf(n + 10);
  uf.init(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
    uf.unite(b, a);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (uf.same(in[i], i + 1)) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}