dinic模板

#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));
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值