Topic Link
https://atcoder.jp/contests/agc038/tasks/agc038_f
answer
Good question.
First we observed a nature, for alignment \ (P \) , each formed in its rotation point \ (A_i \) is selected from the group \ (I \) or choose \ (P_i \) status must be the same. \ (Q_i \) empathy.
Then converted to minimize \ (A_i = B_i \) position \ (I \) number.
Consider \ (A_i = B_i \) conditions:
(. 1) \ (P_i Q_i = I = \) , then this position useless, \ (A_i B_i = \) must satisfy.
(2) \ (P_i = I, Q_i \ Ne I \) , Provisions \ (A_i = B_i \) , etc.价于\ (B_i = I \) .
(3) \ (P_i \ Ne I, Q_i = I \) , Provisions \ (A_i = B_i \) , etc.价于\ (A_i = I \) .
(. 4) \ (P_i Q_i = \ I NE \) , then \ (A_i = B_i \) is equivalent to \ (A_i \) and \ (B_i \) selected status (that is, \ (I \) or \ ( P_i \) or \ (Q_i \) ) different.
(5) \ (P_i \ Ne Q_i \ Ne I \) , Provisions \ (A_i = B_i \) , etc.价于\ (A_i = i \)且\ (B_i = I \) .
Then we can observe from a set partitioning model: all the points are divided into \ (S, T \) two sets, set \ (A_i = i \) represents \ (A_i \) in \ (S \) set, \ (A_i = P_i \) represents \ (A_i \) in \ (T \) set, \ (B_i = I \) represents \ (I \) in \ (T \) set, \ (B_i = Q_i \) represents \ (I \) in \ (S \) set. Then the conditions described above can be converted to:
for each location \ (I \) :
(1) \ (P_i Q_i = \) , this position is useless, be sure to spend \ (1 \) price.
(2) \ (P_i = I, Q_i \ NE I \) , if the \ (B_i \) in \ (T \) Set takes \ (1 \) price.
(. 3) \ (P_i \ NE I, I Q_i = \) , if the \ (A_i \) in \ (S \) Set takes \ (1 \) cost.
(. 4) \ (P_i Q_i = \ I NE \) , if the \ (A_i \) and \ (B_i \) at different set takes \ (1 \) price.
(. 5) \ (P_i \ NE Q_i \ NE I \) , then if \ (A_i \) in \ (S \) Set and \ (B_i \) in \ (T \) Set takes \ (1 \) of cost.
Thus, the establishment of a point of rotation for each, and then for each \ (I \) discussion classification, it corresponds to the rotation side can be connected.
Since the graph is built out of a bipartite graph, and the edge weights are \ (1 \) , so the time complexity \ (O (n-\ n-sqrt) \) .
After I finished up TLE, and finally found that actually I wrote two years to optimize the current arc has been a fake ...... 38 hanging hair really am speechless ......
Code
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
const int INF = 1e7;
namespace NetFlow
{
const int N = 2e5+2;
const int M = 4e5;
struct Edge
{
int v,w,nxt,rev;
} e[(M<<1)+3];
int fe[N+3];
int te[N+3];
int dep[N+3];
int que[N+3];
int n,en,s,t;
void addedge(int u,int v,int w)
{
// printf("addedge %d %d %d\n",u,v,w);
en++; e[en].v = v; e[en].w = w;
e[en].nxt = fe[u]; fe[u] = en; e[en].rev = en+1;
en++; e[en].v = u; e[en].w = 0;
e[en].nxt = fe[v]; fe[v] = en; e[en].rev = en-1;
}
bool bfs()
{
for(int i=1; i<=n; i++) dep[i] = 0;
int head = 1,tail = 1; que[1] = s; dep[s] = 1;
while(head<=tail)
{
int u = que[head]; head++;
for(int i=fe[u]; i; i=e[i].nxt)
{
int v = e[i].v;
if(e[i].w>0 && dep[v]==0)
{
dep[v] = dep[u]+1;
if(v==t)return true;
tail++; que[tail] = v;
}
}
}
return false;
}
int dfs(int u,int cur)
{
if(u==t||cur==0) {return cur;}
int rst = cur;
for(int &i=te[u]; i; i=e[i].nxt)
{
int v = e[i].v;
if(e[i].w>0 && rst>0 && dep[v]==dep[u]+1)
{
int flow = dfs(v,min(rst,e[i].w));
if(flow>0)
{
e[i].w -= flow;
rst -= flow;
e[e[i].rev].w += flow;
if(rst==0) {return cur;}
}
}
}
if(rst==cur) {dep[u] = -2;}
return cur-rst;
}
int dinic(int _n,int _s,int _t)
{
n = _n,s = _s,t = _t;
int ret = 0;
while(bfs())
{
for(int i=1; i<=n; i++) te[i] = fe[i];
memcpy(te,fe,sizeof(int)*(n+1));
ret += dfs(s,INF);
}
return ret;
}
}
using NetFlow::addedge;
using NetFlow::dinic;
const int N = 1e5;
int a[N+3],b[N+3];
int ca[N+3],cb[N+3];
int n,cnta,cntb;
int main()
{
scanf("%d",&n);
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]++;
for(int i=1; i<=n; i++)
{
if(!ca[i])
{
cnta++;
int x = i;
do
{
ca[x] = cnta;
x = a[x];
} while(x!=i);
}
}
for(int i=1; i<=n; i++)
{
if(!cb[i])
{
cntb++;
int x = i;
do
{
cb[x] = cntb;
x = b[x];
} while(x!=i);
}
}
// printf("ca: "); for(int i=1; i<=n; i++) printf("%d ",ca[i]); puts("");
// printf("cb: "); for(int i=1; i<=n; i++) printf("%d ",cb[i]); puts("");
int ans = n;
for(int i=1; i<=n; i++)
{
if(a[i]==i && b[i]==i) {ans--;}
else if(a[i]==i) {addedge(1,cb[i]+cnta+2,1);}
else if(b[i]==i) {addedge(ca[i]+2,2,1);}
else
{
addedge(ca[i]+2,cb[i]+cnta+2,1);
if(a[i]==b[i]) {addedge(cb[i]+cnta+2,ca[i]+2,1);}
}
}
ans -= dinic(2+cnta+cntb,1,2);
printf("%d\n",ans);
return 0;
}