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 vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
#define PRINT(V) for (auto v:(V)) {cerr << v << " ";} cerr << endl
#define dump(x) cerr << #x << " : " << (x) << endl
#define dump2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b){return b ? GCD(b, a % b) : a;}
inline ll LCM(ll a, ll b){return a / GCD(a, b) * b;}
inline ll powint(ll x, ll y){ll r=1;while(y){if(y&1)r*=x;x*=x;y>>=1;}return r;}
inline ll powmod(ll x, ll y, ll m = MOD){ll r=1;while(y){if(y&1)r*=x;x*=x;r%=m;x%=m;y>>=1;}return r;}
/* END OF TEMPLATE */

// 隣接リスト 1-index, to
vvll g(100060, vll());
// 辺リスト 1-index
vll a(100020), b(100030);
// edge used
map<ll, int> eu;
// deg
vll gd(100040, INF);
// minimum deg
ll md = INF;
// locale
ll mdl = 0;
// stats
vll gst(100050, 0);

struct SegmentTree{
    vector<pll> tree;
    int n;
    int n_ex;
    
    SegmentTree(const vll& vec){
        n = vec.size();
        int a = 1;
        while(a < n){
            a *= 2;
        }
        n_ex = a;
        // set tree
        // parent: floor((n-1)/2)
        // child: 2n+1, 2n+2
        // leaf: n+i-1
        rep(i, 2*n_ex-1) tree.emplace_back(INF, 0);
        rep(i, n){
            update(i, vec[i]);
        }
    }
    
    // corresponds to vec[i] = x;
    void update(int i, ll x){
        i += n_ex - 1;
        tree[i] = make_pair(x, i-n_ex+1);
        while(i > 0){
            i = (i - 1) / 2;
            if(tree[i*2+1].first <= tree[i*2+2].first) tree[i] = tree[i*2+1];
            else tree[i] = tree[i*2+2];
        }
    }
    
    // range minimum query min( [a, b) );
    pll query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return make_pair(INF, -1);
        if(a <= l && r <= b) return tree[k];
        else{
            pll vl = query(a, b, k*2+1, l, (l+r)/2);
            pll vr = query(a, b, k*2+2, (l+r)/2, r);
            if(vl.first <= vr.first){
                return vl;
            }else{
                return vr;
            }
        }
    }
    // from outside
    pll query(int a, int b){
        return query(a, b, 0, 0, n_ex);
    }
};

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    ll n, m;
    cin >> n >> m;
    repr(i, 1, n+1){
        gd[i] = 0;
    }
    repr(i, 1, m+1){
        cin >> a[i] >> b[i];
        g[a[i]].emplace_back(b[i]);
        g[b[i]].emplace_back(a[i]);
        gd[a[i]]++; gd[b[i]]++;
        eu.emplace(a[i]*1000000 + b[i], 0);
        eu.emplace(b[i]*1000000 + a[i], 0);
    }
    SegmentTree ST(gd);
    repr(i, 1, n+1){
        if(gd[i] < md){
            md = gd[i];
            mdl = i;
        }
    }
    if(m % 2){
        cout << -1 << endl;
        return 0;
    }
    //ll nmdl = mdl;
    rep(cn, n){
        /*
        rep(i, 10){
           cerr << gd[i] << " ";
        }
        cerr << endl;
        */
        ll sgm = SZ(g[mdl]);
        rep(i, sgm){
            if(eu[mdl*1000000 + g[mdl][i]] == 0){
                eu[mdl*1000000 + g[mdl][i]] = 1;
                eu[g[mdl][i]*1000000 + mdl] = 1;
                if(gst[mdl] % 2){
                    gst[mdl]++;
                    cout << mdl << " " << g[mdl][i] << "\n";
                }else{
                    gst[g[mdl][i]]++;
                    cout << g[mdl][i] << " " << mdl << "\n";
                }
                gd[g[mdl][i]]--;
                if(!gd[g[mdl][i]]) gd[g[mdl][i]] = INF;
                /*if(gd[g[mdl][i]] <= md && gd[g[mdl][i]] > 0){
                    md = gd[g[mdl][i]];
                    nmdl = g[mdl][i];
                }*/
                ST.update(g[mdl][i], gd[g[mdl][i]]);
                gd[mdl]--;
                if(!gd[mdl]) gd[mdl] = INF;
                ST.update(mdl, gd[mdl]);
            }
        }
        //mdl = nmdl;
        mdl = ST.query(0, n+2).second;
        //dump(mdl);
    }
    
    return 0;
}