Language: C++

/*
* 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 <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <climits>

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 rank[UNION_FIND_MAX_N];

void init(int n){
    for(int i=0; i<n; i++){
        par[i] = i;
        rank[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( 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() {

    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;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:79:9: error: reference to 'rank' is ambiguous
   79 |         rank[i] = 0;
      |         ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:11:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:74:5: note:                 'int rank [10005]'
   74 | int rank[UNION_FIND_MAX_N];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:93:9: error: reference to 'rank' is ambiguous
   93 |     if( rank[x] < rank[y] ){
      |         ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:74:5: note:                 'int rank [10005]'
   74 | int rank[UNION_FIND_MAX_N];
      |     ^~~~
a.cc:93:19: error: reference to 'rank' is ambiguous
   93 |     if( rank[x] < rank[y] ){
      |                   ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:74:5: note:                 'int rank [10005]'
   74 | int rank[UNION_FIND_MAX_N];
      |     ^~~~
a.cc:97:13: error: reference to 'rank' is ambiguous
   97 |         if( rank[x] == rank[y] ) rank[x]++;
      |             ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:74:5: note:                 'int rank [10005]'
   74 | int rank[UNION_FIND_MAX_N];
      |     ^~~~
a.cc:97:24: error: reference to 'rank' is ambiguous
   97 |         if( rank[x] == rank[y] ) rank[x]++;
      |                        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:74:5: note:                 'int rank [10005]'
   74 | int rank[UNION_FIND_MAX_N];
      |     ^~~~
a.cc:97:34: error: reference to 'rank' is ambiguous
   97 |         if( rank[x] == rank[y] ) rank[x]++;
      |                                  ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:74:5: note:                 'int rank [10005]'
   74 | int rank[UNION_FIND_MAX_N];
      |     ^~~~