Language: C++

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100000 + 10;
//const int maxn = 100 + 10;
const int N = maxn << 1;
const int maxE = maxn << 3;
const int INF = 0x3f3f3f3f;

int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
int S, T, h[N], ecnt;
struct enode{
	int v, n, w, op;
	enode() {}
	enode(int _v, int _n, int _w, int _op):v(_v), n(_n), w(_w), op(_op) {}
}e[maxE];

inline void addedge(int u, int v, int w) {
//	cout << u << ' ' << v << ' ' << w << endl;
	ecnt ++; e[ecnt] = enode(v,h[u],w,ecnt + 1); h[u] = ecnt;
	ecnt ++; e[ecnt] = enode(u,h[v],0,ecnt - 1); h[v] = ecnt;
}

inline void init(int *a, int *pos) {
	memset(vis,0,sizeof(vis)); int cnt = 0;
	for(int i = 1;i <= n;i ++) {
		if(vis[i]) continue; 
		cnt ++;
		for(int p = i;!vis[p];p = a[p]) {
      pos[p] = cnt;
			vis[p] = 1;
		}
	}
}

int level[N], q[N << 1], cur[N];
inline bool bfs() {
  memset(level,0,sizeof(level));
	for(int i = 1;i <= T;i ++) cur[i] = h[i];
	int head = 1, tail = 0;
	q[++ tail] = S; level[S] = 1;
	while(head <= tail) {
		int u = q[head]; head ++;
		for(int i = h[u];~ i;i = e[i].n) {
			int v = e[i].v;
			if(!level[v] && e[i].w > 0) {
				level[v] = level[u] + 1;
				q[++ tail] = v;
				if(v == T) return 1;
			}
		}
	}
	return 0;
}

int dfs(int u, int c) {
	if(u == T || c == 0) return c;
	int tmp = 0;
	for(int &i = cur[u];~ i;i = e[i].n) {
		int v = e[i].v;
		if(level[v] == level[u] + 1) {
			int x = dfs(v,min(c,e[i].w));
			if(x) {
				tmp += x; c -= x;
				e[i].w -= x; e[e[i].op].w += x;
				if(!c) break;
			}
		}
	}
	if(!tmp) level[u] = -1;
	return tmp;
}

inline int max_flow() {
  int now = 0;
	while(bfs())
		now += dfs(S,INF);
	return now;
}

int main() {
//	freopen("data.in","r",stdin);
  scanf("%d", &n); ecnt = 0;
	memset(h,-1,sizeof(h));
	for(int i = 1;i <= n;i ++) scanf("%d", &a[i]), a[i] ++;
	for(int i = 1;i <= n;i ++) scanf("%d", &b[i]), b[i] ++;
  init(a,pos1);
  init(b,pos2);
	for(int i = 1;i <= n;i ++) pos2[i] += n;
	int Ans = 0; S = n + n + 1, T = S + 1;
	for(int i = 1;i <= n;i ++) if(a[i] != b[i]) Ans ++;
	for(int i = 1;i <= n;i ++) {
//    cout << "work " << i << endl;
		if(a[i] == b[i]) {
			if(a[i] == i) continue;
			else {
				Ans ++;
				addedge(pos1[i],pos2[i],1);
				addedge(pos2[i],pos1[i],1);
			}
		}
		else {
			if(a[i] == i) addedge(S,pos2[i],1);
			else if(b[i] == i) addedge(pos1[i],T,1);
			else addedge(pos1[i],pos2[i],1);
		}
	}
	int now = max_flow();
	Ans -= now;
	printf("%d\n", Ans);
	return 0;
}
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100000 + 10;
//const int maxn = 100 + 10;
const int N = maxn << 1;
const int maxE = maxn << 3;
const int INF = 0x3f3f3f3f;

int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
int S, T, h[N], ecnt;
struct enode{
	int v, n, w, op;
	enode() {}
	enode(int _v, int _n, int _w, int _op):v(_v), n(_n), w(_w), op(_op) {}
}e[maxE];

inline void addedge(int u, int v, int w) {
//	cout << u << ' ' << v << ' ' << w << endl;
	ecnt ++; e[ecnt] = enode(v,h[u],w,ecnt + 1); h[u] = ecnt;
	ecnt ++; e[ecnt] = enode(u,h[v],0,ecnt - 1); h[v] = ecnt;
}

inline void init(int *a, int *pos) {
	memset(vis,0,sizeof(vis)); int cnt = 0;
	for(int i = 1;i <= n;i ++) {
		if(vis[i]) continue; 
		cnt ++;
		for(int p = i;!vis[p];p = a[p]) {
      pos[p] = cnt;
			vis[p] = 1;
		}
	}
}

int level[N], q[N << 1], cur[N];
inline bool bfs() {
  memset(level,0,sizeof(level));
	for(int i = 1;i <= T;i ++) cur[i] = h[i];
	int head = 1, tail = 0;
	q[++ tail] = S; level[S] = 1;
	while(head <= tail) {
		int u = q[head]; head ++;
		for(int i = h[u];~ i;i = e[i].n) {
			int v = e[i].v;
			if(!level[v] && e[i].w > 0) {
				level[v] = level[u] + 1;
				q[++ tail] = v;
				if(v == T) return 1;
			}
		}
	}
	return 0;
}

int dfs(int u, int c) {
	if(u == T || c == 0) return c;
	int tmp = 0;
	for(int &i = cur[u];~ i;i = e[i].n) {
		int v = e[i].v;
		if(level[v] == level[u] + 1) {
			int x = dfs(v,min(c,e[i].w));
			if(x) {
				tmp += x; c -= x;
				e[i].w -= x; e[e[i].op].w += x;
				if(!c) break;
			}
		}
	}
	if(!tmp) level[u] = -1;
	return tmp;
}

inline int max_flow() {
  int now = 0;
	while(bfs())
		now += dfs(S,INF);
	return now;
}

int main() {
//	freopen("data.in","r",stdin);
  scanf("%d", &n); ecnt = 0;
	memset(h,-1,sizeof(h));
	for(int i = 1;i <= n;i ++) scanf("%d", &a[i]), a[i] ++;
	for(int i = 1;i <= n;i ++) scanf("%d", &b[i]), b[i] ++;
  init(a,pos1);
  init(b,pos2);
	for(int i = 1;i <= n;i ++) pos2[i] += n;
	int Ans = 0; S = n + n + 1, T = S + 1;
	for(int i = 1;i <= n;i ++) if(a[i] != b[i]) Ans ++;
	for(int i = 1;i <= n;i ++) {
//    cout << "work " << i << endl;
		if(a[i] == b[i]) {
			if(a[i] == i) continue;
			else {
				Ans ++;
				addedge(pos1[i],pos2[i],1);
				addedge(pos2[i],pos1[i],1);
			}
		}
		else {
			if(a[i] == i) addedge(S,pos2[i],1);
			else if(b[i] == i) addedge(pos1[i],T,1);
			else addedge(pos1[i],pos2[i],1);
		}
	}
	int now = max_flow();
	Ans -= now;
	printf("%d\n", Ans);
	return 0;
}


Compiler output:
a.cc:120:11: error: redefinition of 'const int maxn'
  120 | const int maxn = 100000 + 10;
      |           ^~~~
a.cc:6:11: note: 'const int maxn' previously defined here
    6 | const int maxn = 100000 + 10;
      |           ^~~~
a.cc:122:11: error: redefinition of 'const int N'
  122 | const int N = maxn << 1;
      |           ^
a.cc:8:11: note: 'const int N' previously defined here
    8 | const int N = maxn << 1;
      |           ^
a.cc:123:11: error: redefinition of 'const int maxE'
  123 | const int maxE = maxn << 3;
      |           ^~~~
a.cc:9:11: note: 'const int maxE' previously defined here
    9 | const int maxE = maxn << 3;
      |           ^~~~
a.cc:124:11: error: redefinition of 'const int INF'
  124 | const int INF = 0x3f3f3f3f;
      |           ^~~
a.cc:10:11: note: 'const int INF' previously defined here
   10 | const int INF = 0x3f3f3f3f;
      |           ^~~
a.cc:126:5: error: redefinition of 'int n'
  126 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |     ^
a.cc:12:5: note: 'int n' previously declared here
   12 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |     ^
a.cc:126:8: error: redefinition of 'int a [100010]'
  126 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |        ^
a.cc:12:8: note: 'int a [100010]' previously declared here
   12 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |        ^
a.cc:126:17: error: redefinition of 'int b [100010]'
  126 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |                 ^
a.cc:12:17: note: 'int b [100010]' previously declared here
   12 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |                 ^
a.cc:126:26: error: redefinition of 'int pos1 [100010]'
  126 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |                          ^~~~
a.cc:12:26: note: 'int pos1 [100010]' previously declared here
   12 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |                          ^~~~
a.cc:126:38: error: redefinition of 'int pos2 [100010]'
  126 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |                                      ^~~~
a.cc:12:38: note: 'int pos2 [100010]' previously declared here
   12 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |                                      ^~~~
a.cc:126:50: error: redefinition of 'int vis [100010]'
  126 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |                                                  ^~~
a.cc:12:50: note: 'int vis [100010]' previously declared here
   12 | int n, a[maxn], b[maxn], pos1[maxn], pos2[maxn], vis[maxn];
      |                                                  ^~~
a.cc:127:5: error: redefinition of 'int S'
  127 | int S, T, h[N], ecnt;
      |     ^
a.cc:13:5: note: 'int S' previously declared here
   13 | int S, T, h[N], ecnt;
      |     ^
a.cc:127:8: error: redefinition of 'int T'
  127 | int S, T, h[N], ecnt;
      |        ^
a.cc:13:8: note: 'int T' previously declared here
   13 | int S, T, h[N], ecnt;
      |        ^
a.cc:127:11: error: redefinition of 'int h [200020]'
  127 | int S, T, h[N], ecnt;
      |           ^
a.cc:13:11: note: 'int h [200020]' previously declared here
   13 | int S, T, h[N], ecnt;
      |           ^
a.cc:127:17: error: redefinition of 'int ecnt'
  127 | int S, T, h[N], ecnt;
      |                 ^~~~
a.cc:13:17: note: 'int ecnt' previously declared here
   13 | int S, T, h[N], ecnt;
      |                 ^~~~
a.cc:128:8: error: redefinition of 'struct enode'
  128 | struct enode{
      |        ^~~~~
a.cc:14:8: note: previous definition of 'struct enode'
   14 | struct enode{
      |        ^~~~~
a.cc:132:2: error: conflicting declaration 'int e [800080]'
  132 | }e[maxE];
      |  ^
a.cc:18:2: note: previous declaration as 'enode e [800080]'
   18 | }e[maxE];
      |  ^
a.cc:134:13: error: redefinition of 'void addedge(int, int, int)'
  134 | inline void addedge(int u, int v, int w) {
      |             ^~~~~~~
a.cc:20:13: note: 'void addedge(int, int, int)' previously defined here
   20 | inline void addedge(int u, int v, int w) {
      |             ^~~~~~~
a.cc:140:13: error: redefinition of 'void init(int*, int*)'
  140 | inline void init(int *a, int *pos) {
      |             ^~~~
a.cc:26:13: note: 'void init(int*, int*)' previously defined here
   26 | inline void init(int *a, int *pos) {
      |             ^~~~
a.cc:152:5: error: redefinition of 'int level [200020]'
  152 | int level[N], q[N << 1], cur[N];
      |     ^~~~~
a.cc:38:5: note: 'int level [200020]' previously declared here
   38 | int level[N], q[N << 1], cur[N];
      |     ^~~~~
a.cc:152:15: error: redefinition of 'int q [400040]'
  152 | int level[N], q[N << 1], cur[N];
      |               ^
a.cc:38:15: note: 'int q [400040]' previously declared here
   38 | int level[N], q[N << 1], cur[N];
      |               ^
a.cc:152:26: error: redefinition of 'int cur [200020]'
  152 | int level[N], q[N << 1], cur[N];
      |                          ^~~
a.cc:38:26: note: 'int cur [200020]' previously declared here
   38 | int level[N], q[N << 1], cur[N];
      |                          ^~~
a.cc:153:13: error: redefinition of 'bool bfs()'
  153 | inline bool bfs() {
      |             ^~~
a.cc:39:13: note: 'bool bfs()' previously defined here
   39 | inline bool bfs() {
      |             ^~~
a.cc:172:5: error: redefinition of 'int dfs(int, int)'
  172 | int dfs(int u, int c) {
      |     ^~~
a.cc:58:5: note: 'int dfs(int, int)' previously defined here
   58 | int dfs(int u, int c) {
      |     ^~~
a.cc:190:12: error: redefinition of 'int max_flow()'
  190 | inline int max_flow() {
      |            ^~~~~~~~
a.cc:76:12: note: 'int max_flow()' previously defined here
   76 | inline int max_flow() {
      |            ^~~~~~~~
a.cc:197:5: error: redefinition of 'int main()'
  197 | int main() {
      |     ^~~~
a.cc:83:5: note: 'int main()' previously defined here
   83 | int main() {
      |     ^~~~