Language: C++

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

int rank[100005] , p[100005];

void makeSet(int x){
  for(int i=1;i<=x;i++){
    p[i]=i;
    rank[i]=0;
  }
}

int findSet(int x){
  if(x != p[x]){
    p[x] = findSet(p[x]);
  }
  return(p[x]);
}

void link(int x,int y){
  int X = findSet(x);
  int Y = findSet(y);
  if(rank[X] > rank[Y]){
    p[Y] = X;
  } else {
    p[X] = Y;
    if(rank[X] == rank[Y]){
      rank[Y]++;
    }
  }
}

signed main(){
  int n , m , x , y , p[100005] , ans=0;
  cin>>n>>m;

  makeSet(n);

  for(int i=1;i<=n;i++){
    cin>>p[i];
  }
  
  for(int i=0;i<m;i++){
    cin>>x>>y;
    link(x , y);
  }

  for(int i=1;i<=n;i++){
    if(findSet(i) == findSet(p[i])){
      ans++;
    }
  }

  cout<<ans<<endl;
}


Compiler output:
a.cc: In function 'void makeSet(int)':
a.cc:9:5: error: reference to 'rank' is ambiguous
    9 |     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 /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:4:5: note:                 'int rank [100005]'
    4 | int rank[100005] , p[100005];
      |     ^~~~
a.cc: In function 'void link(int, int)':
a.cc:23:6: error: reference to 'rank' is ambiguous
   23 |   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:4:5: note:                 'int rank [100005]'
    4 | int rank[100005] , p[100005];
      |     ^~~~
a.cc:23:16: error: reference to 'rank' is ambiguous
   23 |   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:4:5: note:                 'int rank [100005]'
    4 | int rank[100005] , p[100005];
      |     ^~~~
a.cc:27:8: error: reference to 'rank' is ambiguous
   27 |     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:4:5: note:                 'int rank [100005]'
    4 | int rank[100005] , p[100005];
      |     ^~~~
a.cc:27:19: error: reference to 'rank' is ambiguous
   27 |     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:4:5: note:                 'int rank [100005]'
    4 | int rank[100005] , p[100005];
      |     ^~~~
a.cc:28:7: error: reference to 'rank' is ambiguous
   28 |       rank[Y]++;
      |       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:4:5: note:                 'int rank [100005]'
    4 | int rank[100005] , p[100005];
      |     ^~~~