Language: C++

#include<stdio.h>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>     
#include <numeric>
#include <utility>
#include <queue>
#include <set>
#include <sstream>
#include <iomanip>
#include <stack>
#include <string>
#include <limits>
#include <vector>
#include<functional>
using namespace std;
#define ll long long
#define ld long double
#define rep(a,t) for(int a=0;a<t;++a) 
#define forever while(true)
#define Sort(a) sort(a.begin(),a.end())
#define Reverse(a) reverse(a.begin(),a.end())
#define pb push_back
#define print_double(val,a) cout << fixed << setprecision(a) << val << endl;
ll mod = 1e9 + 7;
int par[100010];
int rank[100010];
vector<pair<int, int>>G[100005];
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; }
	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()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<int>p(n);
	vector<int>x(m);
	vector<int>y(m);
	for (int i = 0; i < n; i++) {
		cin >> p[i]; p[i]--;
	}
	for (int i = 0; i < m; i++) {
		cin >> x[i] >> y[i];
	}
	init(n);
	rep(i, m) { unite(x[i] - 1, y[i] - 1); }
	rep(i, n) {
		G[find(i)].push_back(pair<int,int>(p[i], i));
	}
	int ans = 0;
	rep(i, n) {
		vector<int>v1, v2;
		rep(j, G[i].size()) {
			v1.push_back(G[i][j].first);
			v2.push_back(G[i][j].second);
		}
		Sort(v1);
		Sort(v2);
		rep(j, v1.size()) {
			if (binary_search(v2.begin(), v2.end(), v1[j]))ans++;
		}
	}
	cout << ans << endl;

	return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:43:17: error: reference to 'rank' is ambiguous
   43 |                 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/algorithm:60,
                 from a.cc:2:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:38:5: note:                 'int rank [100010]'
   38 | int rank[100010];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:54:13: error: reference to 'rank' is ambiguous
   54 |         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:38:5: note:                 'int rank [100010]'
   38 | int rank[100010];
      |     ^~~~
a.cc:54:23: error: reference to 'rank' is ambiguous
   54 |         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:38:5: note:                 'int rank [100010]'
   38 | int rank[100010];
      |     ^~~~
a.cc:56:33: error: reference to 'rank' is ambiguous
   56 |                 par[y] = x; 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:38:5: note:                 'int rank [100010]'
   38 | int rank[100010];
      |     ^~~~
a.cc:56:44: error: reference to 'rank' is ambiguous
   56 |                 par[y] = x; 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:38:5: note:                 'int rank [100010]'
   38 | int rank[100010];
      |     ^~~~
a.cc:56:55: error: reference to 'rank' is ambiguous
   56 |                 par[y] = x; 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:38:5: note:                 'int rank [100010]'
   38 | int rank[100010];
      |     ^~~~