G - Typical Path Problem Editorial by en_translator
This problem asks to determine if there is a pair of paths from vertex \(B\) to vertex \(A\) and from vertex \(B\) to vertex \(C\) that does not share a vertex but vertex \(B\). This problem can be solved as a network flow problem as follows.
Consider constructing the following directed graph \(G'\) with \((2N+2)\) vertices and \((N+2M+3)\) edges based on \(G\).
- The vertices are \(s\), \(t\), \(x_1,x_2,\ldots,x_N\), and \(y_1,y_2,\ldots,y_N\).
- For each \(i=1,2,\ldots,N\), add an edge of capacity \(1\) from \(x_i\) to \(y_i\).
- For each \(i=1,2,\ldots,M\), add an edge of capacity \(1\) from \(y_{U_i}\) to \(x_{V_i}\) and from \(y_{V_i}\) to \(x_{U_i}\).
- Add an edge of capacity \(2\) from \(s\) to \(y_B\).
- Add an edge of capacity \(1\) from \(y_A\) to \(t\) and \(y_C\) to \(t\).
Then, the maximum flow from \(s\) to \(t\) is \(2\) if and only if there is a path satisfying the conditions in the problem statement.
That is, if \(G\) has a conforming path
\(A\to u_1\to\cdots\to u_k\to B\to v_1\to\cdots\to v_{k'}\to C\), then there is a flow along two paths on \(G'\),
\(s\to y_B\to x_{u_k}\to y_{u_k}\to x_{u_{k-1}}\to \cdots \to y_{u_1}\to x_A\to y_A\to t\) and
\(s\to y_B\to x_{v_1}\to y_{v_1}\to x_{v_2}\to \cdots \to y_{v_{k'}}\to x_C\to y_C\to t\).
By definition of \(G'\), if the former exists, then the latter path can always be taken.
On the other hand, if the maximum flow from \(s\) to \(t\) on \(G'\) is \(2\), then it can be split into two flows of amount \(1\) by following the path from \(y_A\) and \(y_C\), as the capacity of the edges other than \(s\to y_B\) is \(1\). Here, these two paths do not share a vertex except for \(s\), \(y_B\), and \(t\), because the sum of capacities of the outgoing edges from vertex \(x_i\), and the sum of the capacities of the incoming edges to vertex \(y_i\) (\(i \neq B\)) are \(0\), which disallows two or more flows on the vertex. By the form of \(G'\), the paths are in the form of \(s\to y_B\to x_{i_1}\to y_{i_1}\to x_{i_2}\to\cdots\to y_{i_k}\to x_A\to y_A\to t\) and \(s\to y_B\to x_{j_1}\to y_{j_1}\to x_{j_2}\to\cdots\to y_{j_{k'}}\to x_C\to y_C\to t\), from which we can construct a conforming path on \(G\).
Therefore, the problem has been boiled down to a network flow problem. With an appropriate algorithm like Dinic’s algorithm, it can be solved in a total of \(O(N+M)\) time, since the maximum flow is bounded by a constant \((2)\). Therefore, the problem has been solved fast enough.
Sample code in C++:
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
int main(void){
int n,m,a,b,c,x,y;
cin>>n>>m;
mf_graph<int> g(2*n+2);
cin>>a>>b>>c;
g.add_edge(0, 2*b, 2);
g.add_edge(2*a, 2*n+1, 1);
g.add_edge(2*c, 2*n+1, 1);
for(int i=0;i<n;i++)g.add_edge(2*i+1, 2*i+2, 1);
for(int i=0;i<m;i++){
cin>>x>>y;
g.add_edge(2*x, 2*y-1, 1);
g.add_edge(2*y, 2*x-1, 1);
}
if(g.flow(0,2*n+1,2)==2)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
posted:
last update: