Submission #55948786
Source Code Expand
// LUOGU_RID: 168483263
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#define int long long
using namespace std;
const int N=2e5+5,inf=0x3f3f3f3f3f3f3f3f;
int n,m,s,t,dep[N],p[N];
struct node{int x,v,id;};
vector<node> v[N];
void add(int x,int y,int val){
int sti=v[x].size(),edi=v[y].size();
v[x].push_back({y,val,edi});
v[y].push_back({x,0,sti});
}
bool bfs(){
queue<int> q;
memset(dep,-1,sizeof(dep));
memset(p,0,sizeof(p));
q.push(s),dep[s]=0;
while(!q.empty()){
int top=q.front();q.pop();
for(node i:v[top]){
if(dep[i.x]==-1&&i.v){
dep[i.x]=dep[top]+1;
q.push(i.x);
}
}
}
return dep[t]!=-1;
}
int dfs(int x,int flow){
int tmp=flow;
if(x==t||!flow){
return flow;
}
for(int id=p[x];id<v[x].size()&&tmp;id++){
p[x]=id;
node &i=v[x][id];
if(dep[x]+1==dep[i.x]&&i.v>0){
int t=dfs(i.x,min(i.v,tmp));
if(!t){
dep[i.x]=-1;
}
else{
i.v-=t;
v[i.x][i.id].v+=t;
tmp-=t;
}
}
}
return flow-tmp;
}
int dinic(){
int ans=0;
while(bfs()){
ans+=dfs(s,inf);
}
return ans;
}
int a[N],b[N],ans,f1[N],f2[N],tot;
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n,ans=n,t=N-1;
for(int i=1;i<=n;i++){
cin>>a[i],a[i]++;
}
for(int i=1;i<=n;i++){
cin>>b[i],b[i]++;
}
for(int i=1,x;i<=n;i++){
if(!f1[i]){
x=i,tot++;
do{
f1[x]=tot,x=a[x];
}while(!f1[x]);
}
if(!f2[i]){
x=i,tot++;
do{
f2[x]=tot,x=b[x];
}while(!f2[x]);
}
}
// for(int i=1;i<=n;i++) cout<<f1[i]<<' ';
// cout<<'\n';
for(int i=1;i<=n;i++){
if(a[i]==i&&b[i]==i) ans--;
else if(a[i]==i) add(f2[i],t,1);
else if(b[i]==i) add(s,f1[i],1);
else if(a[i]!=b[i]) add(f2[i],f1[i],1);
else add(f2[i],f1[i],1),add(f1[i],f2[i],1);
}
cout<<ans-dinic()<<'\n';
return 0;
}
Submission Info
Submission Time
2024-07-25 17:14:03+0900
Task
F - Two Permutations
User
do_it_tomorrow
Language
C++ 17 (gcc 12.2)
Score
1600
Code Size
2300 Byte
Status
AC
Exec Time
1588 ms
Memory
27440 KiB
Compile Error
Main.cpp: In function ‘long long int dfs(long long int, long long int)’:
Main.cpp:38:23: warning: comparison of integer expressions of different signedness: ‘long long int’ and ‘std::vector<node>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
38 | for(int id=p[x];id<v[x].size()&&tmp;id++){
| ~~^~~~~~~~~~~~
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
1600 / 1600
Status
Set Name
Test Cases
Sample
sample-01.txt, sample-02.txt, sample-03.txt
All
01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name
Status
Exec Time
Memory
01-01.txt
AC
4 ms
11432 KiB
01-02.txt
AC
5 ms
11648 KiB
01-03.txt
AC
54 ms
22764 KiB
01-04.txt
AC
53 ms
22848 KiB
01-05.txt
AC
36 ms
22616 KiB
01-06.txt
AC
39 ms
22808 KiB
01-07.txt
AC
28 ms
22448 KiB
01-08.txt
AC
36 ms
22832 KiB
01-09.txt
AC
27 ms
22684 KiB
01-10.txt
AC
31 ms
23092 KiB
01-11.txt
AC
25 ms
22016 KiB
01-12.txt
AC
41 ms
27440 KiB
01-13.txt
AC
537 ms
24156 KiB
01-14.txt
AC
94 ms
26660 KiB
01-15.txt
AC
92 ms
22596 KiB
01-16.txt
AC
113 ms
23048 KiB
01-17.txt
AC
555 ms
22356 KiB
01-18.txt
AC
668 ms
22360 KiB
01-19.txt
AC
1588 ms
22504 KiB
sample-01.txt
AC
4 ms
11340 KiB
sample-02.txt
AC
4 ms
11424 KiB
sample-03.txt
AC
4 ms
11208 KiB