This page is still under construction.

Parts of this page are still being built. What you see may change.

One-Way Roads

Time limit1sMemory limit256 MB

Summary
Decide whether undirected roads can be oriented into a strongly connected digraph and output the DFS-based orientation when possible.
Level

Medium7 of 10

Topics
DFS, Graph
Solved
No attempts yet

Problem

The ACM kingdom has NN cities joined by MM two-way roads. The road network is connected, so you can travel from any city to any other city along the roads. The government wants to make every road one-way. After the directions are fixed, it must still be possible to travel from any city to any other city. Decide whether such an assignment exists, and print the one described in the output section when it does.

Input

The first line contains TT (0≤T≤1000 \le T \le 100), the number of test cases.

The first line of each test case contains two integers NN (1≤N≤501 \le N \le 50) and MM (1≤M≤N(N−1)/21 \le M \le N(N-1)/2). Each of the next MM lines contains two integers XX and YY (1≤X,Y≤N1 \le X, Y \le N, X≠YX \ne Y), a road between city XX and city YY. At most one road joins a given pair of cities, and the road network of every test case is connected.

Output

For each test case, print NO on a single line when no assignment works. Otherwise print YES on the first line, then MM lines that give the direction of each road. Line ii holds the start city and then the end city of the ii-th road of the input.

Several assignments can work, so exactly one of them counts as correct. Build it like this. Run a depth-first search that starts at city 1. When the search leaves a city it moves to the unvisited neighboring city with the smallest number, and it backtracks once every neighbor of the current city is already visited. A road that the search uses to reach a city for the first time is directed from the city the search left towards the city it reached. Every other road is directed from the endpoint that the search visited later towards the endpoint that the search visited earlier. If any assignment keeps the kingdom fully connected, this one does.

Examples1

  1. Example 1

    Input
    3
    3 3
    1 2
    2 3
    1 3
    4 3
    1 2
    1 3
    1 4
    4 5
    1 2
    2 3
    4 3
    1 4
    2 4
    
    Expected output
    YES
    1 2
    2 3
    3 1
    NO
    YES
    1 2
    2 3
    3 4
    4 1
    4 2