Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod (ll)(1e9+7)

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}


int n, m;
int par[100001], rank[100001];
int p[100001];

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]++;
  }
}

signed main() {
  cin >> n >> m;
  rep(i, 0, n) cin >> p[i], par[i] = i;
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    unite(x, y);
  }
  int ans = 0;
  rep(i, 0, n) {
    if(find(p[i]) == find(i + 1)) ans++;
  }
  cout << ans << endl;
}


Compiler output:
a.cc: In function 'void unite(int, int)':
a.cc:37:6: error: reference to 'rank' is ambiguous
   37 |   if(rank[x] < rank[y]) {
      |      ^~~~
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 /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 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:21:18: note:                 'int rank [100001]'
   21 | int par[100001], rank[100001];
      |                  ^~~~
a.cc:37:16: error: reference to 'rank' is ambiguous
   37 |   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:21:18: note:                 'int rank [100001]'
   21 | int par[100001], rank[100001];
      |                  ^~~~
a.cc:41:8: error: reference to 'rank' is ambiguous
   41 |     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:21:18: note:                 'int rank [100001]'
   21 | int par[100001], rank[100001];
      |                  ^~~~
a.cc:41:19: error: reference to 'rank' is ambiguous
   41 |     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:21:18: note:                 'int rank [100001]'
   21 | int par[100001], rank[100001];
      |                  ^~~~
a.cc:41:28: error: reference to 'rank' is ambiguous
   41 |     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:21:18: note:                 'int rank [100001]'
   21 | int par[100001], rank[100001];
      |                  ^~~~