A release tool publishes a proposed build order: a permutation of all n modules. A directed edge u v means module u must be built before module v. The proposed order is valid if and only if, for every edge u v, module u appears earlier than module v in the order. Decide whether the proposed order is valid.
Line 1: two integers n and m — the number of nodes (labeled 0 through n-1) and the number of directed edges.
Each of the next m lines: two integers u v, a directed edge from u to v (read as: u must come before v).
The final line: n space-separated integers, a permutation of 0..n-1 giving the proposed build order (first built to last built).
Print YES if the proposed order satisfies every edge, otherwise NO.
u u can never be satisfied)0..n-1.Example 1
Input
3 2 0 1 1 2 0 1 2
Expected
YES
Explanation
Edges need 0 before 1 and 1 before 2; the order 0,1,2 satisfies both: YES.
Example 2
Input
3 2 0 1 1 2 1 0 2
Expected
NO
Explanation
Edge 0->1 requires 0 before 1, but the order places 1 before 0: NO.
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 →