A logistics network has n facilities, numbered 1 to n, and m one-way shipping routes. A route u v means product can flow directly from facility u to facility v (but not necessarily the other way). Product can also flow through any number of intermediate facilities by chaining routes.
Given a source facility and a destination facility, determine whether a product can be shipped from the source to the destination (a facility can always "ship to itself" with zero hops).
Line 1: two integers n and m — number of facilities (1-indexed) and number of routes.
Next m lines: two integers u v — a directed route from facility u to facility v.
Last line: two integers src dst — the source and destination facilities.
A single integer: 1 if dst is reachable from src, 0 otherwise.
Example 1
Input
4 3 1 2 2 3 3 4 1 4
Expected
1
Explanation
Following 1 -> 2 -> 3 -> 4 reaches facility 4 from facility 1. Output 1.
Example 2
Input
3 1 2 3 1 3
Expected
0
Explanation
Facility 1 has no outgoing route at all, so facility 3 is unreachable from it. Output 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →