A data pipeline has n processing stages. Some stages must run before others, given as directed constraints: an edge u v means stage u must run strictly before stage v. The whole pipeline can be scheduled only if there is an ordering of all n stages that respects every constraint. Determine whether such an ordering exists.
Input format
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 to (read as: must come before ).
Output format
Print YES if a valid ordering of all stages exists, otherwise print NO.
Constraints
- 1 <= n <= 2000
- 0 <= m <= 5000
- 0 <= u, v <= n-1 (an edge may be a self-loop
u u, which makes ordering impossible)