Language: C++

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<complex>
#include<string>
#include<cstring>
using namespace std;
#define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)
#define INF 100000000
#define debug(x) cout<<#x<<": "<<x<<endl

#define all(x) x.begin(),x.end()
typedef pair<int,int> P;
typedef pair<int,P> PP;
int ans;
int n,m;
int a[100006];
int b[100006];
//
#define MAX_N 1000000
int par[MAX_N];
int rank[MAX_N];
void init(int n)
{
	rep(i,n)
	{
		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 p[100006];
vector<int> q[100006];
vector<int> r[100006];
int main()
{
	cin>>n;
	init(n);
	cin>>m;
	rep(i,n) cin>>p[i];
	rep(i,n)p[i]--;
	rep(i,m)
	{
		cin>>a[i]>>b[i];
		a[i]--;b[i]--;
		unite(a[i],b[i]);
	}
	rep(x,n)
	{
		par[x]=find(par[x]);
	}
	rep(x,n)
	{
		q[par[x]].push_back(x);
		r[par[x]].push_back(p[x]);
	}
	rep(i,n)
	{
		sort(all(q[i]));
		sort(all(r[i]));
		int s,t;
		s=0;t=0;
		while(1)
		{
			if(s==q[i].size()||t==r[i].size())break;
			if(q[i][s]==r[i][t])
			{
				ans++;
				s++;t++;
			}
			else if(q[i][s]<r[i][t])
			{
				s++;
			}
			else t++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:34:17: error: reference to 'rank' is ambiguous
   34 |                 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:28:5: note:                 'int rank [1000000]'
   28 | int rank[MAX_N];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:46:12: error: reference to 'rank' is ambiguous
   46 |         if(rank[x]<rank[y])par[x]=y;
      |            ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:28:5: note:                 'int rank [1000000]'
   28 | int rank[MAX_N];
      |     ^~~~
a.cc:46:20: error: reference to 'rank' is ambiguous
   46 |         if(rank[x]<rank[y])par[x]=y;
      |                    ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:28:5: note:                 'int rank [1000000]'
   28 | int rank[MAX_N];
      |     ^~~~
a.cc:50:20: error: reference to 'rank' is ambiguous
   50 |                 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:28:5: note:                 'int rank [1000000]'
   28 | int rank[MAX_N];
      |     ^~~~
a.cc:50:29: error: reference to 'rank' is ambiguous
   50 |                 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:28:5: note:                 'int rank [1000000]'
   28 | int rank[MAX_N];
      |     ^~~~
a.cc:50:37: error: reference to 'rank' is ambiguous
   50 |                 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:28:5: note:                 'int rank [1000000]'
   28 | int rank[MAX_N];
      |     ^~~~