Is it cyclic? X64801


Statement
 

pdf   zip

html

Write a program that, given a directed graph, determines whether the graph has any cycle or not.

Input

Input consists of several cases. Every case begins with the number of vertices n and the number of arcs m. Follow m pairs u, v, indicating that there is an arc uv, where uv. Assume 1 ≤ n ≤ 104, 0 ≤ m ≤ 5n, and that for every pair of vertices u and v there is at most one arc of the kind uv. Vertices are numbered from 0 to n−1.

Output

For every case, print “yes” or “no” depending on whether the graph has any cycle or not.

Public test cases
  • Input

    3 2
    0  1
    1  2
    
    3 3
    0  1
    1  2
    2  0
    
    4 5
    2  3
    1  3
    3  0
    0  2
    0  1
    
    5 6
    0  1
    0  2
    0  3
    1  3
    2  3
    4  1
    

    Output

    no
    yes
    yes
    no
    
  • Information
    Author
    Enric Rodríguez
    Language
    English
    Translator
    Enric Rodríguez
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++