#C5595. Planting Trees Without Cycles

    ID: 49261 Type: Default 1000ms 256MiB

Planting Trees Without Cycles

Planting Trees Without Cycles

You are given N trees (numbered from 0 to N-1) and M planting dependencies. Each dependency is a directed edge from tree a to tree b, meaning tree a must be planted before tree b. Your task is to determine whether it is possible to plant all the trees following the rules, i.e. whether the dependency graph contains no cycles.

In mathematical terms, given a directed graph \(G=(V,E)\) with \(|V|=N\) and \(|E|=M\), check if \(G\) is a Directed Acyclic Graph (DAG). If it is a DAG, output YES; otherwise, output NO.

Hint: A common approach to solve this problem is by using topological sorting (e.g. Kahn's algorithm).

inputFormat

The first line contains two space-separated integers N and M, representing the number of trees and the number of dependencies respectively. Each of the following M lines contains two space-separated integers a and b indicating a directed edge from tree a to tree b.

outputFormat

Output a single line containing YES if it is possible to plant the trees without forming any cycles, or NO otherwise.

## sample
3 2
0 1
1 2
YES