●BZOJ 1797 [Ahoi2009]Mincut 最小割

题链:

http://www.lydsy.com/JudgeOnline/problem.php?id=1797

题解:

详细的讲解去看http://hzwer.com/3217.html
首先跑一个最小割。
那么剩下的就是一个结论的事了:
对残余网络跑一个Tarjan缩点,
1).对于一条满载边u->v,u->v能够出现在某个最小割集中,当且仅当u,v不属于同一个SCC;
2).对于一条满载边u->v,u->v必定出现在最小割集中,当且仅当u,v分别在S,T的SCC中。(u,v必然不在一个SCC中)

代码:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 4500
#define MAXM 125000
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
	int from[MAXM],to[MAXM],cap[MAXM],nxt[MAXM],head[MAXN],ent;
	void Init(){
		ent=2;
		memset(head,0,sizeof(head));
	}
	void Adde(int u,int v,int w){
		from[ent]=u; to[ent]=v; cap[ent]=w; nxt[ent]=head[u]; head[u]=ent++;
		from[ent]=v; to[ent]=u; cap[ent]=0; nxt[ent]=head[v]; head[v]=ent++;
	}
	int Next(int i,bool type){
		return type?head[i]:nxt[i];
	}
}E;
int dfn[MAXN],low[MAXN],bel[MAXN],sta[MAXN],tim,top,cnt;
int cur[MAXN],d[MAXN];
bool ins[MAXN];
int N,M,S,T;
bool bfs(){
	queue<int> q;
	memset(d,0,sizeof(d));
	d[S]=1; q.push(S); int u,v;
	while(!q.empty()){
		u=q.front(); q.pop();
		for(int i=E.Next(u,1);i;i=E.Next(i,0)){
			v=E.to[i];
			if(d[v]||!E.cap[i]) continue;
			d[v]=d[u]+1; q.push(v);
		}
	}
	return d[T];
}
int dfs(int u,int reflow){
	if(u==T||!reflow) return reflow;
	int flowout=0,f,v;
	for(int &i=cur[u];i;i=E.Next(i,0)){
		v=E.to[i];
		if(d[v]!=d[u]+1) continue;
		f=dfs(v,min(reflow,E.cap[i]));
		flowout+=f; E.cap[i^1]+=f;
		reflow-=f;	E.cap[i]-=f;
		if(!reflow) break;
	}
	if(!flowout) d[u]=0;
	return flowout;
}
int Dinic(){
	int flow=0;
	while(bfs()){
		memcpy(cur,E.head,sizeof(E.head));
		flow+=dfs(S,INF);
	}
	return flow;
}
void Tarjan(int u){
	dfn[u]=low[u]=++tim; sta[++top]=u; ins[u]=1;
	for(int i=E.Next(u,1);i;i=E.Next(i,0)) if(E.cap[i]){
		int v=E.to[i];
		if(!dfn[v]) Tarjan(v),low[u]=min(low[u],low[v]);
		else if(ins[v]) low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u]!=low[u]) return;
	cnt++; int v;
	do{
		v=sta[top--];
		bel[v]=cnt; ins[v]=0;
	}while(v!=u);
}
int main()
{

	E.Init();
	scanf("%d%d%d%d",&N,&M,&S,&T);
	for(int a,b,c,i=1;i<=M;i++)
		scanf("%d%d%d",&a,&b,&c),E.Adde(a,b,c);
	int ans=Dinic();
	for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i);
	for(int i=2,u,v;i<2*M+2;i+=2){
		u=E.from[i]; v=E.to[i];
		if(E.cap[i]||bel[u]==bel[v]) printf("0 0\n");//important
		else{
			printf("1 ");
			if((bel[u]==bel[S]&&bel[v]==bel[T])||(bel[u]==bel[T]&&bel[v]==bel[S]))
				printf("1\n");
			else printf("0\n");
		}
	}
	return 0;
}

posted @ 2017-11-30 20:13  *ZJ  阅读(142)  评论(0编辑  收藏  举报