bzoj1797: [Ahoi2009]Mincut 最小割

来源:互联网 发布:网络教育英语难不难 编辑:程序博客网 时间:2024/06/05 18:07

首先一定跑一遍最小割,在残量网络上tarjan一遍
(1) 满足u和v不在同一个强连通分量且有流通过。
(2)满足(1)且u在s的强连通分量,v在t的强连通分量。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<stack>using namespace std;stack<int> s1;queue<int> q;int read(){    char ch=getchar();int f=0;    while(ch>'9'||ch<'0') ch=getchar();    while(ch>='0'&&ch<='9') {f=f*10+(ch^48);ch=getchar();}    return f;}int n,m,s,t,dis[4005],head[4005],dfn[4005],low[4005],pos[4005],size[4005];int tim,tot,cnt;long long ans;bool vis[4005];struct node{    int from;    int to;    int next;    int w;}edge[120005];void add(int u,int v,int w){    edge[tot].from=u;    edge[tot].to=v;    edge[tot].w=w;    edge[tot].next=head[u];    head[u]=tot++;}bool bfs(){    memset(dis,0,sizeof(dis));    dis[s]=1;    q.push(s);    while(!q.empty())    {        int x=q.front();        q.pop();        for(int i=head[x];i!=-1;i=edge[i].next)        {            if(!dis[edge[i].to]&&edge[i].w)            {                dis[edge[i].to]=dis[x]+1;                q.push(edge[i].to);            }        }    }    if(!dis[t])    return 0;    return 1;}int dfs(int x,int flow){    if(x==t||!flow)    return flow;    int ret=0;    for(int i=head[x];i!=-1;i=edge[i].next)    {        if(dis[edge[i].to]==dis[x]+1)        {            int f=dfs(edge[i].to,min(flow,edge[i].w));            edge[i].w-=f;edge[i^1].w+=f;            ret+=f;            flow-=f;            if(!flow)            break;        }    }    if(!ret)    dis[x]=-1;    return ret;}void dinic(){    while(bfs())    {        ans+=dfs(s,0x7f7f7f7f);    }}void tarjan(int x){    dfn[x]=low[x]=++tim;    s1.push(x);    vis[x]=1;    for(int i=head[x];i!=-1;i=edge[i].next)    {        if(edge[i^1].w)        {            if(!dfn[edge[i].to])            {                tarjan(edge[i].to);                 low[x]=min(low[x],low[edge[i].to]);            }            else if(vis[edge[i].to])            {                low[x]=min(low[x],dfn[edge[i].to]);            }         }    }    if(low[x]==dfn[x])    {        cnt++;        while(s1.top()!=x)        {            pos[s1.top()]=cnt;            size[cnt]++;            vis[s1.top()]=0;            s1.pop();        }         s1.pop();        pos[x]=cnt;        size[cnt]++;        vis[x]=0;    } }int main(){    //freopen("input.txt","r",stdin);    //freopen("output.txt","w",stdout);    memset(head,-1,sizeof(head));    n=read(),m=read(),s=read(),t=read();    int x,y,z;    for(int i=1;i<=m;i++)    {        x=read(),y=read(),z=read();        add(x,y,z);        add(y,x,0);    }    dinic();    for(int i=1;i<=n;i++)    {        if(!dfn[i])        tarjan(i);    }    for(int i=0;i<tot;i+=2)    {        if(pos[edge[i].from]!=pos[edge[i].to]&&!edge[i].w)        {            printf("1 ");            if(pos[s]==pos[edge[i].from]&&pos[t]==pos[edge[i].to])            {                puts("1");            }            else puts("0");         }        else        {            puts("0 0");        }    }    //for(int i=1;i<=n;i++)    //printf("%d ",pos[i]);}
原创粉丝点击