Is it cyclic? P45234


Statement
 

pdf   zip

thehtml

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 of a graph G. Follow m pairs u, v, indicating that there is an arc uv in G, with 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++ Python
    User solutions
    Python