Language: C++

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=(0);i<(n);i++)
#define loop(k,a) for(int k=a;~k;k=link[k])
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef double ld;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=500005,M=2000005,inf=1e9;
struct edge { int u, v, f, c; };
struct ISAP {
	int n, m, s, t, head[N], link[M];
	int d[N], p[N], cur[N], num[N];
	edge e[M];
	#define loop(k,a) for (int k=(a); ~k; k=link[k])
	void init(int _n) {
		n = _n; m = 0;
		memset(head, -1, sizeof head);
		memset(cur, -1, sizeof cur);
	}
	void add(int u, int v, int w) {
		e[m].u = u; e[m].v = v; e[m].f = 0; e[m].c = w;
		link[m] = head[u]; cur[u] = head[u] = m++;
		e[m].u = v; e[m].v = u; e[m].f = 0; e[m].c = (w == inf ? inf : 0);
		link[m] = head[v]; cur[v] = head[v] = m++;
	}
	int augment(int lim) {
		int mn = lim, u = t;
		while (u != s) {
			mn = min(mn, e[p[u]].c - e[p[u]].f);
			u = e[p[u]].u;
		}
		u = t;
		while (u != s) {
			e[p[u]].f += mn; e[p[u]^1].f -= mn;
			u = e[p[u]].u;
		}
		return mn;
	}
	int q[N];
	void bfs(){
		memset(d,-1,sizeof d);
		memset(num,0,sizeof num);
		int l=0,r=0;q[r++]=t,d[t]=0;
		while(l<r){
			int x=q[l++];
			num[d[x]]++;
			loop(k,head[x])
				if(d[e[k].v]==-1&&e[k^1].f<e[k^1].c){
					d[e[k].v]=d[x]+1;
					q[r++]=e[k].v;
				}
		}
		rep(i,1,n)if(d[i]==-1)
			num[d[i]=n]++;
	}
	int flow(int _s, int _t, int lim = inf) {
		int u = _s, flag, res = 0;
		s = _s; t = _t; bfs();
		rep (i, 1, n) cur[i] = head[i];
		while (d[s] < n) {
			if (u == t) { 
				res += augment(lim - res); 
				if (res == lim) return res;
				u = s;
			}
			flag = 0;
			loop (k, cur[u])
				if (e[k].f < e[k].c && d[u] == d[e[k].v] + 1) {
					p[e[k].v] = cur[u] = k;
					u = e[k].v; flag = 1;
					break;
				}
			if (flag) continue;
			int mn = n;
			loop (k, head[u])
				if (e[k].f < e[k].c)
					mn = min(mn, d[e[k].v] + 1);
			if (--num[d[u]] == 0) return res;
			num[d[u] = mn]++; cur[u] = head[u];
			if (u != s) u = e[p[u]].u;
		}
		return res;
	}
} G;
int x[N],y[N],n,a,b,s,t,ans;

int main(){
	read(n);
	rep(i,1,n)read(x[i]);
	rep(i,1,n)read(y[i]);
	s=2*n+1,t=s+1;
	G.init(t);
	rep(i,1,n){
		a=x[i]+1,b=y[i]+1;
		if(a!=i)		G.add(i,a,inf);
		if(b!=i)		G.add(n+i,n+b,inf);
		if(a==i&&b==i)	continue;
		ans++;
		if(a==i)		G.add(s,n+i,1);
		else if(b==i)	G.add(i,t,1);
		else if(a==b)	G.add(i,n+i,1),G.add(n+i,i,1);
		else			G.add(i,n+i,1);
	}
	printf("%d\n",ans-G.Maxflow(s,t));
	return 0;
}

Compiler output:
a.cc:29:17: warning: "loop" redefined
   29 |         #define loop(k,a) for (int k=(a); ~k; k=link[k])
      |                 ^~~~
a.cc:5:9: note: this is the location of the previous definition
    5 | #define loop(k,a) for(int k=a;~k;k=link[k])
      |         ^~~~
a.cc: In function 'int main()':
a.cc:119:29: error: 'struct ISAP' has no member named 'Maxflow'; did you mean 'flow'?
  119 |         printf("%d\n",ans-G.Maxflow(s,t));
      |                             ^~~~~~~
      |                             flow