跳到正文

个人记录

【学习笔记】图论

WilliamAlbert约 33 分钟
原文
文章信息与版本

文章信息

收录
2026年2月27日
更新
2026年2月27日
篇幅
约 33 分钟

版本

  1. v2当前
  2. v1

最短路

Johnson 全源最短路

求两两之间的最短路,有负权。1≤n≤3000,1≤m≤60001 \le n \le 3000, 1 \le m \le 6000。

用 floyd 是可以 O(n3)O(n^3) 的。

首先建立一个超级源点向所有点连边,边权 00。然后跑一遍 spfa。此时对于一条边 u→vu \rightarrow v,我们有 distv≤distu+wdist_v \le dist_u + w,所以新建一图 G′G',EG=EG′E_G = E_{G'},但对于每条边,边权改为 distu+w−distvdist_u + w - dist_v。我们再对于每个点跑一遍 dij 单源最短路。我们发现此时 ii 到 jj 的最短路就是 disti,j′+distj−distidist'_{i, j} + dist_j - dist_i,证明方式类似裂项,时间复杂度 O(nm+nmlog⁡2n)O(nm + nm\log_2n)。

网络流

最大流

Max-Flow
C++
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 1e4 + 5, M = 1e5 + 5, INF = 2e9;
int h[N], e[M * 2], ne[M * 2], f[M * 2], idx;
int n, m, S, T;
void add(int a, int b, int c) {
	e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
	e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
int d[N], cur[N];
bool bfs() {
	For(i, 0, n + 1) d[i] = -1, cur[i] = -1;
	queue<int> q;
	q.push(S);
	d[S] = 0;
	cur[S] = h[S];
	while (q.size()) {
		int t = q.front();
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (f[i] && d[j] == -1) {
				d[j] = d[t] + 1;
				cur[j] = h[j];
				if (j == T) return 1;
				q.push(j);
			}
		}
	}
	return 0;
}
int find(int x, int limit) {
	if (x == T) return limit;
	int flow = 0;
	for (int i = cur[x]; (~i) && flow < limit; i = ne[i]) {
		cur[x] = i;
		int j = e[i];
		if (f[i] && d[j] == d[x] + 1) {
			int t = find(j, min(limit - flow, f[i]));
			if (!t) d[j] = -1;
			flow += t;
			f[i] -= t, f[i ^ 1] += t;
		}
	}
	return flow;
}
int dinic() {
	int flow = 0, t = 0;
	while (bfs()) {
		while (flow = find(S, INF)) t += flow;
	}
	return t;
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	memset(h, -1, sizeof(h));
	idx = 0;
	cin >> n >> m >> S >> T;
	For(i, 1, m) {
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
	}
	cout << dinic() << '\n';
	return 0;
}

最小割

最大流 = 最小割

费用流

EK 费用流

MaxCostFlow
C++
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 5005, M = 50005, INF = 2e9;
int h[N], e[M * 2], ne[M * 2], f[M * 2], w[M * 2], idx;
int n, m, S, T;
void add(int a, int b, int c, int d) {
	e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx++;
	e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx++;
}
int pre[N], incf[N], dist[N];
bool vis[N];
bool spfa() {
	For(i, 0, n + 1) incf[i] = 0, dist[i] = INF, vis[i] = 0;
	queue<int> q;
	q.push(S);
	vis[S] = 1;
	incf[S] = INF, dist[S] = 0;
	while (q.size()) {
		int t = q.front();
		q.pop();
		vis[t] = 0;
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (f[i] && dist[j] > dist[t] + w[i]) {
				dist[j] = dist[t] + w[i];
				pre[j] = i;
				incf[j] = min(incf[t], f[i]);
				if (!vis[j]) {
					vis[j] = 1;
					q.push(j);
				}
			}
		}
	}
	return incf[T] > 0;
}
void EK(int &flow, int &cost) {
	while (spfa()) {
		flow += incf[T], cost += incf[T] * dist[T];
		for (int i = T; i != S; i = e[pre[i] ^ 1]) {
			f[pre[i]] -= incf[T];
			f[pre[i] ^ 1] += incf[T];
		}
	}
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	memset(h, -1, sizeof(h));
	idx = 0;
	cin >> n >> m >> S >> T;
	For(i, 1, m) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		add(a, b, c, d);
	}
	int flow = 0, cost = 0;
	EK(flow, cost);
	cout << flow << ' ' << cost;
	return 0;
}

Prime-Dual 费用流(原始对偶)

考虑先求一遍 spfa,源点为 SS,套用 Johnson 全源最短路的方法,将 i→ji \rightarrow j 边权改为 di+w−djd_i + w - d_j,这样就没有负权了。每次寻找增广路的时候跑 dij,并更新 di=di+di′d_i = d_i + d'_i 即可,效率显著提高。

Prime-Dual
C++
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 5e3 + 5, M = 50005, INF = 1e8;
int n, m, S, T;
int h[N], e[M * 2], ne[M * 2], f[M * 2], w[M * 2], idx;
void add(int a, int b, int c, int d) {
	e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx++;
	e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx++;
}
int d[N];
bool vis[N];
void spfa() {
	For(i, 1, n) d[i] = INF;
	queue<int> q;
	q.push(S);
	vis[S] = 1, d[S] = 0;
	while (q.size()) {
		int t = q.front();
		q.pop();
		vis[t] = 0;
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (f[i] && d[j] > d[t] + w[i]) {
				d[j] = d[t] + w[i];
				if (!vis[j]) vis[j] = 1, q.push(j);
			}
		}
	}
}
int dist[N], pre[N], incf[N];
bool dijkstra() {
	For(i, 1, n) vis[i] = 0, dist[i] = INF, incf[i] = 0;
	priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
	q.push(mp(0, S));
	incf[S] = INF;
	dist[S] = 0;
	while (q.size()) {
		auto t = q.top();
		q.pop();
		if (vis[t.y]) continue;
		vis[t.y] = 1;
		for (int i = h[t.y]; ~i; i = ne[i]) {
		    if (vis[e[i]]) continue;
			int j = e[i], W = w[i] + d[t.y] - d[j];
			if (f[i] && dist[j] > dist[t.y] + W) {
				dist[j] = dist[t.y] + W;
				pre[j] = i;
				incf[j] = min(incf[t.y], f[i]);
				q.push(mp(dist[j], j));
			}
		}
	}
	return incf[T];
}
void EK(int &flow, int &cost) {
	spfa();
	while (dijkstra()) {
		For(i, 1, n) d[i] = min(INF, d[i] + dist[i]);
		for (int i = T; i != S; i = e[pre[i] ^ 1]) {
			f[pre[i]] -= incf[T];
			f[pre[i] ^ 1] += incf[T];
		}
		flow += incf[T], cost += incf[T] * d[T];
	}
}
signed main() {
	//assert(freopen(".in", "r", stdin));
	//assert(freopen(".out", "w", stdout));
	cin.tie(nullptr)->sync_with_stdio(false);
	memset(h, -1, sizeof(h));
	cin >> n >> m >> S >> T;
	For(i, 1, m) {
		int u, v, c, w;
		cin >> u >> v >> c >> w;
		add(u, v, c, w);
	}
	int flow = 0, cost = 0;
	EK(flow, cost);
	cout << flow << ' ' << cost;
	return 0;
}

Dinic 费用流(zkw 费用流)

就是每一次跑 spfa,然后 dfs 找到所有增广路。

Dinic

咕噜咕噜

上下界网络流

无源汇上下界可行流

给定一个无源汇网络,每个边都有流量下界和上界,输出一种方案,若无输出 -1。

考虑将下界删掉,对于 u→vu \rightarrow v 的一条容量 [l,r][l, r] 的边,强制让这条边先流 ll 的流量。但因为要保证流量守恒,我们新建一个超级源点和汇点 S,TS, T,先对于每个点统计 ini,outiin_i, out_i 分别表示强制流入 iniin_i 的流量,强制流出了 outiout_i 的流量,为了守恒,若 ini≥outiin_i \ge out_i,就 S→iS \rightarrow i 容量为 ini−outiin_i - out_i,否则 i→Ti \rightarrow T 容量为 outi−iniout_i - in_i。跑一遍从 S→TS \rightarrow T 的最大流即可。存在方案当且仅当 i→Ti \rightarrow T 的边都是满流的,并且可以证明 S→iS \rightarrow i 的总容量等于 i→Ti \rightarrow T 的总容量。

输出方案就是对于此时每条边加上 lil_i 即可。

无源汇上下界可行流
C++
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 205, M = 10205 + N, INF = 2e9;
int d[N], cur[N];
int n, m, S, T;
int sum[N];
int h[N], e[M * 2], ne[M * 2], f[M * 2], l[M * 2], idx;
void add(int a, int b, int c, int d) {
	e[idx] = b, f[idx] = d - c, l[idx] = c, ne[idx] = h[a], h[a] = idx++;
	e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
bool bfs() {
	For(i, 0, n + 1) d[i] = -1;
	queue<int> q;
	q.push(S);
	d[S] = 0, cur[S] = h[S];
	while (q.size()) {
		int t = q.front();
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (d[j] == -1 && f[i]) {
				d[j] = d[t] + 1;
				cur[j] = h[j];
				if (j == T) return 1;
				q.push(j);
			}
		}
	}
	return 0;
}
int find(int x, int limit) {
	if (x == T) return limit;
	int flow = 0;
	for (int i = cur[x]; ~i && flow < limit; i = ne[i]) {
		int j = e[i];
		cur[x] = i;
		if (d[j] == d[x] + 1 && f[i]) {
			int t = find(j, min(limit - flow, f[i]));
			if (t == 0) d[j] = -1;
			f[i] -= t, f[i ^ 1] += t, flow += t;
		}
	}
	return flow;
}
int dinic() {
	int flow = 0, t = 0;
	while (bfs()) while (flow = find(S, INF)) t += flow;
	return t;
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	memset(h, -1, sizeof(h));
	cin >> n >> m;
	For(i, 1, m) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		add(a, b, c, d);
		sum[a] -= c;
		sum[b] += c;
	}
	S = 0, T = n + 1;
	int tot = 0;
	For(i, 1, n) {
		if (sum[i] >= 0) add(S, i, 0, sum[i]), tot += sum[i];
		else add(i, T, 0, -sum[i]);
	}
	if (dinic() != tot) cout << "NO";
	else {
		cout << "YES\n";
		for (int i = 0; i < 2 * m; i += 2) cout << f[i ^ 1] + l[i] << '\n';
	}
	return 0;
}

有源汇上下界最大/最小流

和无源汇的一样,只是此时要新添一条 t→st \rightarrow s 容量 +∞+\infty 的边,称之为附加边。

最大流

先从 S→TS \rightarrow T 跑一遍最大流,保证流量守恒,并设 flowflow 为此时附加边的流量。然后删去附加边,在残留网络上跑 s→ts \rightarrow t 的最大流 flow′flow',那么 Maxflow=flow+flow′\text{Maxflow} = flow + flow'。

当然还有一种比较简单巧妙的方法,第一次跑完后不删边,而是直接跑 s→ts \rightarrow t 的最大流 flowflow,那么 Maxflow=flow\text{Maxflow} = flow。这是因为附加边会退流,退的流量就是上面的 flow′flow'。

有源汇上下界最大流
C++
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 205, M = 10005 + N, INF = 2e9;
int h[N], e[M * 2], ne[M * 2], f[M * 2], idx;
int n, m, s, t, S, T;
int d[N], cur[N];
int sum[N], tot;
void add(int a, int b, int c) {
	e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
	e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
bool bfs() {
	For(i, 0, n + 1) d[i] = -1;
	queue<int> q;
	d[S] = 0, cur[S] = h[S];
	q.push(S);
	while (q.size()) {
		int t = q.front();
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (d[j] == -1 && f[i]) {
				d[j] = d[t] + 1;
				cur[j] = h[j];
				if (j == T) return 1;
				q.push(j);
			}
		}
	}
	return 0;
}
int find(int x, int limit) {
	if (x == T) return limit;
	int flow = 0;
	for (int i = cur[x]; ~i && flow < limit; i = ne[i]) {
		int j = e[i];
		cur[x] = i;
		if (d[j] == d[x] + 1 && f[i]) {
			int t = find(j, min(limit - flow, f[i]));
			if (t == 0) d[j] = -1;
			f[i] -= t, f[i ^ 1] += t, flow += t;
		}
	}
	return flow;
}
int dinic() {
	int t = 0, flow = 0;
	while (bfs()) while (flow = find(S, INF)) t += flow;
	return t;
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	memset(h, -1, sizeof(h));
	cin >> n >> m >> s >> t;
	For(i, 1, m) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		add(a, b, d - c);
		sum[a] -= c;
		sum[b] += c;
	}
	S = 0, T = n + 1;
	For(i, 1, n) {
		if (sum[i] >= 0) add(S, i, sum[i]), tot += sum[i];
		else add(i, T, -sum[i]);
	}
	add(t, s, INF);
	if (dinic() != tot) cout << "No Solution";
	else {
		int res = f[idx - 1];
		f[idx - 1] = f[idx - 2] = 0;
		S = s, T = t;
		res += dinic();
		cout << res << '\n';
	}
	return 0;
}
最小流

同最大流一样,但第二次变成求从 t→st \rightarrow s 的最大流,并且 Minflow=flow−flow′\text{Minflow} = flow - flow'。

并且注意,不能用上面第二种方式了。

有源汇上下界最小流
C++
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 5e4 + 10, M = 125000 + 10 + N + 10, INF = 2147483647;
int n, m, s, t;
int S, T;
int h[N], e[M * 2], ne[M * 2], f[M * 2], idx;
int sum[N];
void add(int a, int b, int c) {
	e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
	e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
int cur[N], d[N];
bool bfs() {
	For(i, 0, n + 1) d[i] = -1;
	queue<int> q;
	q.push(S);
	d[S] = 0, cur[S] = h[S];
	while (q.size()) {
		int t = q.front();
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (d[j] == -1 && f[i]) {
				d[j] = d[t] + 1;
				cur[j] = h[j];
				if (j == T) return 1;
				q.push(j);
			}
		}
	}
	return 0;
}
int find(int x, int limit) {
	if (x == T) return limit;
	int flow = 0;
	for (int i = cur[x]; ~i && flow < limit; i = ne[i]) {
		int j = e[i];
		cur[x] = i;
		if (d[x] + 1 == d[j] && f[i]) {
			int t = find(j, min(f[i], limit - flow));
			if (!t) d[j] = -1;
			flow += t, f[i] -= t, f[i ^ 1] += t;
		}
	}
	return flow;
}
int dinic() {
	int t = 0, flow = 0;
	while (bfs()) while (flow = find(S, INF)) t += flow;
	return t;
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	memset(h, -1, sizeof(h));
	cin >> n >> m >> s >> t;
	For(i, 1, m) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		add(a, b, d - c);
		sum[a] -= c;
		sum[b] += c;
	}
	S = 0, T = n + 1;
	int tot = 0;
	For(i, 1, n) {
		if (sum[i] >= 0) add(S, i, sum[i]), tot += sum[i];
		else add(i, T, -sum[i]);
	}
	add(t, s, INF);
	if (dinic() != tot) cout << "No Solution";
	else {
		int res = f[idx - 1];
		f[idx - 1] = f[idx - 2] = 0;
		S = t, T = s;
		cout << res - dinic();
	}
	return 0;
}

无源汇上下界最小/最大费用可行流

其实就是直接改成 spfa 就行。

有源汇上下界最小/最大费用可行流

这里就只用跑第一遍 S→TS \rightarrow T 的费用流就可以了,最终的费用也就是答案。

有源汇上下界最小/最大费用最大/最小流

最大流

考虑第一遍 S→TS \rightarrow T 跑最小/最大费用流,然后 s→ts \rightarrow t 跑最小/最大费用流,答案是两次加起来。

最小流

考虑第一遍 S→TS \rightarrow T 跑最小/最大费用流,然后 t→st \rightarrow s 跑最大/最小费用流,答案为第一次减第二次。

带负权的费用流

我们发现,若一个网络中存在负环,但并不意味着没有最小费用流。

考虑和上下界的处理方式一样,我们强制那些负权边 i→ji \rightarrow j 先流满,改变方向,权值变正,表示可以退流,当然还要建超级源点和汇点保证流量守恒。

带负权的费用流
C++
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 205, M = 1e4 + 5, K = M + N, INF = 2e9;
int n, m, s, t, S, T;
int in[N], out[N];
int h[N], e[K * 2], ne[K * 2], f[K * 2], w[K * 2], idx;
void add(int a, int b, int c, int d) {
	e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx++;
	e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx++;
}
int dist[N], pre[N], incf[N];
bool vis[N];
bool spfa() {
	memset(vis, 0, sizeof(vis));
	memset(dist, 0x3f, sizeof(dist));
	memset(incf, 0, sizeof(incf));
	incf[S] = INF;
	dist[S] = 0;
	queue<int> q;
	q.push(S);
	vis[S] = 1;
	while (q.size()) {
		int t = q.front();
		q.pop();
		vis[t] = 0;
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (f[i] && dist[j] > dist[t] + w[i]) {
				dist[j] = dist[t] + w[i];
				pre[j] = i;
				incf[j] = min(incf[t], f[i]);
				if (!vis[j]) q.push(j), vis[j] = 1;
			}
		}
	}
	return incf[T] > 0;
}
void EK(int &flow, int &cost) {
	while (spfa()) {
		flow += incf[T];
		cost += incf[T] * dist[T];
		for (int i = T; i != S; i = e[pre[i] ^ 1]) {
			f[pre[i]] -= incf[T];
			f[pre[i] ^ 1] += incf[T];
		}
	}
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	memset(h, -1, sizeof(h));
	cin >> n >> m >> s >> t;
	int ans = 0;
	For(i, 1, m) {
		int x, y, f, v;
		cin >> x >> y >> f >> v;
		if (v < 0) {
			in[y] += f, out[x] += f;
			ans += f * v;
			add(y, x, f, -v);
		} else add(x, y, f, v);
	}
	S = 0, T = n + 1;
	For(i, 1, n) {
		if (in[i] > out[i]) add(S, i, in[i] - out[i], 0);
		else add(i, T, out[i] - in[i], 0);
	}
	add(t, s, INF, 0);
	int flow1 = 0, cost1 = 0;
	EK(flow1, cost1);
	flow1 = f[idx - 1];
	f[idx - 1] = f[idx - 2] = 0;
	int flow2 = 0, cost2 = 0;
	S = s, T = t;
	EK(flow2, cost2);
	cout << flow1 + flow2 << ' ' << cost1 + cost2 + ans;
	return 0;
}

关于图论一些定理

Hall 定理

Hall 定理

给定一个二分图,设左边点集为 LL,右边为 RR,对于 x∈Lx \in L,定义 NxN_x 为其在 RR 中的对应点点集。对于左边的点,存在一个完美匹配,当前仅当对于任意点集 S⊆LS \subseteq L,定义 NS=⋃x∈SNxN_S = \bigcup_{x \in S}N_x,都有 ∣S∣≤∣NS∣|S| \le |N_S|。

推论 1

对于一个正则二分图(所有点的度数相同的二分图),一定存在完美匹配。

推论 2

对于一个二分图,其最大匹配为:

∣L∣−max⁡{∣S∣−∣NS∣}|L| - \max\{|S| - |N_S|\}

我们证明一下 推论 2,那么 Hall 定理 也就自然成立。

这里有两种方法。

Method 1(最大流最小割定理)

考虑对于一个集合 SS,我们建出一个图:

S⟶N↗L−S⟶R−N\begin{array}{ccccccc} S & \longrightarrow & N \\ & \nearrow & \\ L - S & \longrightarrow & R - N \end{array}

然后我们将其变为流:

flow:1+∞1___________________________________S⟶N↗↗↘SS⟶L−S⟶R−N⟶TT\begin{array}{ccccccc} flow: & 1 & & +\infty & & 1\\ \_\_\_\_\_ & \_\_\_\_\_ & \_\_\_\_\_ & \_\_\_\_\_ & \_\_\_\_\_ & \_\_\_\_\_ & \_\_\_\_\_ \\ & & & & & \\ & & S & \longrightarrow & N & & \\ & \nearrow & & \nearrow & & \searrow & \\ SS & \longrightarrow & L - S & \longrightarrow & R - N & \longrightarrow & TT \end{array}

那么最大匹配数就一定是这个图的最大流。根据 最大流最小割定理 ,我们得出这里的最小割要么为

原文