#C11449. Detecting Circular Dependencies

    ID: 40766 Type: Default 1000ms 256MiB

Detecting Circular Dependencies

Detecting Circular Dependencies

In modern file systems, folders may be related by subfolder relationships or symbolic links, which can sometimes lead to circular dependencies. In this problem, you are given a directed graph with nn nodes and mm edges. Each node represents a folder, and each directed edge (u,v)(u, v) represents a relationship from folder uu to folder vv. Your task is to determine whether the graph contains a cycle, i.e. a circular dependency.

Constraints:

  • $1 \le n \le 10^5$
  • $0 \le m \le 10^5$

If there is at least one cycle in the graph, output YES; otherwise, output NO.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains two space-separated integers $n$ and $m$, representing the number of folders and the number of relationships respectively.
  • Each of the following $m$ lines contains two space-separated integers $u$ and $v$, indicating there is an edge from folder $u$ to folder $v$.

outputFormat

Print a single line to standard output (stdout) containing YES if there is a cycle (circular dependency) in the graph, and NO otherwise.## sample

5 6
1 2
2 3
3 4
4 5
5 2
3 1
YES