#K60767. Bypass Existence Check
Bypass Existence Check
Bypass Existence Check
In this problem, you are given a directed graph representing intersections connected by one-way streets. You must determine whether there exists a bypass from a starting intersection \(A\) to a destination intersection \(B\) such that the number of intermediate intersections does not exceed \(K\). An intermediate intersection is any intersection encountered along the path excluding the source and destination.
Formally, if the path from \(A\) to \(B\) uses \(d\) edges, then the number of intermediate nodes is \(d-1\). The condition for a valid bypass is:
\[ d - 1 \leq K \]
If a direct edge exists (i.e. \(d = 1\)), then the path has 0 intermediate intersections and is valid provided \(K \geq 0\).
inputFormat
The first line contains five space-separated integers \(n\), \(m\), \(K\), \(A\), and \(B\):
- \(n\) is the number of intersections.
- \(m\) is the number of one-way streets.
- \(K\) is the maximum allowed number of intermediate intersections.
- \(A\) is the starting intersection.
- \(B\) is the destination intersection.
The next \(m\) lines each contain two space-separated integers \(u\) and \(v\), indicating a one-way street from intersection \(u\) to intersection \(v\).
outputFormat
Output a single line containing either True
or False
. Output True
if there exists a path from \(A\) to \(B\) such that the number of intermediate intersections is at most \(K\); otherwise, output False
.
5 6 2 1 5
1 2
2 3
3 4
4 5
1 3
1 4
True
</p>