Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

template<class T> class Dinic {
	struct edge {
		int to;
		T cap;
		int rev;
	};
	vector<edge> graph[200010];
	int level[200010];
	int iter[200010];
public:
	void add_edge(int from, int to, T cap) {
		graph[from].push_back({ to,cap,(int)graph[to].size() });
		graph[to].push_back({ from,0,(int)graph[from].size() - 1 });
	}
	void bfs(int s) {
		memset(level, -1, sizeof(level));
		queue<int> que;
		level[s] = 0;
		que.push(s);
		while (!que.empty()) {
			int v = que.front(); que.pop();
			for (int i = 0; i < graph[v].size(); i++) {
				edge &e = graph[v][i];
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					que.push(e.to);
				}
			}
		}
	}
	T dfs(int v, int t, T f) {
		if (v == t) return f;
		for (int &i = iter[v]; i < graph[v].size(); i++) {
			edge &e = graph[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				T d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					graph[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
	T max_flow(int s, int t) {
		T flow = 0;
		while (true) {
			bfs(s);
			if (level[t] < 0) return flow;
			memset(iter, 0, sizeof(iter));
			T f = 0;
			while ((f = dfs(s, t, inf)) > 0) flow += f;
		}
	}
	void debug() {
		for (int i = 0; i < graph.size(); i++) {
			for (auto &e : graph[i]) {
				if (e.cap) {
					cerr << i << ' ' << e.to << ' ' << e.cap << '\n';
				}
			}
		}
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	int n;
	cin >> n;
	vector<int> p(n), q(n);
	rep(i, n) cin >> p[i];
	rep(i, n) cin >> q[i];
	Dinic<ll> d(2 * n + 2);
	int ans = n;
	rep(i, n) {
		if (p[i] == i && q[i] == i) { ans--; continue; }
		else if (p[i] == i) d.add_edge(2 * n, n + i, 1);
		else if (q[i] == i) d.add_edge(i, 2 * n + 1, 1);
		if (p[i] != i) d.add_edge(p[i], i, inf);
		if (q[i] != i) d.add_edge(n + i, n + q[i], inf);
		d.add_edge(i, n + i, 1);
		if (p[i] != i && p[i] == q[i]) d.add_edge(n + i, i, 1);
	}
	cout << ans - d.max_flow(2 * n, 2 * n + 1) << '\n';
}

Compiler output:
a.cc: In function 'int main()':
a.cc:118:30: error: no matching function for call to 'Dinic<long long int>::Dinic(int)'
  118 |         Dinic<ll> d(2 * n + 2);
      |                              ^
a.cc:42:25: note: candidate: 'Dinic<long long int>::Dinic()'
   42 | template<class T> class Dinic {
      |                         ^~~~~
a.cc:42:25: note:   candidate expects 0 arguments, 1 provided
a.cc:42:25: note: candidate: 'Dinic<long long int>::Dinic(const Dinic<long long int>&)'
a.cc:42:25: note:   no known conversion for argument 1 from 'int' to 'const Dinic<long long int>&'
a.cc:42:25: note: candidate: 'Dinic<long long int>::Dinic(Dinic<long long int>&&)'
a.cc:42:25: note:   no known conversion for argument 1 from 'int' to 'Dinic<long long int>&&'