Language: C++

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

//typedef pair<int,int> P;

int par[100000];
int rank[100000];

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;
	scanf("%d %d", &N, &M);

	int p[N];
	for(int i=0;i<N;i++){
		scanf("%d", &p[i]);
		// 1<=p[i]<=N -> 0<=p[i]<=N-1
		p[i]--;
	}

	// init union-find
	init(N);

	for(int i=0;i<M;i++){
		int x,y;
		scanf("%d %d", &x, &y);
		// 1<=x,y<=N -> 0<=x,y<=N-1
		x--;
		y--;
		unite(x,y);
	}

	int ans = 0;
	for(int i=0;i<N;i++){
		if(same(i,p[i])) ans++;
	}

	printf("%d\n", ans);

	return 0;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:21:17: error: reference to 'rank' is ambiguous
   21 |                 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:16:5: note:                 'int rank [100000]'
   16 | int rank[100000];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:38:12: error: reference to 'rank' is ambiguous
   38 |         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:16:5: note:                 'int rank [100000]'
   16 | int rank[100000];
      |     ^~~~
a.cc:38:20: error: reference to 'rank' is ambiguous
   38 |         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:16:5: note:                 'int rank [100000]'
   16 | int rank[100000];
      |     ^~~~
a.cc:42:20: error: reference to 'rank' is ambiguous
   42 |                 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:16:5: note:                 'int rank [100000]'
   16 | int rank[100000];
      |     ^~~~
a.cc:42:29: error: reference to 'rank' is ambiguous
   42 |                 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:16:5: note:                 'int rank [100000]'
   16 | int rank[100000];
      |     ^~~~
a.cc:42:38: error: reference to 'rank' is ambiguous
   42 |                 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:16:5: note:                 'int rank [100000]'
   16 | int rank[100000];
      |     ^~~~