Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> ve_vel;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<bool>> ve_veb;
typedef vector<vector<string>> ve_ves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define puf push_front
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define MAX_N 100005
int par[MAX_N]; // 親
int ran[MAX_N]; // 木の深さ

// n要素で初期化
void init(int n) {
  rep(i, n) {
    par[i] = i;
    ran[i] = 0;
  }
}

// 木の根を求める
int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

// x,yの属する集合の併合
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;

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

// x,yが同じ集合に属するか
bool same(int x, int y) { return root(x) == root(y); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vei p(N);
  rep(i, N) {
    cin >> p[i];
    p[i]--;
    par[i] = i;
  }
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    unite(x, y);
  }
  int cnt = 0;
  rep(i, M) if (same(p[i], i)) cnt++;
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> ve_vel;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<bool>> ve_veb;
typedef vector<vector<string>> ve_ves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define puf push_front
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define MAX_N 100005
int par[MAX_N]; // 親
int ran[MAX_N]; // 木の深さ

// n要素で初期化
void init(int n) {
  rep(i, n) {
    par[i] = i;
    ran[i] = 0;
  }
}

// 木の根を求める
int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

// x,yの属する集合の併合
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;

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

// x,yが同じ集合に属するか
bool same(int x, int y) { return root(x) == root(y); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vei p(N);
  rep(i, N) {
    cin >> p[i];
    p[i]--;
    par[i] = i;
  }
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    unite(x, y);
  }
  int cnt = 0;
  rep(i, N) if (same(i, p[i])) cnt++;
  cout << cnt << endl;
  return 0;
}