[bzoj1797]Mincut 最小割
是否是最小割的判断方法
就是把残量网络跑一下tarjan(有流量的时候有边)
可能出现:
满流且co[u]!=co[v]
一定出现
co[u]==S&&co[v]==T
- 代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=2e6+5;
int n,m;
const int INF=0x3f3f3f3f;
namespace graph{
int hed[N],to[M],nxt[M],cap[M],cnt=1,S,T;
queue<int>Q;
int d[N];
inline void adde(int u,int v,int _fl){
++cnt;to[cnt]=v,nxt[cnt]=hed[u],cap[cnt]=_fl;hed[u]=cnt;
++cnt;to[cnt]=u,nxt[cnt]=hed[v],cap[cnt]=0;hed[v]=cnt;
}
inline bool bfs(){
for(int i=1;i<=n;i++)d[i]=0;
Q.push(S);d[S]=1;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=hed[u];i;i=nxt[i])if(cap[i]){
int v=to[i];
if(!d[v])d[v]=d[u]+1,Q.push(v);
}
}
return d[T]>0;
}
inline int dfs(int x,int F){
if(!F)return 0;
if(x==T)return F;
int rst=F,cur=0;
for(int i=hed[x];i;i=nxt[i]){
int v=to[i];if(d[v]!=d[x]+1)continue;
int ff=dfs(v,min(cap[i],rst));
rst-=ff,cur+=ff,cap[i]-=ff,cap[i^1]+=ff;
if(rst==0)break;
}
if(!cur)d[x]=0;
return cur;
}
int dinic(){
int maxflow=0;
while(bfs()) maxflow+=dfs(S,INF);
return maxflow;
}
}using namespace graph;
int l[M],r[M],id[N];
int dfn[N],low[N],num;
int stk[N],top;
int co[N],col;
inline void tarjan(int x){
stk[++top]=x,dfn[x]=low[x]=++num;
for(int i=hed[x];i;i=nxt[i])if(cap[i]){
int v=to[i];
if(!dfn[v]){
tarjan(v);
low[x]=min(low[x],low[v]);
}else if(!co[v])low[x]=min(low[x],low[v]);
}
if(dfn[x]==low[x]){
++col;
while(stk[top]!=x)co[stk[top--]]=col;
co[stk[top--]]=col;
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&S,&T);
for(int i=1;i<=m;i++){
int cap;scanf("%d%d%d",&l[i],&r[i],&cap);
adde(l[i],r[i],cap);
id[i]=cnt-1;
}
dinic();
for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
for(int i=1;i<=m;i++){
int u=l[i],v=r[i],ff=cap[id[i]];
printf("%d ",co[u]!=co[v]&&ff==0);
printf("%d\n",co[u]==co[S]&&co[v]==co[T]);
}
}