#C10684. Cycle Tour Detection

    ID: 39916 Type: Default 1000ms 256MiB

Cycle Tour Detection

Cycle Tour Detection

You are given a directed graph representing waypoints and trails. The graph has V vertices (numbered from 1 to V) and E directed edges. Each edge represents a trail from one waypoint to another. Your task is to determine whether there exists a cycle in the graph. In other words, check if there is a starting point from which it is possible to travel along one or more trails and return to the same waypoint.

If such a cycle exists, print YES; otherwise, print NO.

Note: The input is read from standard input and the result must be written to standard output.

inputFormat

The first line of input contains two integers V and E, representing the number of vertices and edges respectively. The following E lines each contain two integers u and v, indicating a directed edge from vertex u to vertex v.

Constraints:

  • 1 ≤ V ≤ 105
  • 0 ≤ E ≤ 105

outputFormat

Output a single line containing YES if there exists a cycle in the graph; otherwise, output NO.

## sample
4 5
1 2
2 3
3 4
4 2
4 1
YES