[最小割+Tarjan] BZOJ1797: [Ahoi2009]Mincut 最小割

关于最小割唯一性:

在残余网络上跑 Tarjan T a r j a n 。记 idx i d x 为点 x x 所在 SCC 的编号。

将每个 SCC S C C 缩成一个点,得到的新图就只含有满流边了。那么新图的任一 ST S − T 割都对应原图的某个最小割。

对于任意一条满流边 (u,v) ( u , v ) ,若能够出现在某个最小割集中,当且仅当 iduidv i d u ≠ i d v 。对于一个 SCC S C C 内部的一条满流边,割了它会导致割到非满流边,一定不是最小割。

对于任意一条满流边 (u,v) ( u , v ) ,若必定出现在最小割集中,当且仅当 idu=ids i d u = i d s idv=idt i d v = i d t

#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#include<algorithm>
using namespace std;
inline char gc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int getint(){
  char ch=gc(); int res=0,ff=1;
  while(!isdigit(ch)) ch=='-'?ff=-1:0, ch=gc();
  while(isdigit(ch)) res=(res<<3)+(res<<1)+ch-'0', ch=gc();
  return res*ff;
}
const int maxn=100005,maxe=200005;
int N,m,S,T;
struct Edge{
  int from,to,cap,flow;
  Edge(int t1=0,int t2=0,int t3=0,int t4=0){ from=t1;to=t2;cap=t3;flow=t4; }
} Es[maxe];
int fir[maxn],nxt[maxe],tot=1,pos_fir[maxn],d[maxn];
void add(int x,int y,int z){
  Es[++tot]=Edge(x,y,z,0);
  nxt[tot]=fir[x]; fir[x]=tot;
  Es[++tot]=Edge(y,x,0,0);
  nxt[tot]=fir[y]; fir[y]=tot;
}
queue<int> que;
bool Bfs(){
  memset(d,63,sizeof(d)); int INF=d[0];
  d[S]=0; que.push(S);
  while(!que.empty()){
    int x=que.front(); que.pop();
    for(int j=fir[x];j;j=nxt[j])
      if(Es[j].cap>Es[j].flow&&d[Es[j].to]==INF) d[Es[j].to]=d[x]+1, que.push(Es[j].to);
  }
  return d[T]!=INF;
}
int Dfs(int x,int flw){
  if(x==T||flw==0) return flw;
  int t,res=0;
  for(int &j=pos_fir[x];j;j=nxt[j])
    if(d[x]+1==d[Es[j].to]&&(t=Dfs(Es[j].to,min(Es[j].cap-Es[j].flow,flw)))>0){
      res+=t; flw-=t; Es[j].flow+=t; Es[j^1].flow-=t;
      if(flw==0) break;
    }
  return res;
}
int MaxFlow(){
  int res=0;
  while(Bfs()){
    for(int i=1;i<=N;i++) pos_fir[i]=fir[i];
    res+=Dfs(S,1e9);
  }
  return res;
}
int blg[maxn],G,instk[maxn],dfn[maxn],low[maxn],Tim,stk[maxn],top;
void Tarjan(int x){
  dfn[x]=low[x]=++Tim; stk[++top]=x; instk[x]=true;
  for(int j=fir[x];j;j=nxt[j])
    if(Es[j].cap>Es[j].flow){
      int v=Es[j].to;
      if(!dfn[v]) Tarjan(v), low[x]=min(low[x],low[v]);
      else if(instk[v]) low[x]=min(low[x],dfn[v]);
    }
  if(dfn[x]==low[x]){
    G++;
    do{
      blg[stk[top]]=G;
      instk[stk[top]]=false;
    }while(stk[top--]!=x);
  }
}
int main(){
  scanf("%d%d%d%d",&N,&m,&S,&T);
  for(int i=1;i<=m;i++){
    int x=getint(),y=getint(),z=getint();
    add(x,y,z);
  }
  MaxFlow();
  for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i);
  // for(int i=1;i<=N;i++) printf("%d ",blg[i]); printf("\n");
  for(int j=2;j<=(m<<1);j+=2){
    if(Es[j].cap>Es[j].flow) puts("0 0");
    else printf("%d %d\n",blg[Es[j].from]!=blg[Es[j].to],blg[Es[j].from]==blg[S]&&blg[Es[j].to]==blg[T]);  
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值