Language: C++

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)

vector <int> a,b;
vector <int> x[100005];
int p[100005];
int visit[100005];
void matomeru(int s){
    FOR(i,0,x[s].size()){
        if(visit[x[s][i]] == 1){
            continue;
        }
        a.push_back(x[s][i]);
        visit[x[s][i]] =1;
        matomeru(x[s][i]);
    }
};

void bset(){
    sort(a.begin(),a.end());
    b.push_back(p[a[0]]);
    FOR(i,1,a.size()){
        if(a[i]== a[i-1])
            continue;
        else {
            b.push_back(p[a[i]]);
        }
    }
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int n,m,tmp;
    pair<int,int> tmp2;
    cin >> n >> m;
    FOR(i,0,n){
        cin >> p[i+1];
        visit[i+1]=0;
        visited[i+1]=0;
    }
    FOR(i,0,m){
        cin >> tmp2.first >> tmp2.second;
        x[min(tmp2.first,tmp2.second)].push_back(max(tmp2.first,tmp2.second));
        x[max(tmp2.first,tmp2.second)].push_back(min(tmp2.first,tmp2.second));
    }
    int ans =0;
    FOR(i,1,n+1){
        if(visit[i] == 1)
            continue;
        a.erase(a.begin(),a.end());
        b.erase(b.begin(),b.end());
        a.push_back(i);
        matomeru(i);
        bset();
        sort(b.begin(),b.end());

        int j=0,k=0;
        while (1){
            if(a.size()== j || b.size() == k)
                break;
            if(a[j] == b[k]){
                ans++;
                j++;
                k++;
            }else if (a[j] < b[k]){
                j++;
            }else{
                k++;
            }
        }
    }
    cout << ans;
    return 0;
}

Compiler output:
a.cc: In function 'void matomeru(int)':
a.cc:12:12: error: reference to 'visit' is ambiguous
   12 |         if(visit[x[s][i]] == 1){
      |            ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:9:5: note:                 'int visit [100005]'
    9 | int visit[100005];
      |     ^~~~~
a.cc:16:9: error: reference to 'visit' is ambiguous
   16 |         visit[x[s][i]] =1;
      |         ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:9:5: note:                 'int visit [100005]'
    9 | int visit[100005];
      |     ^~~~~
a.cc: In function 'int main()':
a.cc:41:9: error: reference to 'visit' is ambiguous
   41 |         visit[i+1]=0;
      |         ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:9:5: note:                 'int visit [100005]'
    9 | int visit[100005];
      |     ^~~~~
a.cc:42:9: error: 'visited' was not declared in this scope; did you mean 'visit'?
   42 |         visited[i+1]=0;
      |         ^~~~~~~
      |         visit
a.cc:51:12: error: reference to 'visit' is ambiguous
   51 |         if(visit[i] == 1)
      |            ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:9:5: note:                 'int visit [100005]'
    9 | int visit[100005];
      |     ^~~~~