Problem:
One-Way Conveyors

You are working at a factory manufacturing many different products. Products have to be processed on a number of different machine tools. Machine shops with these machines are connected with conveyor lines to exchange unfinished products. Each unfinished product is transferred from a machine shop to another through one or more of these conveyors.

As the orders of the processes required are not the same for different types of products, the conveyor lines are currently operated in two-way. This may induce inefficiency as conveyors have to be completely emptied before switching their directions. Kaizen (efficiency improvements) may be found here!

Adding more conveyors is too costly. If all the required transfers are possible with currently installed conveyors operating in fixed directions, no additional costs are required. All the required transfers, from which machine shop to which, are listed at hand. You want to know whether all the required transfers can be enabled with all the conveyors operated in one-way, and if yes, directions of the conveyor lines enabling it.

Input

The input consists of a single test case of the following format.

$n$ $m$
$x_1$ $y_1$
.
.
.
$x_m$ $y_m$
$k$
$a_1$ $b_1$
.
.
.
$a_k$ $b_k$

The first line contains two integers $n$ ($2 \leq n \leq 10 000$) and $m$ ($1 \leq m \leq 100 000$), the number of machine shops and the number of conveyors, respectively. Machine shops are numbered $1$ through $n$. Each of the following $m$ lines contains two integers $x_i$ and $y_i$ ($1 \leq x_i < y_i \leq n$), meaning that the $i$-th conveyor connects machine shops $x_i$ and $y_i$. At most one conveyor is installed between any two machine shops. It is guaranteed that any two machine shops are connected through one or more conveyors. The next line contains an integer $k$ ($1 \leq k \leq 100 000$), which indicates the number of required transfers from a machine shop to another. Each of the following $k$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i \leq n$, $1 \leq b_i \leq n$, $a_i \ne b_i$), meaning that transfer from the machine shop $a_i$ to the machine shop $b_i$ is required. Either $a_i \ne a_j$ or $b_i \ne b_j$ holds for $i \ne j$.

Output

Output “No” if it is impossible to enable all the required transfers when all the conveyors are operated in one-way. Otherwise, output “Yes” in a line first, followed by $m$ lines each of which describes the directions of the conveyors. All the required transfers should be possible with the conveyor lines operated in these directions. Each direction should be described as a pair of the machine shop numbers separated by a space, with the start shop number on the left and the end shop number on the right. The order of these $m$ lines do not matter as far as all the conveyors are specified without duplicates or omissions. If there are multiple feasible direction assignments, whichever is fine.

Sample Input 1

3 2
1 2
2 3
3
1 2
1 3
2 3

Sample Output 1

Yes
1 2
2 3

Sample Input 2

3 2
1 2
2 3
3
1 2
1 3
3 2

Sample Output 2

No

Sample Input 3

4 4
1 2
1 3
1 4
2 3
7
1 2
1 3
1 4
2 1
2 3
3 1
3 2

Sample Output 3

Yes
1 2
2 3
3 1
1 4

Solution in JAVA (judge verdict: Accepted)

import java.io.PrintWriter;
import java.util.*;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        new Thread(null, Main::solve, "solver", 64000000L).start();
    }
    public static void solve() {
        try (Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out)) {
            int n = in.nextInt(), m = in.nextInt();

            // Graph Construction
            Node[] nodes = new Node[n];
            for (int i = 0; i < n; i++) nodes[i] = new Node(i);
            for (int i = 0; i < m; i++) {
                int x = in.nextInt() - 1, y = in.nextInt() - 1;
                Edge nat = new Edge(nodes[x], nodes[y]);
                Edge rev = new Edge(nodes[y], nodes[x]);
                nat.rev = rev;
                rev.rev = nat;
                nodes[x].dest.add(nat);
                nodes[y].dest.add(rev);
            }

            // Bridge listing
            Bridges bridges = new Bridges(nodes);
            bridges.solve();

            int group = 0;
            for (int i = 0; i < n; i++) {
                if (nodes[i].visited) continue;
                nodes[i].dir(group);
                group++;
            }

            // Tree Construction
            Node[] tree = new Node[group];
            for (int i = 0; i < group; i++) tree[i] = new Node(i);
            for (int i = 0; i < n; i++) {
                for (Edge edge : nodes[i].dest) {
                    if (!edge.isBridge || edge.t.index < edge.s.index) continue;
                    int x = edge.s.group, y = edge.t.group;
                    Edge nat = new Edge(tree[x], tree[y]);
                    Edge rev = new Edge(tree[y], tree[x]);
                    nat.rev = rev;
                    nat.subEdge = edge;
                    rev.rev = nat;
                    rev.subEdge = edge.rev;
                    tree[x].dest.add(nat);
                    tree[y].dest.add(rev);
                }
            }
            tree[0].treenize(null);

            // Solve
            try {
                int k = in.nextInt();
                for (int i = 0; i < k; i++) {
                    int x = in.nextInt() - 1, y = in.nextInt() - 1;
                    int s = nodes[x].group, t = nodes[y].group;
                    if (s == t) continue;
                    Node lca = Node.lca(tree[s], tree[t]);
                    if (tree[s] != lca) {
                        Constraint c = new Constraint(Direction.UP, tree[s], lca);
                        tree[s].constraint = tree[s].constraint == null ? c : tree[s].constraint.merge(c);
                    }
                    if (tree[t] != lca) {
                        Constraint c = new Constraint(Direction.DOWN, tree[t], lca);
                        tree[t].constraint = tree[t].constraint == null ? c : tree[t].constraint.merge(c);
                    }
                }

                tree[0].solve();
            } catch (RuntimeException ex) {
                out.println("No");
                return;
            }

            out.println("Yes");
            for (int i = 0; i < n; i++) {
                for (Edge edge : nodes[i].dest) {
                    if (!edge.rev.valid) {
                        edge.valid = true;
                        out.println((edge.s.index + 1) + " " + (edge.t.index + 1));
                    }
                }
            }
        }
    }

    private static class Bridges {
        int n;
        Node[] p;
        int[] pre, low;
        int pr;

        Bridges(Node[] p) {
            this.n = p.length;
            this.pre = new int[n];
            this.low = new int[n];
            this.p = p;
        }

        int dfs(int i, int from) {
            if (pre[i] > 0) return low[i];
            pre[i] = pr;
            low[i] = pr;
            for (Edge edge : p[i].dest) {
                int j = edge.t.index;
                if (j == from) continue;
                pr++;
                int to = dfs(j, i);
                if (to < pre[i]) {
                    low[i] = Math.min(low[i], to);
                } else {
                    if (low[j] == pre[j]) {
                        edge.isBridge = edge.rev.isBridge = true;
                    }
                }
            }
            return low[i];
        }

        void solve() {
            pr = 1;
            dfs(0, -1);
        }

    }

    private static class Node {
        int index, group;
        List<Edge> dest = new ArrayList<>();
        boolean visited;
        Node[] parents;
        int depth;
        Constraint constraint;


        Node(int index) {
            this.index = index;
        }

        void dir(int group) {
            if (visited) return;
            visited = true;
            this.group = group;
            for (Edge edge : dest) {
                if (edge.isBridge) continue;
                if (!edge.rev.valid) edge.valid = true;
                if (edge.t.visited) continue;
                edge.t.dir(group);
            }
        }

        void treenize(Node from) {
            this.parents = new Node[20];
            this.depth = (from == null) ? 0 : from.depth + 1;
            this.parents[0] = (from == null) ? this : from;
            for (int i = 1; i < 20; i++) {
                parents[i] = parents[i - 1].parents[i - 1];
            }

            for (Edge edge : dest) {
                if (edge.t == from) continue;
                edge.t.treenize(this);
            }
        }

        static Node lca(Node x, Node y) {
            if (x.depth < y.depth) return lca(y, x);
            for (int i = 19; i >= 0; i--) {
                if (x.parents[i].depth > y.depth) x = x.parents[i];
            }
            if (x.depth > y.depth) x = x.parents[0];

            for (int i = 19; i >= 0; i--) {
                if (x.parents[i] != y.parents[i]) {
                    x = x.parents[i];
                    y = y.parents[i];
                }
            }
            if (x != y) return x.parents[0];
            return x;
        }

        void solve() {
            Constraint c = this.constraint;
            for (Edge e : dest) {
                if (parents[0] == e.t) continue;
                e.t.solve();
                if (e.t.constraint != null) {
                    if (e.t.constraint.direction == Direction.UP) e.subEdge.rev.valid = true;
                    else e.subEdge.valid = true;
                    if (e.t.constraint.until != this) {
                        if (c == null) c = e.t.constraint;
                        else c = c.merge(e.t.constraint);
                    }
                }
            }
            this.constraint = c;
        }
    }

    private static class Edge {
        Node s, t;
        Edge rev;
        Edge subEdge;
        boolean isBridge;
        boolean valid;

        Edge(Node s, Node t) {
            this.s = s;
            this.t = t;
        }
    }

    enum Direction {
        UP, DOWN;
    }

    private static class Constraint {
        Direction direction;
        Node from, until;

        Constraint(Direction direction, Node from, Node until) {
            this.direction = direction;
            this.from = from;
            this.until = until;
        }

        Constraint merge(Constraint other) {
            if (direction != other.direction) throw new RuntimeException("Constraint violation@"+from.index+"and"+other.from.index);
            return until.depth < other.until.depth ? this : other;
        }
    }

}