Language: C++

#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
vector<int>graph[MAX];
int arr[MAX];
bool visit[MAX];
vector<int>v;
map<int,int>mp;
void dfs(int src)
{
    visit[src]=1;
    mp[src]++;
    mp[arr[src]]++;
    v.push_back(src);
    for(int i=0; i<graph[src].size(); i++)
    {
        if(!visit[graph[src][i]])
            dfs(graph[src][i]);
    }
}
void fix()
{
    for(auto x:v)
    {
        if(mp[x]==2)
            arr[x]=x;

    }
    mp.clear();
    v.clear();
}
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int n,m;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        cin>>arr[i];
    while(m--)
    {
        int u,v;
        cin>>u>>v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    for(int i=1; i<=n; i++)
    {
        if(graph[i].size() && !visit[i])
        {
            dfs(i);
            fix();
        }
    }
    int cnt=0;
    for(int i=1; i<=n; i++)
    {
        if(arr[i]==i)
            cnt++;
        //cout<<arr[i]<<" "<<i<<" "<<visit[i]<<endl;
    }
    cout<<cnt<<endl;


    return 0;
}


Compiler output:
a.cc: In function 'void dfs(int)':
a.cc:21:5: error: reference to 'visit' is ambiguous
   21 |     visit[src]=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:16:6: note:                 'bool visit [1000000]'
   16 | bool visit[MAX];
      |      ^~~~~
a.cc:27:13: error: reference to 'visit' is ambiguous
   27 |         if(!visit[graph[src][i]])
      |             ^~~~~
/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:16:6: note:                 'bool visit [1000000]'
   16 | bool visit[MAX];
      |      ^~~~~
a.cc: In function 'int main()':
a.cc:59:32: error: reference to 'visit' is ambiguous
   59 |         if(graph[i].size() && !visit[i])
      |                                ^~~~~
/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:16:6: note:                 'bool visit [1000000]'
   16 | bool visit[MAX];
      |      ^~~~~