Language: C++

#include<iostream>
#include<vector>

using namespace std;

int par[100010]={};
int rank[100010]={};

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

int root(int x){
	return par[x]==x?x: par[x]=root(par[x]);
}

bool same(int x,int y){
	return root(x)==root(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;
	vector<int> p(n);
	for(int i=0;i<n;i++)cin>>p[i];
	init(n);
	for(int i=0;i<m;i++){
		int a,b;cin>>a>>b;
		a--,b--;
		unite(p[a],p[b]);
	}
	int ret=0;
	for(int i=0;i<n;i++){
		if(same(i+1,p[i]))ret++;
	}
	cout<<ret<<endl;
	return 0;
}



Compiler output:
a.cc: In function 'void init(int)':
a.cc:12:17: error: reference to 'rank' is ambiguous
   12 |                 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:7:5: note:                 'int rank [100010]'
    7 | int rank[100010]={};
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:28:12: error: reference to 'rank' is ambiguous
   28 |         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:7:5: note:                 'int rank [100010]'
    7 | int rank[100010]={};
      |     ^~~~
a.cc:28:20: error: reference to 'rank' is ambiguous
   28 |         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:7:5: note:                 'int rank [100010]'
    7 | int rank[100010]={};
      |     ^~~~
a.cc:32:20: error: reference to 'rank' is ambiguous
   32 |                 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:7:5: note:                 'int rank [100010]'
    7 | int rank[100010]={};
      |     ^~~~
a.cc:32:29: error: reference to 'rank' is ambiguous
   32 |                 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:7:5: note:                 'int rank [100010]'
    7 | int rank[100010]={};
      |     ^~~~
a.cc:32:37: error: reference to 'rank' is ambiguous
   32 |                 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:7:5: note:                 'int rank [100010]'
    7 | int rank[100010]={};
      |     ^~~~