Cycles P36563


Statement
 

pdf   zip

Given a directed graph with nn vertices and mm arcs, can you keep exactly nn arcs (and remove the rest) in such a way that every vertex belongs to one cycle of the resulting graph?

Input

Input consists of several cases, each one with nn and mm, followed by nn pairs xx yy to indicate an arc from xx to yy, with xyx \ne y. Assume 2n10002 \le n \le 1000, nm5nn \le m \le 5n, that vertices are numbered from 0 to n1n-1, and that there are no repeated arcs.

Output

Print one line for every given graph. If there is no solution, print “no”. Otherwise, print “yes” followed by the nn chosen arcs in any order. If there is more than one solution, you can print any one. Follow strictly the format of the sample output.

Hint

Consider the max-flow problem.

Public test cases
  • Input

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

    Output

    yes  0 1  1 2  2 0
    no
    yes  0 2  1 3  2 1  3 0
    yes  2 0  3 1  1 3  0 2
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++