bzoj1797 [Ahoi2009]Mincut 最小割 网络流+强连通分量

题目大意:
给一张图,S和T。
对于每一条边,问这条边是否可以在最小割中,是否一定在最小割中。

题目分析:
http://hzwer.com/3217.html
先跑一边网络流,然后在残量网络中进行强连通分量缩点。

对于第一问,如果u和v不在一个强连通分量中,则这条边可以在最小割中。
对于第二问,如果u和S在一个强连通分量中,并且v和T在一个强连通分量中,在这条边必须在最小割中。

代码如下:

#include <cstdio>
#include <cstring>
#define N 5200
#define M 220000
using namespace std;
const int INF=2147483647;
inline int Min(int x,int y) { return x<y?x:y; }
int n,m,S,T;
int fir[N],nes[M],v[M],q[M],tot=1;
int cur[N],d[N],dl[N];
int scc[N],pre[N],low[N],sta[N],dfn,jsq,top;
void edge(int x,int y,int z)
{
    v[++tot]=y;
    q[tot]=z;
    nes[tot]=fir[x];
    fir[x]=tot;
}
#define edge(x,y,z) edge(x,y,z),edge(y,x,0)
bool bfs()
{
    static int c;
    memset(d,0,sizeof(d));
    int l=1,r=1;
    dl[1]=S; d[S]=1;
    while(l<=r)
    {
        c=dl[l++];
        for(int t=fir[c];t;t=nes[t])
        {
            if(!q[t] || d[v[t]]) continue;
            d[v[t]]=d[c]+1;
            dl[++r]=v[t];
            if(v[t]==T) return true;
        }
    }
    return false;
}
int dfs(int c,int flow)
{
    if(c==T || flow==0) return flow;
    int ans=0;
    for(int& t=cur[c];t;t=nes[t])
    {
        if(!q[t] || d[v[t]]!=d[c]+1) continue;
        int tmp=dfs(v[t],Min(q[t],flow));
        q[t]-=tmp; q[t^1]+=tmp;
        flow-=tmp; ans+=tmp;
        if(!flow) break;
    }
    if(!ans) d[c]=-1;
    return ans;
}
void dinic()
{
    while(bfs())
    {
        for(int i=1;i<=n;i++) cur[i]=fir[i];
        dfs(S,INF);
    }
}
void tarjan(int c)
{
    pre[c]=low[c]=++dfn;
    sta[++top]=c;
    for(int t=fir[c];t;t=nes[t])
    {
        if(!q[t]) continue;
        if(!pre[v[t]])
        {
            tarjan(v[t]);
            low[c]=Min(low[c],low[v[t]]);
        }
        else if(!scc[v[t]]) low[c]=Min(low[c],pre[v[t]]);
    }
    if(low[c]==pre[c])
    {
        jsq++;
        do scc[sta[top]]=jsq; while(sta[top--]!=c);
    }
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&S,&T);
    for(int i=1,x,y,z;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        edge(x,y,z);
    }
    dinic();
    for(int i=1;i<=n;i++)
        if(!pre[i]) tarjan(i);
    for(int i=2;i<=tot;i+=2)
    {
        if(q[i]) printf("0 0\n");
        else{
            if(scc[v[i]]!=scc[v[i^1]]) printf("1 ");
            else printf("0 ");
            if(scc[v[i]]==scc[T] && scc[v[i^1]]==scc[S]) printf("1\n");
            else printf("0\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值