1797: [Ahoi2009]Mincut 最小割

题目链接

题目大意:给出一个有向图,每条边有流量,给出源点汇点s、t。对于每条边,询问:(1)是否存在一个最小割包含该边(2)是否所有的最小割都包含该边

题解:套路题……知道结论就好了

我的收获:套路++

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;

const int N=4005;
const int M=120005;
const int INF=1e9;

int n,m,st,ed;
int t,head[N],last[N];
int num[N],d[N];
int scnt,col[N];
int tim,dfn[N],low[N];
bool Exit,ins[N];

stack<int> s;

struct edge{int fro,to,c,nex;}e[M*2];

void add(int u,int v,int w){e[t]=(edge){u,v,w,head[u]};last[u]=head[u]=t++;}
void insert(int x,int y,int z){add(x,y,z),add(y,x,0);};

int dfs(int x,int in)
{
    if(x==ed) return in;
    int ans=0,f;
    for(int i=last[x];i!=-1;last[x]=i=e[i].nex)
    {
        int v=e[i].to;
        if(e[i].c&&d[v]==d[x]-1){
            f=dfs(v,min(in-ans,e[i].c));
            ans+=f;e[i].c-=f;e[i^1].c+=f;
            if(Exit||ans==in) return ans;
        }
    }
    if(--num[d[x]]==0) Exit=1;//GAP优化
    d[x]++,num[d[x]]++,last[x]=head[x];
    return ans;
}

void tarjan(int x){
    int now=0;
    dfn[x]=low[x]=++tim;
    s.push(x);ins[x]=1;
    for(int i=head[x];i!=-1;i=e[i].nex){
        int v=e[i].to;
        if(!e[i].c) continue;
        if(!dfn[v]) tarjan(v),low[x]=min(low[x],low[v]);
        else if(ins[v]) low[x]=min(low[x],dfn[v]);
    }
    if(low[x]==dfn[x]){
        scnt++;
        while(x!=now){
            now=s.top();s.pop();
            ins[now]=0;col[now]=scnt;
        }
    }
}

void Isap()
{
    Exit=0;
    while(!Exit) dfs(st,INF);
}

void work()
{
    Isap();
    for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
    for(int i=0;i<t;i+=2){
        putchar((!e[i].c&&col[e[i].fro]!=col[e[i].to])?'1':'0');
        putchar(' ');
        putchar((!e[i].c&&col[st]==col[e[i].fro]&&col[ed]==col[e[i].to])?'1':'0');
        putchar('\n');
    } 
}

void init()
{
    memset(head,-1,sizeof(head));
    memset(last,-1,sizeof(last));
    cin>>n>>m>>st>>ed;num[0]=n;
    for(int x,y,z,i=1;i<=m;i++) scanf("%d%d%d",&x,&y,&z),insert(x,y,z);
}

int main()
{
    init();
    work();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值