#C6150. Treasure Hunt Clues

    ID: 49879 Type: Default 1000ms 256MiB

Treasure Hunt Clues

Treasure Hunt Clues

You are given n treasures and m clues. Each clue is a directed relation (a, b) indicating that treasure a must be found before treasure b. Your task is to determine whether there exists a valid order in which all treasures can be collected that satisfies all of the clues.

This is equivalent to checking if a directed graph (with treasures as nodes and clues as edges) has a valid topological ordering. If the graph contains a cycle, it is impossible to collect the treasures in any order that satisfies all clues. Otherwise, such an order exists.

Note: We expect to process input from standard input (stdin) and output the result to standard output (stdout).

For clarity, consider the following examples:

  • Example 1: n = 4, m = 4, clues = {(1,2), (2,3), (3,4), (4,1)}. The cycle 1→2→3→4→1 makes it impossible to satisfy the clues, so the output is No.
  • Example 2: n = 3, m = 2, clues = {(1,2), (2,3)}. A valid order exists; the output is Yes.
  • Example 3: n = 5, m = 0. With no clues, any order is valid; the output is Yes.

inputFormat

The first line of the input contains two integers n and m, where n is the number of treasures and m is the number of clues. The following m lines each contain two integers a and b indicating that treasure a must be collected before treasure b.

All input should be read from standard input.

outputFormat

Output a single line containing either Yes if it is possible to collect all treasures in an order that satisfies all the clues, or No if it is not possible.

Output the result to standard output.

## sample
4 4
1 2
2 3
3 4
4 1
No