Language: C++

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define all(x) (x).begin(),(x).end()
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
using Graph = vector<vector<int>>;
double nCk(int n, int k) {
double res=1.0;
for(int i=0; i<n; i++){
res*=0.5;}
for(int i=0; i<k; i++){
res*=(double)(n-i);
res/=(double)(k-i);
}
return res;}
struct edge{ll to, cost;};
typedef pair<ll,ll> P;
struct graph{
  ll V;
  vector<vector<edge> > G;
  vector<ll> d;

  graph(ll n){
    init(n);
  }

  void init(ll n){
    V = n;
    G.resize(V);
    d.resize(V);
    rep(i,V){
      d[i] = INF;
    }
  }

  void add_edge(ll s, ll t, ll cost){
    edge e;
    e.to = t, e.cost = cost;
    G[s].push_back(e);
  }

  void dijkstra(ll s){
    rep(i,V){
      d[i] = INF;
    }
    d[s] = 0;
    priority_queue<P,vector<P>, greater<P> > que;
    que.push(P(0,s));
    while(!que.empty()){
      P p = que.top(); que.pop();
      ll v = p.second;
      if(d[v]<p.first) continue;
      for(auto e : G[v]){
        if(d[e.to]>d[v]+e.cost){
          d[e.to] = d[v]+e.cost;
          que.push(P(d[e.to],e.to));
        }
      }
    }
  }
};
class UnionFind
{
public:
  int par[MAX_N];
  int depth[MAX_N];
  int nGroup[MAX_N];
 
  UnionFind(int n) {
    init(n);
  }
 
  void init(int n) {
    for(int i=0; i<n; i++) {
      par[i] = i;
      depth[i] = 0;
      nGroup[i] = 1;
    }
  }
 
  int root(int x) {
    if(par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }
 
  bool same(int x, int y) {
    return root(x) == root(y);
  }
 
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if(x == y) return;
 
    if(depth[x] < depth[y]) {
      par[x] = y;
      nGroup[y] += nGroup[x];
      nGroup[x] = 0;
    } else {
      par[y] = x;
      nGroup[x] += nGroup[y];
      nGroup[y] = 0;
      if(depth[x] == depth[y])
        depth[x]++;
    }
  }
};
int main(){
ll n,m; cin>>n>>m;
ll a[n]={};
rep(i,n){
cin>>a[i];
}
UnionFind.tree(n);
rep(i,m){
int x,y; cin>>x>>y;
tree.unite(x-1,y-1);}
ll ca = 0;
rep(i,n){
if(tree.same(a[i]-1,i) ca++;
   }
cout << ca << endl;}

Compiler output:
a.cc:80:11: error: 'MAX_N' was not declared in this scope
   80 |   int par[MAX_N];
      |           ^~~~~
a.cc:81:13: error: 'MAX_N' was not declared in this scope
   81 |   int depth[MAX_N];
      |             ^~~~~
a.cc:82:14: error: 'MAX_N' was not declared in this scope
   82 |   int nGroup[MAX_N];
      |              ^~~~~
a.cc: In member function 'void UnionFind::init(int)':
a.cc:90:7: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   90 |       par[i] = i;
      |       ^~~
      |       __pstl::execution::v1::par
In file included from /usr/include/c++/14/pstl/glue_algorithm_defs.h:15,
                 from /usr/include/c++/14/algorithm:86,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:91:7: error: 'depth' was not declared in this scope
   91 |       depth[i] = 0;
      |       ^~~~~
a.cc:92:7: error: 'nGroup' was not declared in this scope
   92 |       nGroup[i] = 1;
      |       ^~~~~~
a.cc: In member function 'int UnionFind::root(int)':
a.cc:97:8: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   97 |     if(par[x] == x) {
      |        ^~~
      |        __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In member function 'void UnionFind::unite(int, int)':
a.cc:113:8: error: 'depth' was not declared in this scope
  113 |     if(depth[x] < depth[y]) {
      |        ^~~~~
a.cc:114:7: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
  114 |       par[x] = y;
      |       ^~~
      |       __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:115:7: error: 'nGroup' was not declared in this scope
  115 |       nGroup[y] += nGroup[x];
      |       ^~~~~~
a.cc:118:7: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
  118 |       par[y] = x;
      |       ^~~
      |       __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:119:7: error: 'nGroup' was not declared in this scope
  119 |       nGroup[x] += nGroup[y];
      |       ^~~~~~
a.cc: In function 'int main()':
a.cc:132:10: error: expected unqualified-id before '.' token
  132 | UnionFind.tree(n);
      |          ^
a.cc:135:1: error: 'tree' was not declared in this scope; did you mean 'free'?
  135 | tree.unite(x-1,y-1);}
      | ^~~~
      | free
a.cc:138:4: error: 'tree' was not declared in this scope; did you mean 'free'?
  138 | if(tree.same(a[i]-1,i) ca++;
      |    ^~~~
      |    free
a.cc:139:4: error: expected primary-expression before '}' token
  139 |    }
      |    ^
a.cc:138:29: error: expected ')' before '}' token
  138 | if(tree.same(a[i]-1,i) ca++;
      |   ~                         ^
      |                             )
  139 |    }
      |    ~                         
a.cc:139:4: error: expected primary-expression before '}' token
  139 |    }
      |    ^