G - Typical Path Problem
题目大意
给定一张 N N N 个点、 M M M 条边的简单无向图 G G G 和三个整数 A , B , C A,B,C A,B,C。
是否存在一条从顶点 A A A 到 C C C,且经过 B B B 的简单路径?
数据范围:
- 3 ≤ N ≤ 2 × 1 0 5 3\le N\le 2\times 10^5 3≤N≤2×105
- N − 1 ≤ M ≤ min ( N ( N − 1 ) 2 , 2 × 1 0 5 ) N-1\le M\le \min(\frac{N(N-1)}2,2\times 10^5) N−1≤M≤min(2N(N−1),2×105)
- 1 ≤ A , B , C ≤ N 1\le A,B,C\le N 1≤A,B,C≤N( A , B , C A,B,C A,B,C 互不相同)
什么是 简单路径 ?
简单路径 是不重复经过同一个点的路径。例如, 1 → 2 → 3 1\to 2\to 3 1→2→3 是简单路径,但 1 → 2 → 1 1\to 2\to 1 1→2→1 不是简单路径。
解法1:最大流
不难发现,存在一条 A → B → C A\to B\to C A→B→C 的简单路径,当且仅当存在一条 B → A B\to A B→A 和一条 B → C B\to C B→C 的路径,使得这两条路径不经过同一个点( B B B 除外)。因此,我们可以构建网络流模型来解决此问题。
考虑由 ( 2 N + 2 ) (2N+2) (2N+2) 个点组成的有向图 G ′ G' G′:
- 源点: s s s
- 汇点: t t t
- G G G 中每个点对应的入点: x 1 , … , x N x_1,\dots,x_N x1,…,xN
- G G G 中每个点对应的出点: y 1 , … , y N y_1,\dots,y_N y1,…,yN
然后进行连边:
- 对于每个 1 ≤ i ≤ N 1\le i\le N 1≤i≤N,从入点 x i x_i xi 向出点 y i y_i yi 连接一条流量为 1 1 1 的边;
- 从源点 s s s 到中转点的入点 x B x_B xB 连接一条流量为 2 2 2 的边;
- 从 A A A 和 C C C 的出点 y A , y C y_A,y_C yA,yC 向汇点 t t t 分别连接一条流量为 1 1 1 的边;
- 最后, ∀ ( u , v ) ∈ E G \forall (u,v)\in E_G ∀(u,v)∈EG,连接 y u → x v y_u \to x_v yu→xv 和 y v → x u y_v \to x_u yv→xu,流量为 1 1 1。
计算 s s s 到 t t t 的最大流,如果最大流为 2 2 2 则必定有存在不经过同一个顶点的 B → A , B → C B\to A,B\to C B→A,B→C 的路径。
证明
显然,如果最大流为 2 2 2,必然通过了 y A y_A yA 和 y C y_C yC 向汇点连接的边,则一定分别有 B → A B\to A B→A 和 B → C B\to C B→C 的路径。
假设选择的这两条路径经过了同一顶点 v v v,则两流都必须经过 x v → y v x_v\to y_v xv→yv 这一条流量为 1 1 1 的边,此时最大流不可能超过 1 1 1。而最大流为 2 2 2,说明假设不成立,故没有经过同一顶点。
若使用 Dinic \text{Dinic} Dinic 算法,由于最大流不超过 2 2 2,网络流的时间复杂度为 O ( N + M ) \mathcal O(N+M) O(N+M)。
代码实现
在以下的两种实现中,我们规定
- 源点: s = 0 s=0 s=0
- 汇点: t = 2 n + 1 t=2n+1 t=2n+1
- i i i 的入点: x i = i x_i=i xi=i
- i i i 的出点: y i = n + i y_i=n+i yi=n+i
AC Library 实现
AtCoder Library 内置最大流的 Dinic \text{Dinic} Dinic 实现。
#include <cstdio>
#include <atcoder/maxflow>
using namespace std;
int main()
{
int n, m, a, b, c;
scanf("%d%d%d%d%d", &n, &m, &a, &b, &c);
int s = 0, t = (n << 1) + 1;
atcoder::mf_graph<int> G(t + 1);
G.add_edge(s, b + n, 2);
G.add_edge(a + n, t, 1);
G.add_edge(c + n, t, 1);
for(int i=1; i<=n; i++)
G.add_edge(i, i + n, 1);
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
G.add_edge(x + n, y, 1);
G.add_edge(y + n, x, 1);
}
puts(G.flow(s, t, 2) == 2? "Yes": "No");
return 0;
}
Dinic 手写实现
Dinic \text{Dinic} Dinic 算法对于此图的时间复杂度为 O ( N + M ) \mathcal O(N+M) O(N+M)。如果不清楚算法原理可以参考 OI Wiki。
关于空间分配问题
由于新图 G ′ G' G′ 包含 ( N + 2 M + 3 ) (N+2M+3) (N+2M+3) 条边,若使用静态链式前向星存图,数组大小需要开到 2 ( N + 2 M + 3 ) 2(N+2M+3) 2(N+2M+3),其理论最大值为 1.2 × 1 0 6 + 6 1.2\times 10^6+6 1.2×106+6。此处建议使用 1.25 × 1 0 6 1.25\times 10^6 1.25×106 大小的数组。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define maxn 400005
#define maxm 1250005
using namespace std;
int n, s, t, head[maxn], cur[maxn], dis[maxn],
cnt, w[maxm], to[maxm], nxt[maxm];
inline void add(int u, int v, int flow)
{
nxt[cnt] = head[u];
head[u] = cnt;
to[cnt] = v;
w[cnt++] = flow;
}
inline void add_flow(int u, int v, int f)
{
add(u, v, f);
add(v, u, 0);
}
inline bool bfs()
{
memset(dis, -1, sizeof(int) * n);
dis[s] = 0, cur[s] = head[s];
queue<int> q;
q.push(s);
while(!q.empty())
{
int v = q.front(); q.pop();
for(int i=head[v]; ~i; i=nxt[i])
if(w[i])
{
int u = to[i];
if(dis[u] == -1)
{
dis[u] = dis[v] + 1, cur[u] = head[u];
if(u == t) return true;
q.push(u);
}
}
}
return false;
}
int dfs(int v, int flow)
{
if(v == t) return flow;
int res = 0;
for(int i=cur[v]; ~i && flow; i=nxt[i])
{
cur[v] = i;
int u = to[i];
if(w[i] && dis[u] == dis[v] + 1)
{
int k = dfs(u, min(flow, w[i]));
w[i] -= k;
w[i ^ 1] += k;
flow -= k;
res += k;
}
}
return res;
}
int main()
{
int n, m, a, b, c;
scanf("%d%d%d%d%d", &n, &m, &a, &b, &c);
s = 0, t = (n << 1) + 1, ::n = t + 1;
memset(head, -1, sizeof(int) * ::n);
add_flow(s, b + n, 2);
add_flow(a + n, t, 1);
add_flow(c + n, t, 1);
for(int i=1; i<=n; i++)
add_flow(i, i + n, 1);
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
add_flow(x + n, y, 1);
add_flow(y + n, x, 1);
}
int mf = 0;
while(bfs()) mf += dfs(s, 2);
puts(mf == 2? "Yes": "No");
return 0;
}
解法2:圆方树
注意到以下算法的正确性:
- 找到 A → C A\to C A→C 的任意简单路径。对于经过的每一个点双连通分量,如果 B B B 在此点双内,则必然存在 A → B → C A\to B\to C A→B→C 的简单路径;如果 B B B 不属于任一经过的点双,则不可能存在 A → B → C A\to B\to C A→B→C 的简单路径。
因此,可以使用 Tarjan \text{Tarjan} Tarjan 算法构造原图的圆方树 T T T 来解决此问题。将上述算法转换到圆方树上如下:
- 在 T T T 上找到 A → C A\to C A→C 的唯一简单路径。对于经过的每一个方点,如果 B B B 是与其相邻的圆点,则必然存在 A → B → C A\to B\to C A→B→C 的简单路径;如果 B B B 不与任一经过的方点相邻,则不可能存在 A → B → C A\to B\to C A→B→C 的简单路径。
总时间复杂度为 O ( N + M ) \mathcal O(N+M) O(N+M),实际运行时间优于网络流解法。
代码实现
小贴士:圆方树相关的数组要开到两倍大小,不然会 RE 哦~
#include <cstdio>
#include <cstdlib>
#include <vector>
#define maxn 200005
using namespace std;
inline void setmin(int& x, int y)
{
if(y < x) x = y;
}
vector<int> G[maxn], T[maxn << 1];
inline void add_edge(vector<int>* G, int x, int y)
{
G[x].push_back(y);
G[y].push_back(x);
}
int dfc, dfn[maxn], low[maxn], top, st[maxn], cnt;
void tarjan(int v)
{
low[v] = dfn[v] = ++dfc;
st[++top] = v;
for(int u: G[v])
if(!dfn[u])
{
tarjan(u);
setmin(low[v], low[u]);
if(low[u] == dfn[v])
{
add_edge(T, v, ++cnt);
do add_edge(T, st[top], cnt);
while(st[top--] != u);
}
}
else setmin(low[v], dfn[u]);
}
int n, m, a, b, c, ct[maxn << 1];
void dfs(int v, int par)
{
if(v > n)
for(int u: T[v])
ct[u] ++;
if(v == c)
{
puts(ct[b]? "Yes": "No");
exit(0);
}
for(int u: T[v])
if(u != par)
dfs(u, v);
if(v > n)
for(int u: T[v])
ct[u] --;
}
int main()
{
scanf("%d%d%d%d%d", &n, &m, &a, &b, &c);
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
add_edge(G, x, y);
}
cnt = n;
tarjan(1);
dfs(a, -1);
return 0;
}
总结
三种解法的对比参见下表:
解法 | 代码长度 | 运行时间 | 内存占用 |
---|---|---|---|
最大流(AC Library)1 | 523 B 523~\mathrm{B} 523 B | 337 m s 337~\mathrm{ms} 337 ms | 106480 K B 106480~\mathrm{KB} 106480 KB |
最大流(Dinic)2 | 1650 B 1650~\mathrm{B} 1650 B | 334 m s 334~\mathrm{ms} 334 ms | 46980 K B 46980~\mathrm{KB} 46980 KB |
圆方树3 | 1142 B 1142~\mathrm{B} 1142 B | 162 m s 162~\mathrm{ms} 162 ms | 57824 K B 57824~\mathrm{KB} 57824 KB |
可见,圆方树算法的运行速度最快,最大流(AC Library)的代码最短,最大流(Dinic)的内存占用最小。
个人评价
这道题出得很好,题意简单而内涵丰富。
我赛时甚至没想到还可以网络流