Language: C++

#include <vector>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#define REP(i,n)for (int i=0;i<(n);i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define ll long long
using namespace std;
typedef pair<int,int> P;

int par[100100];
int rank[100100];

void init(int n){
	for(int i=1;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 to[100100];

int main(){
	int n,m;
	int p[100100];
	int ans=0;
	cin>>n>>m;
	init(n);
	REP(i,n){
		cin>>p[i+1];
		to[p[i+1]]=i+1;
	}
	REP(i,m){
		int a,b;
		cin>>a>>b;
		unite(a,b);
	}
	for(int i=n;i>=1;i--){
		if(same(i,to[i])){
			ans++;
		}
	}
	cout<<ans<<endl;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:38:17: error: reference to 'rank' is ambiguous
   38 |                 rank[i]=0;
      |                 ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/vector:62,
                 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:33:5: note:                 'int rank [100100]'
   33 | int rank[100100];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:54:12: error: reference to 'rank' is ambiguous
   54 |         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:33:5: note:                 'int rank [100100]'
   33 | int rank[100100];
      |     ^~~~
a.cc:54:22: error: reference to 'rank' is ambiguous
   54 |         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:33:5: note:                 'int rank [100100]'
   33 | int rank[100100];
      |     ^~~~
a.cc:58:20: error: reference to 'rank' is ambiguous
   58 |                 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:33:5: note:                 'int rank [100100]'
   33 | int rank[100100];
      |     ^~~~
a.cc:58:29: error: reference to 'rank' is ambiguous
   58 |                 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:33:5: note:                 'int rank [100100]'
   33 | int rank[100100];
      |     ^~~~
a.cc:58:37: error: reference to 'rank' is ambiguous
   58 |                 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:33:5: note:                 'int rank [100100]'
   33 | int rank[100100];
      |     ^~~~