ISAP模板
灰鹤在此
·
·
个人记录
#include<bits/stdc++.h>
using namespace std;
using type=int;
const type inf=(1<<30);
#define debug cerr<<x<<" "<<y<<" "<<flow<<endl;
const int MaxN=1e4+5,MaxM=1e6+5;
struct edge{
int next,to;
type flow;
}e[MaxM<<1];
int head[MaxN],cnt=1;
inline void ADD(int x,int y,type flow){
e[++cnt]={head[x],y,flow};
head[x]=cnt;
}
inline void add(int x,int y,type flow){
ADD(x,y,flow);
ADD(y,x,0);
}
int all,dep[MaxN],gap[MaxN],cur[MaxN];
inline void bfs(int s,int t){
all=1;
queue<int>q;
q.push(t);
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
dep[t]=0;
gap[0]=1;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=head[x];i;i=e[i].next){
int y=e[i].to;
if(dep[y]){
dep[y]=dep[x]+1;
q.push(y);
}
}
}
for(int i=0;i<MaxN;i++){
if(!dep[i]){
all++;
gap[dep[i]]++;
}
}
gap[dep[t]]++;
}
inline type dfs(int s,int x,type flow,int t){
if(x==t){
return flow;
}
type limit,res=0;
for(int i=cur[x];i&&flow;i=e[i].next){
cur[x]=i;
int y=e[i].to;
if(dep[y]==dep[x]-1&&e[i].flow){
limit=dfs(s,y,min(e[i].flow,flow-res),t);
if(limit){
e[i].flow-=limit;
e[i^1].flow+=limit;
res+=limit;
if(res==flow){
return flow;
}
}
}
}
if(!(--gap[dep[x]])){
dep[s]=all+1;
}
gap[++dep[x]]++;
return res;
}
inline type ISAP(int s,int t){
bfs(s,t);
type res=0;
while(dep[s]<=all){
memcpy(cur,head,sizeof(cur));
res+=dfs(s,s,inf,t);
}
return res;
}
inline void init(){
memset(head,0,sizeof(head));
cnt=1;
}
int n,m,s,t;
inline void build(){
cin>>n>>m;
cout<<ISAP(s,t)<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
t=MaxN-1;
build();
return 0;
}