#K82052. Ordering Pairs for a Valid Sequence

    ID: 35889 Type: Default 1000ms 256MiB

Ordering Pairs for a Valid Sequence

Ordering Pairs for a Valid Sequence

You are given a set of ordered pairs that indicate a constraint between two elements. Each pair (u, v) means that element u must come before element v in any valid ordering. Your task is to determine whether there exists an ordering of the elements that satisfies all given constraints. In other words, you must check if the directed graph formed by these pairs is a Directed Acyclic Graph (DAG).

A valid ordering exists if and only if the graph has no cycles. Mathematically, if we let S denote a valid topologically sorted order over the set of vertices V, then the condition is $$|S| = |V|.$$

inputFormat

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

  1. The first line contains two space‐separated integers n and m, where n is the number of nodes (not necessarily all appearing in the pairs) and m is the number of pairs.
  2. Each of the next m lines contains two integers u and v indicating that u must come before v.

Note: The nodes that appear in pairs can be outside the range [1, n]. However, all nodes in the range [1, n] are considered to exist and must be included in the ordering.

outputFormat

Output a single line to stdout containing either yes if a valid ordering exists (i.e. the constraints can be satisfied), or no if not.

## sample
3 3
1 2
2 3
1 3
yes