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: Accepted)

#include <bits/stdc++.h>

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
*/

using namespace std;

#if DEBUG && !ONLINE_JUDGE
    #include "debug.h"
#else
    #define debug(...)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<vector<int> > vvi;
typedef vector<vector<ii> > vvii;

#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define fill(a,x) memset(a,x,sizeof(a))
#define ff first
#define ss second
#define trav(a, x) for(auto& a : x) 
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
const ll INF = 1e18;
const int mod = 1e9+7;
const int N = 1e5;
ll gcd(ll a,ll b){if (a==0) return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return a*(b/gcd(a,b));}
ll Abs(ll a){if(a>0)return a;return -a;}
ll Ceil(ll a,ll b){if(a%b==0)return a/b;else return a/b+1;}
double Abs(double a){if(a>0)return a;return -a;}

//*X.find_by_order(2) element at index=2
//X.order_of_key(1) how many elements strictly less than 1

/*
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
    tree_order_statistics_node_update>;
*/

inline int pow_(ll x, ll y,ll p){
  int r = 1; while(y){if(y & 1) r = r * x % p; y >>= 1; x = x * x % p;} return r;
}
inline int inv_(int x){return pow_(x, mod - 2, mod);}
inline int add(int a, int b){a += b; if(a >= mod) a -= mod; return a;}
inline int mul(int a, int b){return a * 1LL * b % mod;}
inline int sub(int a, int b){a -= b; if(a < 0) a += mod; return a;}

ll n,m;
vvi G(100005);
vvi G1(100005);
int indeg[100005] = {0};
int visited[100005] = {0};
int f = 0;
vector<ii> fi;

void dfs_visit(int u)
{
	visited[u]=1;
	for(int i=0;i<G[u].size();i++)
	{
		int v = G[u][i];
		if(visited[v]==0)
		{
			G1[u].pb(v);
			dfs_visit(v);
		}
	}
}

void dfs_visit1(int u)
{
	f++;
	visited[u]=1;
	for(int i=0;i<G1[u].size();i++)
	{
		int v = G1[u][i];
		if(visited[v]==0)
		{
			dfs_visit1(v);
		}
	}
	f++;
	visited[u]=2;
	fi.pb(mp(f,u));
}

void dfs1()
{
	for(int i=1;i<=n;i++)
	{
		if(visited[i]==0)dfs_visit1(i);
	}
}

void dfs()
{
	for(int i=1;i<=n;i++)
	{
		if(visited[i]==0)dfs_visit(i);
	}
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //freopen("in.txt","r",stdin);
  ll t;
  t = 1;
  while(t--)
  {
    cin>>n>>m;
    vector<ii> edges;
    for(int i=1;i<=m;i++)
    {
      int x,y;
      cin>>x>>y;
      G[x].pb(y);
      G[y].pb(x);
      edges.pb(mp(x,y));
    }

    if(m%2==1)
    {
      cout << "-1\n";
      for(int i=1;i<=n;i++)G[i].clear();
      continue;
    }

    dfs();
    for(int i=1;i<=n;i++)visited[i]=0;
    f=0;
	/*
	for(int i=1;i<=n;i++)
	{
		//for(int j=0;j<G1[i].size();j++)cout << G1[i][j] << " ";
		//cout << "\n";
	}
	*/
	dfs1();
	sort(all(fi));
	//for(int i=0;i<fi.size();i++)cout << fi[i].second << " ";cout << "\n";
	set<ii> s;
	int process[100005] = {0};
	for(int i=0;i<fi.size();i++)
	{
		int u = fi[i].second;
		for(int i=0;i<G[u].size();i++)
		{
			int v = G[u][i];
			if(process[v]==0)
			{
				if(indeg[u]%2==1)
				{
					indeg[u]++;
					//cout << v <<  " " << u << "\n";
					s.insert(mp(v,u));
				}
				else
				{
					indeg[v]++;
					//cout << u << " " << v << "\n";
					s.insert(mp(u,v));
				}
			}
		}
		process[u] = 1;
	}

	int ans[100005] = {0};
	for(int i=0;i<edges.size();i++)
	{
		int u = edges[i].first;
		int v = edges[i].second;
		//cout << u << " " << v << "\n"; 
		if(s.find(mp(u,v))!=s.end())ans[i]=0;
		else ans[i]=1;
	}

	for(int i=1;i<=n;i++)
	{
		assert(indeg[i]%2==0);
	}

	for(int i=0;i<edges.size();i++)
	{
		int u = edges[i].first;
		int v = edges[i].second;

		if(ans[i] == 0)
		{
			cout << v << " " << u << "\n";
		}
		else
		{
			cout << u << " " << v << "\n";
		}
	}

	for(int i=1;i<=n;i++)
	{
		visited[i]=0;
		G[i].clear();
		indeg[i] = 0;
		G1[i].clear();
		f = 0;
		fi.clear();
	}
  }
  return 0;
}