Language: C++

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<numeric>
#include<queue>
#include<stack>
#define repp(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define repm(i,a,b) for(int i=(int)a;i>(int)b;--i)
using ll=long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

//Union-Findtree
struct UF {
	int par[MAX_N];//親
	int Rank[MAX_N];//木の深さ

	//初期化
	void init(int n) {
		for (int i = 0; i < n; i++) {
			par[i] = i;
			Rank[i] = 0;
		}
	}

	//木の根を求める
	int find(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = find(par[x]);
		}
	}

	//xとyの属する集合を併合
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)return;//すでに同じ集合に属している

		if (Rank[x] < Rank[y]) {//高さが小さい方を下に
			par[x] = y;
		}
		else {
			par[y] = x;
			if (Rank[x] == Rank[y])Rank[x]++;//同じだとrankが変わらないままになるので増やす（それ以外は大きい方のrankが採用される）
		}
	}

	//xとyが同じ集合に属するかを判定
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

int main() {
	int n, m;
	int ans = 0;
	cin >> n >> m;
	vector<int>p(n);
	repp(i, 0, n)cin >> p[i];
	vector<int>x(m), y(m);
	repp(i, 0, m)cin >> x[i] >> y[i];
	UF tree;
	tree.init(n);
	repp(i,0,m)tree.unite(x[i], y[i]);
	repp(i, 0, n) {
		if (tree.same(i + 1, p[i]))++ans;
	}
	cout << ans << endl;
	return 0;
}

Compiler output:
a.cc:21:17: error: 'MAX_N' was not declared in this scope
   21 |         int par[MAX_N];//親
      |                 ^~~~~
a.cc:22:18: error: 'MAX_N' was not declared in this scope
   22 |         int Rank[MAX_N];//木の深さ
      |                  ^~~~~
a.cc: In member function 'void UF::init(int)':
a.cc:27:25: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   27 |                         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 a.cc:4:
/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:28:25: error: 'Rank' was not declared in this scope
   28 |                         Rank[i] = 0;
      |                         ^~~~
a.cc: In member function 'int UF::find(int)':
a.cc:34:21: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   34 |                 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 UF::unite(int, int)':
a.cc:48:21: error: 'Rank' was not declared in this scope
   48 |                 if (Rank[x] < Rank[y]) {//高さが小さい方を下に
      |                     ^~~~
a.cc:49:25: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   49 |                         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:52:25: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   52 |                         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{};
      |                                             ^~~