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");
}
}