Language: C++

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric> // accumulate(v.begin(), v.end(), 0)

using namespace std;
#define ll long long

#define MAX_N 100000

int par[MAX_N]; // 親の番号
int rank[MAX_N];

// n要素で初期化
void init(int n){
  for(int i=0; i<n; i++){
    par[i] = i;
    rank[i] = 0;
  }
}
// 木の根を求める
int root(int x){
  if(par[x] == x){ // 根
    return x;
  } else {
    return par[x] = root(par[x]); // 経路圧縮
  }
}

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

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

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

int main(){
  int N, M;
  cin >> N >> M;
  int P[N];
  for(int i=0; i<N; i++){
    cin >> P[i];
    P[i]--; // 0 <= P[i] <= N-1
  }

  init(N);
  for(int i=0; i<M; i++){
    int x, y;
    cin >> x >> y;
    unite(x-1, y-1);
  }

  int ret = 0;
  for(int i=0; i<N; i++)
    if(same(P[i], i))
      ret++;
  cout << ret << endl;

  return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:20:5: error: reference to 'rank' is ambiguous
   20 |     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:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:43:6: error: reference to 'rank' is ambiguous
   43 |   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:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:43:16: error: reference to 'rank' is ambiguous
   43 |   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:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:47:8: error: reference to 'rank' is ambiguous
   47 |     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:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:47:19: error: reference to 'rank' is ambiguous
   47 |     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:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:48:7: error: reference to 'rank' is ambiguous
   48 |       rank[x]++;
      |       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~