Problem:
Score : 700 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i.
Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph.
Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.

Notes

An undirected graph is said to be simple when it contains no self-loops or multiple edges.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N (1\leq i\leq M)

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If it is impossible to assign directions to satisfy the requirement, print -1.
Otherwise, print an assignment of directions that satisfies the requirement, in the following format:

C_1 D_1
:
C_M D_M

Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.

Sample Input 1

4 4
1 2
2 3
3 4
4 1

Sample Output 1

1 2
1 4
3 2
3 4

After this assignment of directions, Vertex 1 and 3 will each have two outgoing edges, and Vertex 2 and 4 will each have zero outgoing edges.

Sample Input 2

5 5
1 2
2 3
3 4
2 5
4 5

Sample Output 2

-1

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

int visit[100000];

bool dfs(int c, int p, int depth, vector<vector<vector<int>>> &edges, vector<pii> &ret) {
    bool end = false;
    for (auto &e : edges[c]) {
        if (e[0] == p)
            continue;
        if (e[2] != 0)
            continue;
        e[2] = 1;
        edges[e[0]][e[1]][2] = 1;
        if (depth % 2 == 0)
            ret.emplace_back(e[0], c);
        else
            ret.emplace_back(c, e[0]);

        end = dfs(e[0], c, depth + 1, edges, ret);
        if (end)
            break;
        else {
            e[2] = 0;
            edges[e[0]][e[1]][2] = 0;
            ret.pop_back();
        }
    }
    if (!end) {
        if (depth % 2 == 0)
            end = true;
    }
    return end;
}

int main() {
    int n, m;
    cin >> n >> m;
    vi a(m), b(m);
    vector<vector<vector<int>>> edges(n); // to, inv, used
    for (int i = 0; i < m; ++i) {
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
        edges[a[i]].push_back({b[i], int(edges[b[i]].size()), 0});
        edges[b[i]].push_back({a[i], int(edges[a[i]].size()) - 1, 0});
    }
    vector<pii> ret;
    if (m % 2 != 0) {
        cout << -1 << "\n";
        return 0;
    }
    for (auto &e : edges) {
        for (auto &ee : e) {
            if (ee[2] == 0)
                dfs(ee[0], -1, 0, edges, ret);
        }
    }
    bool ok = true;
    for (auto &e : edges) {
        for (auto &ee : e) {
            if (ee[2] != 1) {
                ok = false;
                break;
            }
        }
    }
    if (ok) {
        for (auto &r : ret) {
            cout << r.first + 1 << " " << r.second + 1 << endl;
        }
    } else {
        cout << -1 << "\n";
    }

    return 0;
}