Is it cyclic? X64801


Statement
 

pdf   zip

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 nn and the number of arcs mm. Follow mm pairs uu, vv, indicating that there is an arc uvu \rightarrow v, where uvu \neq v. Assume 1n1041 \leq n \leq 10^{4}, 0m5n0 \leq m \leq 5n, and that for every pair of vertices uu and vv there is at most one arc of the kind uvu \rightarrow v. Vertices are numbered from 00 to n1n-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++