跳到正文

个人记录

网络流技巧

gala_xk约 45 分钟
原文
文章信息与版本

文章信息

发布
2025年7月14日
收录
2026年7月23日
更新
2026年11月10日
篇幅
约 45 分钟

版本

  1. v1当前

对偶图

概念

这似乎是离散数学的东西。

首先有一个显然的定理:我们把原图中每一个面作为一个点(外面的面视情况看作一或二个点),将每一条边转九十度,改为双向边,此时对偶图中的一条路径相当于原图中的一个割。

如果在对偶图上跑最短路,最短路即为最小割。

这样做时间复杂度可以被优化到 O(Elog⁡2E)O(E\log_2E),比网络流更加优秀。

例题

P4001 [ICPC-Beijing 2006] 狼抓兔子

题意:求最小割。

使用对偶图即可求解。

代码
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
int n,m;
struct node{
	int u,dis;
	friend bool operator <(node x,node y){
		return x.dis>y.dis;
	}
};
vector<node>ed[4100005];
int pt(int x,int y,int k){
	if(x==n)return (n-1)*(m-1)*2+3;
	if(y==0)return (n-1)*(m-1)*2+3;
	if(x==0)return 0;
	if(y==m)return 0;
	return (x-1)*(m-1)*2+(y-1)*2+k;
}
int dis[4100005],vis[4100005];
priority_queue<node>q;
void work(){
	/*fill your code here*/
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<m;j++){
			int x;
			cin>>x;
			ed[pt(i-1,j,1)].push_back({pt(i,j,2),x});
			ed[pt(i,j,2)].push_back({pt(i-1,j,1),x});
		}
	}
	for(int i=1;i<n;i++){
		for(int j=1;j<=m;j++){
			int x;
			cin>>x;
			ed[pt(i,j,1)].push_back({pt(i,j-1,2),x});
			ed[pt(i,j-1,2)].push_back({pt(i,j,1),x});
		}
	}
	for(int i=1;i<n;i++){
		for(int j=1;j<m;j++){
			int x;
			cin>>x;
			ed[pt(i,j,2)].push_back({pt(i,j,1),x});
			ed[pt(i,j,1)].push_back({pt(i,j,2),x});
		}
	}
	q.push({0,0});
	while(!q.empty()){
		node nd=q.top();q.pop();
//		printf("%d %d %d\n",nd.u,nd.dis,ed[nd.u].size());
		if(vis[nd.u])continue;
		dis[nd.u]=nd.dis;
		vis[nd.u]=1;
		for(int i=0;i<ed[nd.u].size();i++){
			int v=ed[nd.u][i].u,dis=nd.dis+ed[nd.u][i].dis;
			if(!vis[v]){
				q.push({v,dis});
			}
		}
	}
	cout<<dis[pt(n,0,1)];
	return;
}
#ifdef int
signed main()
#else
int main()
#endif
{
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		work();
	}
    return 0;
}

/*
3 3
2 100
100 2
2 100
100 2 100
100 2 100
100 100
2 2

*/

/* Code written by
		gala_xk (MarchWind)*/

T4 种植

题面
题目描述

一片农田被划分成 ( N ) 行和 ( M ) 列的网格,其中一些格子已被毁坏,无法种植农作物。农夫希望在未被毁坏的格子中选择一些格子种植农作物,满足以下条件:

从左上角 ((0,0)) 出发,只能向下或向右移动,最终到达右下角 ((N-1,M-1)),且无论怎么走都必须恰好经过一颗农作物(起点和终点也算经过)。

计算满足条件的种植方案数,结果对 (10^9+7) 取模。两种方案不同当且仅当存在一个格子在其中一种方案中种植了农作物而在另一种方案中没有种植。

输入格式
  • 第一行:两个正整数 ( N, M )。
  • 接下来 ( N ) 行:每行一个长度为 ( M ) 的字符串,描述农田状态。
    • - 表示格子未被毁坏;
    • # 表示格子被毁坏。
  • 保证 ((0,0)) 和 ((N-1,M-1)) 未被毁坏。
输出格式
  • 一行一个非负整数,表示满足条件的种植方案数,对 (10^9+7) 取模。
样例
样例输入 1
代码
3 3  
---  
---  
---  
样例输出 1
代码
5  
样例解释 1

可能的种植方案:

  1. ({(0, 0)});
  2. ({(0, 1), (1, 0)});
  3. ({(0, 2), (1, 1), (2, 0)});
  4. ({(2, 1), (1, 2)});
  5. ({(2, 2)})。
样例输入 2
代码
3 3  
--#  
##-
---  
样例输出 2
代码
64  
样例解释 2

不存在从 ((0,0)) 到 ((N-1,M-1)) 的路径,因此任何种植方案均满足条件。共有 6 个可选格子,方案数为 (2^6 = 64)。

样例输入 3
代码
5 8
------##
-##--#--
---#----
-----##-
#-------
样例输出 3
代码
432  
数据范围与提示
  • 数据范围:

    • (1 \leq N, M \leq 30)。
    • 保证 ((0,0)) 和 ((N-1,M-1)) 未被毁坏。
  • 子任务:

    • 前 10% 数据:(1 \leq N, M \leq 4);
    • 接下来 40% 数据:网格未被毁坏,且 ((N, M)) 为特定值(如 ((5,5))、((30,30)) 等)。
题解
题解

不难发现,题目中的种植相当于求一个图的割,于是我们建对偶图,上面的路径数就是割的数量。但是,这张图的割是在点上,所以,对应地,对偶图的边也要经过原来的点而非边。(相当于转四十五度而非九十度)

至于障碍如何处理,显然,和障碍连的四条边都是无效的,也就是障碍周围四条边的区块实际上是一个区块,所以我们只需要将这四个区块缩成一个点即可,用并查集维护。

注意,对于走不到的点,怎么种都可以,将最终答案乘上这个值即可。

代码
代码
C++
 /* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=100005;
char ch[105][105];
int n,m;
int ac[35][35],aac[35][35];
int pt(int x,int y){
	if(x==n)return (n-1)*(m-1)+3;
	if(y==0)return (n-1)*(m-1)+3;
	if(x==0)return 0;
	if(y==m)return 0;
	return (x-1)*(m-1)+y;
}
ll qpow(ll x,ll y){
	if(y==0)return 1;
	ll z=qpow(x,y/2);
	z=z*z%mod;
	if(y&1)z=z*x%mod;
	return z;
}
int f[10005];
int getf(int x){
	if(f[x]==x)return x;
	return f[x]=getf(f[x]);
}
vector<int>ed[10005];
queue<int>q;
int num[10005],in[10005];
void work(){
	/*fill your code here*/
	cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%s",ch[i]+1);
	ll cc=(ll)n*m;
	int uacnum=0;
	for(int i=0;i<=n*m+1;i++)f[i]=i;
	for(int i=0;i<=n+1;i++)
		for(int j=0;j<=m+1;j++)
		ac[i][j]=aac[i][j]=0;
	ac[0][1]=ac[1][0]=1;
	aac[n+1][m]=aac[n][m+1]=1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ch[i][j]=='#'){
				ac[i][j]=0;
				cc--;
				continue;
			}
			ac[i][j]=(ac[i-1][j]|ac[i][j-1]);
			if(ac[i][j]==0)uacnum++;
		}
	}
	for(int i=n;i>=1;i--){
		for(int j=m;j>=1;j--){
			if(ch[i][j]=='#'){
				aac[i][j]=0;
				continue;
			}
			aac[i][j]=(aac[i+1][j]|aac[i][j+1]);
			if(aac[i][j]==0&&ac[i][j]==1)uacnum++;
		}
	}
//	for(int i=1;i<=n;i++){
//		for(int j=1;j<=m;j++){
//			printf("%d",ac[i][j]);
//		}
//		puts("");
//	}
//	for(int i=1;i<=n;i++){
//		for(int j=1;j<=m;j++){
//			printf("%d",aac[i][j]);
//		}
//		puts("");
//	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ac[i][j]==0||aac[i][j]==0){
				if(getf(pt(i,j-1))!=getf(pt(i,j)))
					f[getf(pt(i,j-1))]=getf(pt(i,j));
				if(getf(pt(i-1,j))!=getf(pt(i,j)))
					f[getf(pt(i-1,j))]=getf(pt(i,j));
				if(getf(pt(i-1,j-1))!=getf(pt(i,j)))
					f[getf(pt(i-1,j-1))]=getf(pt(i,j));
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<m;j++){
			if(getf(pt(i,j))!=getf(pt(i-1,j+1))){
				ed[getf(pt(i-1,j+1))].push_back(getf(pt(i,j)));
				in[getf(pt(i,j))]++;
			}
		}
	}
	q.push(getf(0));num[getf(0)]=1;
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int i=0;i<ed[u].size();i++){
			int v=ed[u][i];
			num[v]+=num[u];
			num[v]%=mod;
			in[v]--;
			if(!in[v])q.push(v);
		}
	}
	cout<<num[getf(pt(n,0))]*qpow(2,uacnum)%mod;
	return;
}
#ifdef int
signed main()
#else
int main()
#endif
{
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		work();
	}
    return 0;
}



/* 
5 8
------##
-##--#--
---#----
-----##-
#-------

3 3
--#
##-
---
*/


P2046 [NOI2010] 海拔

题解
题解

注意到,我们只会找到一个分界线,左上角全为 11,右下角全为 00,这样显然最优。

证明:

  1. 考虑在 0/1 之中有其他数:海拔差比 0/1 的差大,绝对不优。
  2. 考虑在 0 之中有一个 1,上下左右都有贡献,显然不优
  3. 数据范围。。。

于是就转化成了求原图的最小割,最大流以获得 80pts80pts。

建对偶图跑最短路即可通过本题。

std
代码
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
#define int ll
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
int n,m;
struct node{
	int u,dis;
	friend bool operator <(node x,node y){
		return x.dis>y.dis;
	}
};
vector<node>ed[4100005];
int pt(int x,int y){
	if(x==m)return (n+1)*(m-1)+3;
	if(y==0)return (n+1)*(m-1)+3;
	if(x==0)return 0;
	if(y==m)return 0;
	return (x-1)*n+(y);
}
int dis[4100005],vis[4100005];
priority_queue<node>q;
void add_edge(int u,int v,int w){
	ed[u].push_back({v,w});
}
void work(){
	/*fill your code here*/
	cin>>n;m=n+1;
	for(int i=1;i<=n+1;i++){
		for(int j=1;j<=n;j++){
			int u=pt(i-1,j),v=pt(i,j),w;
			cin>>w;
			add_edge(u,v,w);
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n+1;j++){
			int u=pt(i,j-1),v=pt(i,j),w;
			cin>>w;
			add_edge(v,u,w);
		}
	}
	for(int i=1;i<=n+1;i++){
		for(int j=1;j<=n;j++){
			int u=pt(i-1,j),v=pt(i,j),w;
			cin>>w;
			add_edge(v,u,w);
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n+1;j++){
			int u=pt(i,j-1),v=pt(i,j),w;
			cin>>w;
			add_edge(u,v,w);
		}
	}
	q.push({0,0});
	while(!q.empty()){
		node nd=q.top();q.pop();
//		printf("%d %d %d\n",nd.u,nd.dis,ed[nd.u].size());
		if(vis[nd.u])continue;
		dis[nd.u]=nd.dis;
		vis[nd.u]=1;
		for(int i=0;i<ed[nd.u].size();i++){
			int v=ed[nd.u][i].u,dis=nd.dis+ed[nd.u][i].dis;
			if(!vis[v]){
				q.push({v,dis});
			}
		}
	}
	cout<<dis[pt(n+1,0)];
	return;
}
#ifdef int
signed main()
#else
int main()
#endif
{
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		work();
	}
    return 0;
}

P7916 [CSP-S 2021] 交通规划

题解
题解

考虑直接跑最小割,我们只用给原点连所有白点,所有黑点连汇点(反之也可以)即可。

但这样怎么使用对偶图优化呢?

我们可以把所有连续的白点(或黑点)连成一个块,这个块两边的节点一个作为最短路的起点,一个作为最短路的终点,对于所有的块,我们将起点和终点进行配对即可。

怎么配对呢?我们只需要跑一遍二分图最大权完美匹配即可。

std
代码
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
#define int ll
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
int ddd[505][505];
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

namespace Flow{
	#define int ll
	typedef long long ll;
	const int inf=0x0f0f0f0f0f0f0f0f;
		
	struct node{
		int u,dis,num,val;
	};
	vector<node> ed[2005];
	int s,t,fl[2005],dis[2005],pre[2005],cost,inq[2005],ans;
	queue<int>q;
	bool bfs(){
		memset(fl,0,sizeof(fl));
		memset(inq,0,sizeof(inq));
		memset(pre,-1,sizeof(pre));
		memset(dis,0x0f,sizeof(dis));
		while(!q.empty())q.pop();
		q.push(s);
		inq[s]=1;dis[s]=0;
		fl[s]=inf;
		while(!q.empty()){
			int x=q.front();q.pop();
			inq[x]=0;
			for(int i=0;i<ed[x].size();i++){
				node v=ed[x][i];
				if(v.dis<=0)continue;
				if(dis[x]+v.val<dis[v.u]){
					dis[v.u]=dis[x]+v.val;
					int k=min(fl[x],v.dis);
					fl[v.u]=k;
					pre[v.u]=ed[x][i].num;
					if(!inq[v.u]){
						q.push(v.u);
						inq[v.u]=1;
					}
				}
			}
		}
		return pre[t]>=0;
	}
	void flow(){
		ans+=fl[t];
		cost+=fl[t]*dis[t];
		for(int i=t;;){
			if(i==s)break;
			node v=ed[i][pre[i]];
			ed[i][pre[i]].dis+=fl[t];
			ed[v.u][v.num].dis-=fl[t];
			i=ed[i][pre[i]].u;
		}
	}
	void add_edge(int u,int v,int w,int val){
		int s1=ed[u].size();
		int s2=ed[v].size();
		ed[u].push_back({v,w,s2,val});
		ed[v].push_back({u,0,s1,-val});
	}
	int fll(int S,int T){
		cost=0;ans=0;s=S;t=T;
		while(bfs()){
			flow();
		}
		return cost;
	}
	void clear(){
		for(int i=0;i<2005;i++)ed[i].clear();
	}
}

int n,m,a[20005];
struct node{
	int u,dis;
	friend bool operator <(node x,node y){
		return x.dis>y.dis;
	}
};
vector<node>ed[3100005];
int pt(int x,int y){
	return x*(m+1)+(y);
}
int dis[3100005],vis[3100005],bk[3100005],a1[505][505],a2[505][505];
priority_queue<node>q;
void add_edge(int u,int v,int w){
	ed[u].push_back({v,w});
}
int getu(int t){
		if(t<=m){
			int u=pt(0,t-1);
			int v=pt(0,t);
			return u;
		}
		else if(t<=n+m){
			t-=m;
			int u=pt(t-1,m);
			int v=pt(t,m);
			return u;
		}
		else if(t<=n+m+m){
			t-=m+n;
			t=m-t+1;
			int u=pt(n,t);
			int v=pt(n,t-1);
			return u;
		}
		else{
			t-=m+n+m;
			t=n-t+1;
			int u=pt(t,0);
			int v=pt(t-1,0);
			return u;
		}
	
}
int getv(int t){
		if(t<=m){
			int u=pt(0,t-1);
			int v=pt(0,t);
			return v;
		}
		else if(t<=n+m){
			t-=m;
			int u=pt(t-1,m);
			int v=pt(t,m);
			return v;
		}
		else if(t<=n+m+m){
			t-=m+n;
			t=m-t+1;
			int u=pt(n,t);
			int v=pt(n,t-1);
			return v;
		}
		else{
			t-=m+n+m;
			t=n-t+1;
			int u=pt(t,0);
			int v=pt(t-1,0);
			return v;
		}
	
}
void addd(int k,int t){
	int u=getu(t);
	int v=getv(t);
	add_edge(u,v,k);
	add_edge(v,u,k);
}
void work(){
	/*fill your code here*/
	
	vector<int>vt,vt2;
	memset(a,0,sizeof(a));
	memset(bk,0,sizeof(bk));
	int nn;
	cin>>nn;
	while(nn--){
		int k,p,t;
		cin>>k>>t>>p;
		a[t]=p+1;
		addd(k,t);
	}
	for(int t=1;t<=n*2+m*2;t++){
		if(a[t]==0){
			addd(0,t);
			a[t]=a[t-1];
		}
	}
	a[0]=a[n*2+m*2];
	a[n*2+m*2+1]=a[1];
	for(int t=1;t<=n*2+m*2;t++){
		if(a[t]==0){
			a[t]=a[t-1];
		}
		else break;
	}
	a[0]=a[n*2+m*2];
	a[n*2+m*2+1]=a[1];
	for(int i=1;i<=n*2+m*2;i++){
		if(a[i]==1&&a[i-1]!=1){
//			if(getu(i)!=getv(i-1))puts("err");
//			if(vt.size())
//			if(vt.back()==getu(i)){
//				printf("err: 1 %d\n",i);
//			}
			vt.push_back(getu(i));
//		printf("u:%d %d\n",i,getu(i));
			
		}
		if(a[i]==1&&a[i+1]!=1){
//			if(vt.size())
//			if(vt.back()==getv(i)){
//				printf("err: 2 %d\n",i);
//			}
//			if(getv(i)!=getu(i+1))puts("err");
			vt2.push_back(getv(i));
//		printf("v:%d %d\n",i,getv(i));
		}
	}
//	if(vt.size()&1){
//		puts("ERR");
//	}
//	sort(vt.begin(),vt.end());
//	vt.erase(unique(vt.begin(),vt.end()),vt.end());
//	for(int i=0;i<vt.size();i++){
//		bk[vt[i]]=i;
//	}
//	puts("");
//	cout<<vt.size()<<" "<<vt2.size()<<endl;
//	debug(vt);
//	debug(vt2);
	Flow::clear();
	for(int i=0;i<vt.size();i++){
		memset(dis,0x0f,sizeof(dis));
		memset(vis,0,sizeof(vis));
		while(!q.empty())q.pop();
		q.push({vt[i],0});
		while(!q.empty()){
			node nd=q.top();q.pop();
	//		printf("%d %d %d\n",nd.u,nd.dis,ed[nd.u].size());
			if(vis[nd.u])continue;
			dis[nd.u]=nd.dis;
			vis[nd.u]=1;
			for(int i=0;i<ed[nd.u].size();i++){
				int v=ed[nd.u][i].u,dis=nd.dis+ed[nd.u][i].dis;
				if(!vis[v]){
					q.push({v,dis});
				}
			}
		}
		for(int j=0;j<vt2.size();j++){
//				printf("%d->%d %lld\n",i,j,dis[vt[j]]);
//				if(vis[vt[j]]){
				Flow::add_edge(i+1,j+vt2.size()+1,1,dis[vt2[j]]);
				ddd[i][j]=dis[vt2[j]];
//				}
		}
	}
	/*
	for(int i=0;i<vt2.size();i++){
		memset(dis,0x0f,sizeof(dis));
		memset(vis,0,sizeof(vis));
		while(!q.empty())q.pop();
		q.push({vt2[i],0});
		while(!q.empty()){
			node nd=q.top();q.pop();
	//		printf("%d %d %d\n",nd.u,nd.dis,ed[nd.u].size());
			if(vis[nd.u])continue;
			dis[nd.u]=nd.dis;
			vis[nd.u]=1;
			for(int i=0;i<ed[nd.u].size();i++){
				int v=ed[nd.u][i].u,dis=nd.dis+ed[nd.u][i].dis;
				if(!vis[v]){
					q.push({v,dis});
				}
			}
		}
		for(int j=0;j<vt.size();j++){
			if(j!=i){
//				printf("%d->%d %lld\n",i,j,dis[vt[j]]);
//				if(vis[vt[j]]){
				Flow::add_edge(i+1,j+vt.size()+1,1,dis[vt[j]]);
				ddd[i][j]=dis[vt[j]];
//				}
			}
		}
	}
	*/
//	for(int i=0;i<vt.size();i++){
//		for(int j=0;j<vt.size();j++)cout<<ddd[i][j]<<" ";
//		puts("");
//	}
		for(int j=0;j<vt.size();j++){
			Flow::add_edge(0,j+1,1,0);
		}
		for(int j=0;j<vt2.size();j++){
			Flow::add_edge(j+vt2.size()+1,vt.size()+vt2.size()+2,1,0);
		}
	
	cout<<Flow::fll(0,vt.size()+vt2.size()+2)<<endl;
	for(int i=1;i<=n*2+m*2;i++){
		ed[getu(i)].clear();
		ed[getv(i)].clear();
	}
	for(int i=1;i<=n-1;i++){
		for(int j=0;j<m;j++){
			int u=pt(i,j),v=pt(i,j+1),w;
			w=a1[i][j];
			if(j==0)
			add_edge(u,v,w);
			if(j+1==m)
			add_edge(v,u,w);
		}
	}
	for(int i=0;i<n;i++){
		for(int j=1;j<m;j++){
			int u=pt(i,j),v=pt(i+1,j),w;
			w=a2[i][j];
			if(i==0)
			add_edge(u,v,w);
			if(i+1==n)
			add_edge(v,u,w);
		}
	}
	return;
}
#ifdef int
signed main()
#else
int main()
#endif
{
//	freopen("traffic3.in","r",stdin);
	int T=1;
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>T;
	for(int i=1;i<=n-1;i++){
		for(int j=0;j<m;j++){
			int u=pt(i,j),v=pt(i,j+1),w;
			cin>>w;
			a1[i][j]=w;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=1;j<m;j++){
			int u=pt(i,j),v=pt(i+1,j),w;
			cin>>w;
			a2[i][j]=w;
		}
	}
	for(int i=1;i<=n-1;i++){
		for(int j=0;j<m;j++){
			int u=pt(i,j),v=pt(i,j+1),w;
			w=a1[i][j];
			add_edge(u,v,w);
			add_edge(v,u,w);
		}
	}
	for(int i=0;i<n;i++){
		for(int j=1;j<m;j++){
			int u=pt(i,j),v=pt(i+1,j),w;
			w=a2[i][j];
			add_edge(u,v,w);
			add_edge(v,u,w);
		}
	}
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		work();
	}
    return 0;
}

Dilworth 定理

理论

一个偏序集中的最长反链大小,等于其中最小不可重链覆盖大小。

推论:一个 DAG 中最长反链的大小,等于其中最小可重链覆盖大小。

注释:这里的反链指的是一个点集 AA,满足 ∀a,b∈Aa无法到达b\forall_{a,b\in A} a 无法到达 b

例题

P4298 [CTSC2008] 祭祀

题解
题解

祭祀地点的集合就是反链的定义。于是转化为了 DAG 上的最小可重链覆盖了。

但这不太好做。于是我们考虑跑一遍传递闭包,将其转化为偏序集上的不可重链覆盖,然后就是这题了。

std
std

CF1630F Making It Bipartite

题解
题解

由于倍数关系的传递性,不存在三个数 a,b,c∈ansa,b,c\in ans 使得 a∣ba|b 且 b∣cb|c。(不然 a→b→c→aa\to b\to c\to a 是一个奇环)于是,我们令大的点统一指向小的点,每个点都要么只有出边,要么只有入边。我们考虑将每个点拆成只有出边或只有入边的两个点,连一些边表示不能一起选的信息之后跑最大独立集即可。

最大独立集怎么求呢?由 Dilworth 定理,以及原图是偏序集,最大独立集等于最小链覆盖,直接做即可。

std
std
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int mod=998244353;
const int inf=0x3f3f3f3f3f3f3f3f;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)


struct node{
	int u,dis,num;
};
vector<node> ed[200005];
int n,m,s,t,mk[200005],now[200005],cost,ans;
queue<int>q;
bool bfs(){
	for(int i=0;i<=t;i++){
		mk[i]=-1;
	}
	queue<node>q;
	q.push({s,0,0});
	while(!q.empty()){
		node x=q.front();q.pop();
		if(mk[x.u]>=0)continue;
		if(x.u==s&&x.dis>0)continue;
		mk[x.u]=x.dis;
		if(x.u==t)break;
		for(int i=0;i<ed[x.u].size();i++){
			node v=ed[x.u][i];
			if(v.dis)q.push({v.u,x.dis+1,0});
		}
	}
	return mk[t]>-1;
}
int dfs(int x,int fl){
	if(x==t){
		return fl;
	}
	int ret=0;
	for(int i=now[x];i<ed[x].size();i++){
		now[x]=i;
		node v=ed[x][i];
		if(!v.dis)continue;
		if(mk[v.u]!=mk[x]+1)continue;
		int k=dfs(v.u,min(fl,v.dis));
		ed[x][i].dis-=k;
		ed[v.u][v.num].dis+=k;
		fl-=k;
		ret+=k;
		if(!fl)break;
	}
	return ret;
}
void add_edge(int u,int v,int w){
	int s1=ed[u].size();
	int s2=ed[v].size();
	ed[u].push_back({v,w,s2});
	ed[v].push_back({u,0,s1});
}
void aded(int u,int v){
	add_edge(u,v+n*2,1);
}
void add(int u,int v){
	aded(u,v);
	aded(u+n,v+n);
	aded(u,v+n);
}
int vis[200005],a[200005];
void work(){
	/*fill your code here*/
	
	cin>>n;s=0;t=n*4+1;
	for(int i=s;i<=t;i++){
		ed[i].clear();
	}
	cost=0;ans=0;
	for(int i=1;i<=n*2;i++){
		add_edge(0,i,1);
//		add_edge(0,i+n*2,1);
	}
	for(int i=1;i<=n*2;i++){
		add_edge(i+n*2,t,1);
	}
	for(int i=1;i<=n;i++)
		aded(i,i+n);
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+1+n);
	if(n<=300){
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				if(a[j]%a[i]==0){
					add(j,i);
				}
			}
			
		}
	}
	else{
		for(int i=1;i<=n;i++)vis[a[i]]=i;
		for(int i=1;i<=n;i++){
			for(int j=2;j*a[i]<=5e4;j++){
				if(vis[j*a[i]]){
					int v=vis[j*a[i]];
					add(v,i);
				}
			}
		}
		for(int i=1;i<=n;i++)vis[a[i]]=0;
	}
	int cnt=0;
	while(bfs()){
		for(int i=0;i<=t;i++)now[i]=0;
		cost+=dfs(s,inf);
	}
//	for(int i=1;i<=n;i++){
//		if(!mark[i]){
//			int j=i;
//			while(j){
//				printf("%d ",j);
//				j=last_flow[j];
//			}
//			puts("");
//		}
//	}
	printf("%lld\n",cost-n);
	return;
}

#ifdef int
signed main()
#else
int main()
#endif
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int T=1;
	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		work();
	}
    return 0;
}



/* Code written by
		gala_xk (MarchWind)*/






原文