Directed Cycle Detection

View as PDF

Submit solution


Points: 100
Time limit: 1.0s
PyPy 3 3.0s
Python 3 3.0s
Memory limit: 500M

Problem type

You are given a directed graph with n vertices and m edges. Each edge goes from one vertex to another, and vertices are numbered from 1 to n.

Determine whether the graph contains at least one directed cycle. A directed cycle is a sequence of one or more edges that starts and ends at the same vertex while following edge directions.

Input

The first line contains two integers n and m: the number of vertices and directed edges.

The next m lines each contain two integers a and b, meaning there is a directed edge from vertex a to vertex b.

Output

Print YES if the graph contains at least one directed cycle. Otherwise, print NO.

Constraints

  • 1 \le n \le 200000
  • 0 \le m \le 300000
  • 1 \le a, b \le n

Example 1

Input
4 3
1 2
2 3
3 4
Output
NO
Explanation

All edges move forward along the chain, so it is impossible to return to a previous vertex.

Example 2

Input
4 4
1 2
2 3
3 1
3 4
Output
YES
Explanation

The edges 1 \to 2, 2 \to 3, and 3 \to 1 form a directed cycle.


Comments

There are no comments at the moment.