Language: C++

#include <stdio.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#define REP(i,n) for(int i = 0;i < (n);i++)
#define rep(i,m,n) for(int i = (m);i < (n);i++)
#define P pair<int,int>
#define pb push_back
#define mk make_pair
using namespace std;
 
//I imitated Ant Book version 1: P.84.
 
int n,q,com,x,y;
int rank[10000],parent[10000];
 
int find(int x){
    if(x == parent[x])return x;
    else{
        return parent[x] = find(parent[x]);
    }
}
 
bool isSame(int x,int y){
    return find(x) == find(y);
}
 
void unite(int x,int y){
    x = find(x);
    y = find(y);
 
    if(x == y)return;
 
    if(rank[x] < rank[y]){
        parent[x] = y;
    }else{
        parent[y] = x;
        if(rank[x] == rank[y])rank[x]++;
    }
}
 
int main(){
 
    scanf("%d %d",&n,&q);
    vector <int> p;
    REP(i,n){
      int c;
      cin >> c;
      p.pb(c);
    }
    for(int i = 0; i < n; i++){
        rank[i] = 0;
        parent[i] = i;
    }
 
    for(int i = 0; i < q; i++){
        scanf("%d %d",&x,&y);
 
	if(isSame(x,y))continue;
	unite(x,y);
    }
    int sum = 0;
    REP(i,n){
      if(isSame(p[i],i+1)){
	sum++;
      }
      
    }
    cout << sum << endl;
    return 0;
}


Compiler output:
a.cc: In function 'void unite(int, int)':
a.cc:36:8: error: reference to 'rank' is ambiguous
   36 |     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/vector:62,
                 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:17:5: note:                 'int rank [10000]'
   17 | int rank[10000],parent[10000];
      |     ^~~~
a.cc:36:18: error: reference to 'rank' is ambiguous
   36 |     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:17:5: note:                 'int rank [10000]'
   17 | int rank[10000],parent[10000];
      |     ^~~~
a.cc:40:12: error: reference to 'rank' is ambiguous
   40 |         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:17:5: note:                 'int rank [10000]'
   17 | int rank[10000],parent[10000];
      |     ^~~~
a.cc:40:23: error: reference to 'rank' is ambiguous
   40 |         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:17:5: note:                 'int rank [10000]'
   17 | int rank[10000],parent[10000];
      |     ^~~~
a.cc:40:31: error: reference to 'rank' is ambiguous
   40 |         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:17:5: note:                 'int rank [10000]'
   17 | int rank[10000],parent[10000];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:54:9: error: reference to 'rank' is ambiguous
   54 |         rank[i] = 0;
      |         ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:5: note:                 'int rank [10000]'
   17 | int rank[10000],parent[10000];
      |     ^~~~