1797: [Ahoi2009]Mincut 最小割

这题还挺有意思的……呃……我的意思是,反正我不会。。。。。。。

做法嘛,先求最小割是肯定的了,然后就是很坑爹的结论题。

边(u,v)能出现在最小割集中当且仅当(u,v)为满流且在残量网络里u,v不属于同一个强连通分量。

边(u,v)必定出现在最小割集中当且仅当(u,v)为满流且在残量网络中u与s在同一强连通分量且v与t在同一强连通分量。

证明什么的,画个图试试?

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
const int inf=1e9;
struct Edge{int to,next,v;}e[200005];
int head[4005],d[4005],cnt=1;
int sccno[4005],dfs_clock,scc_cnt,lowlink[4005],pre[4005];
stack<int>s;
void ins(int u,int v,int w){
	cnt++;e[cnt].to=v;e[cnt].next=head[u];e[cnt].v=w;head[u]=cnt;
}
void insert(int u,int v,int w){
	ins(u,v,w);ins(v,u,0);
}
bool bfs(int s,int t){
	memset(d,-1,sizeof(d));
	d[s]=0;queue<int>q;q.push(s);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int i=head[u];i;i=e[i].next)
		if(e[i].v&&d[e[i].to]==-1){
			d[e[i].to]=d[u]+1;
			q.push(e[i].to);
		}
	}
	return d[t]!=-1;
}
int dfs(int x,int f,int t){
	if(x==t)return f;
	int flow=0,w;
	for(int i=head[x];i;i=e[i].next)
	if(e[i].v&&d[e[i].to]==d[x]+1){
		w=f-flow;
		w=dfs(e[i].to,min(e[i].v,w),t);
		e[i].v-=w;e[i^1].v+=w;flow+=w;
		if(flow==f)return f;
	}
	if(!flow)d[x]=-1;
	return flow;
}
int dinic(int s,int t){int flow=0;while(bfs(s,t))flow+=dfs(s,inf,t);return flow;}
void tarjan(int u){
	pre[u]=lowlink[u]=++dfs_clock;
	s.push(u);
	for(int i=head[u];i;i=e[i].next)
	if(e[i].v){
		int v=e[i].to;
		if(!pre[v]){
			tarjan(v);
			lowlink[u]=min(lowlink[u],lowlink[v]);
		}else if(!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]);
	}
	if(lowlink[u]==pre[u]){
		scc_cnt++;
		while(true){
			int x=s.top();s.pop();
			sccno[x]=scc_cnt;
			if(x==u)break;
		}
	}
}
int main(){
	int n,m,s,t;
	scanf("%d%d%d%d",&n,&m,&s,&t);
	int u,v,c;
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&u,&v,&c);
		insert(u,v,c);
	}
	dinic(s,t);
	for(int i=1;i<=n;i++)if(!pre[i])tarjan(i);
	for(int i=1;i<=m;i++)
	if(e[i<<1].v)
	printf("0 0\n");
	else{
		int u=e[i<<1].to,v=e[i<<1|1].to;
		printf("%d %d\n",sccno[u]!=sccno[v],sccno[s]==sccno[v]&&sccno[t]==sccno[u]);
	}
	return 0;
}


AHOI2001是一种用于处理模式匹配和字符串搜索的经典算法,全称为"Another Happy Odyssey in 2001"。它通常应用于构建高效、空间优化的KMP(Knuth-Morris-Pratt)算法的一种改进版本。这种有限自动机常用于处理字符串搜索问题,尤其是在处理大量文本数据时。 关于题目代码的具体内容,这通常涉及到编程竞赛或算法实现题。通常,你需要编写一段程序,包括定义一个有限状态机(Finite Automaton),处理输入字符串和模式串,并根据AHOI2001算法来查找模式是否在原字符串中。关键部分会涉及如何创建前缀函数表、动态规划和自适应策略。 由于这不是一个直接的答案,下面是一个简化版的代码框架示例(假设用Python): ```python class AhoCorasickAutomaton: def __init__(self, patterns): self.prefix_func = self.build_prefix_function(patterns) def build_prefix_function(self, patterns): # 建立前缀函数表的计算过程... pass def search(self, text): index = 0 for pattern in patterns: while index < len(text) and index + len(pattern) <= len(text): if self.match(text[index:], pattern): return True index += self.prefix_func[pattern] return False def match(self, text, pattern): # 匹配函数,比较两个字符串是否相等... pass # 使用示例: patterns = ['AB', 'AC'] # 输入模式列表 automaton = AhoCorasickAutomaton(patterns) text = 'ABCABCD' # 待搜索的字符串 if automaton.search(text): print("Pattern found") else: print("Pattern not found")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值