#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL MAXN=1e5+5;
LL n,m;
LL tot=1,st[MAXN],to[MAXN],nx[MAXN],len[MAXN];
void add(LL u,LL v,LL w){
to[++tot]=v;nx[tot]=st[u];len[tot]=w;st[u]=tot;
to[++tot]=u;nx[tot]=st[v];len[tot]=0;st[v]=tot;
}
LL dep[MAXN],S,T;
queue<LL>q;
LL bfs(){
memset(dep,0,sizeof(dep));
dep[S]=1;q.push(S);
while(!q.empty()){
LL u=q.front();q.pop();
for(LL i=st[u];i;i=nx[i]){
if(len[i]>0&&!dep[to[i]]){
dep[to[i]]=dep[u]+1;q.push(to[i]);
}
}
}
return dep[T];
}
LL dinic(LL u,LL flow){
if(u==T)return flow;
LL rest=flow,tmp;
for(LL i=st[u];i;i=nx[i]){
if(len[i]>0&&dep[to[i]]==dep[u]+1){
tmp=dinic(to[i],min(rest,len[i]));
if(!tmp)dep[to[i]]=0;
rest-=tmp;len[i]-=tmp;len[i^1]+=tmp;
}
}
return flow-rest;
}
int main(){
freopen("sequence.in","r",stdin);
freopen("sequence.out","w",stdout);
scanf("%lld%lld",&n,&m);
S=0,T=2*n+1;
for(LL i=1;i<=m;i++){
LL u,v;
scanf("%lld%lld",&u,&v);
add(u,v+n,1e9);
}
for(LL i=1;i<=n;i++){
add(S,i,1);add(i+n,T,1);
}
LL ans=0;
while(bfs()){
while(LL flow=dinic(S,1e9))ans+=flow;
}
printf("%lld\n",n*(2*n-ans));
}
dinic模板
最新推荐文章于 2024-08-15 16:09:23 发布