Problem p03354 (C++)

Buggy submission (status: Runtime Error):
/*
 * Note: This template uses some c++11 functions , so you have to compile it
 * with c++11 flag. Example:-   $ g++ -std=c++11 c++Template.cpp
 *
 * Author : Akshay Pratap Singh
 * Handle: code_crack_01
 *
 */

/********   All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
// #include <unordered_map>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <utility>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

#define UNION_FIND_MAX_N 10005
int par[UNION_FIND_MAX_N];
int ran[UNION_FIND_MAX_N];

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

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

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

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

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

int main() {

  int N, M;
  cin >> N >> M;
  vi p(N);
  REP(i, N) {
    cin >> p[i];
    p[i]--;
  }

  init(N);

  int x, y;
  REP(i, M) {
    cin >> x >> y;
    x--;
    y--;
    unite(x, y);
  }

  vi used(N, 0);

  int res = 0;
  REP(i, N) {
    if (same(i, p[i]))
      res++;
    else if (i == p[i])
      res++;
  }

  cout << res << endl;

  return 0;
}


Fixed submission:
/*
 * Note: This template uses some c++11 functions , so you have to compile it
 * with c++11 flag. Example:-   $ g++ -std=c++11 c++Template.cpp
 *
 * Author : Akshay Pratap Singh
 * Handle: code_crack_01
 *
 */

/********   All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
// #include <unordered_map>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <utility>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

#define UNION_FIND_MAX_N 100005
int par[UNION_FIND_MAX_N];
int ran[UNION_FIND_MAX_N];

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

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

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

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

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

int main() {

  int N, M;
  cin >> N >> M;
  vi p(N);
  REP(i, N) {
    cin >> p[i];
    p[i]--;
  }

  init(N);

  int x, y;
  REP(i, M) {
    cin >> x >> y;
    x--;
    y--;
    unite(x, y);
  }

  vi used(N, 0);

  int res = 0;
  REP(i, N) {
    if (same(i, p[i]))
      res++;
    else if (i == p[i])
      res++;
  }

  cout << res << endl;

  return 0;
}