1797: [Ahoi2009]Mincut 最小割(from hzwer)

本文介绍了如何使用Tarjan算法在残余网络中找到所有强连通分量(SCC),并利用这些信息来确定哪些满流边可能属于最小割集。通过分析SCC之间的连接特性,文章给出了两个关键结论,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jcvb:

在残余网络上跑tarjan求出所有SCC,记id[u]为点u所在SCC的编号。显然有id[s]!=id[t](否则s到t有通路,能继续增广)。

①对于任意一条满流边(u,v),(u,v)能够出现在某个最小割集中,当且仅当id[u]!=id[v];
②对于任意一条满流边(u,v),(u,v)必定出现在最小割集中,当且仅当id[u]==id[s]且id[v]==id[t]。

<==将每个SCC缩成一个点,得到的新图就只含有满流边了。那么新图的任一s-t割都对应原图的某个最小割,从中任取一个把id[u]和id[v]割开的割即可证明。


<==:假设将(u,v)的边权增大,那么残余网络中会出现s->u->v->t的通路,从而能继续增广,于是最大流流量(也就是最小割容量)会增大。这即说明(u,v)是最小割集中必须出现的边。

code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
const int inf=(1<<28);
struct node{
    int x,y,next,c,other;
}a[150000];int last[4100],len=0;
int h[4100],s[4100],st,ed,n,m,top=0,dfn[4100],low[4100],belong[4100],id=0,cnt=0;
bool u[4100];
void ins(int x,int y,int c)
{
    int k1=++len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
    int k2=++len;
    a[len].x=y;a[len].y=x;a[len].c=0;
    a[len].next=last[y];last[y]=len;
    a[k1].other=k2;a[k2].other=k1;
}
bool bt_h()
{
    memset(h,0,sizeof(h));
    int l=1,r=2;s[l]=st;h[st]=1;
    while(l!=r)
    {
        int x=s[l];
        for(int i=last[x];i;i=a[i].next)
        {
            int y=a[i].y;
            if(h[y]==0&&a[i].c>0) h[y]=h[x]+1,s[r++]=y;
        }
        l++;
    }
    return h[ed]!=0;
}
int findflow(int x,int f)
{
    if(x==ed) return f;
    int t,ans=0;
    for(int i=last[x];i;i=a[i].next)
    {
        int y=a[i].y;
        if(h[x]+1==h[y]&&a[i].c>0&&ans<f)
        {
            ans+=(t=findflow(y,min(a[i].c,f-ans)));
            a[i].c-=t;a[a[i].other].c+=t;
        }
    }
    if(ans==0) h[x]=0;
    return ans;
}
void dfs(int x)
{
    dfn[x]=low[x]=++id;
    s[++top]=x;u[x]=true;
    for(int i=last[x];i;i=a[i].next)
	    if(a[i].c)
	    {
	        int y=a[i].y;
	        if(dfn[y]==-1)
	        {
	            dfs(y);
	            low[x]=min(low[x],low[y]);
	        }
	        else
	            if(u[y]) low[x]=min(low[x],dfn[y]);
	    }
    if(low[x]==dfn[x])
    {
        cnt++;int i;
        do
        {
            i=s[top--];
            u[i]=false;
            belong[i]=cnt;
        }while(i!=x);
    }
}
int main()
{
	scanf("%d %d %d %d",&n,&m,&st,&ed);
	for(int i=1;i<=m;i++)
	{
		int x,y,c;scanf("%d %d %d",&x,&y,&c);
		ins(x,y,c);
	}
	while(bt_h()) findflow(st,inf);
	memset(u,false,sizeof(u));
	memset(dfn,-1,sizeof(dfn));
	for(int i=1;i<=n;i++) if(dfn[i]==-1) dfs(i);
	int X=belong[st],Y=belong[ed];
	for(int i=1;i<=len;i+=2)
	{
		int x=belong[a[i].x],y=belong[a[i].y];
		if(a[i].c==0&&x!=y) printf("1 ");
		else printf("0 ");
		if(a[i].c==0&&x==X&&y==Y) printf("1\n");
		else printf("0\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值