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 (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define df 0
#define MAX 100010

int q[MAX];
int head=0,tail=0;
void enque(int a){
  q[tail++]=a;
}
int deque(){
  return q[head++];
}
void print_que(){
  int i;
  printf("Q:[ ");
  for(i=head;i<tail;i++)
    printf("%d ",q[i]);
  printf("]\n");
}

int parent[MAX+1]={};

int* list[MAX+1];
int deg[MAX+1]={};
int odeg[MAX+1]={};
int searched[MAX+1]={};
typedef struct{
  int u;
  int v;
  int d;
}edge;
edge tree[MAX];
edge other[MAX];
int comp[MAX]={};
int height[MAX]={};
void dfs(int v);
void input();
int root(int a);
int solve();

void write_tree(int v);
int dep[MAX+1]={};
int depth=0;
void print_list();

int n,m;
int main(){
  scanf("%d%d",&n,&m);
  if(m%2){
    printf("-1\n");
    int a,b,i;
    for(i=0;i<m;i++) scanf("%d%d",&a,&b);
    return 0;
  }
  int i;
  for(i=1;i<=n;i++)list[i]=NULL;
  for(i=0;i<m;i++){
    input();
  }
  dfs(1);
  if(df){
    print_list();
    write_tree(1);
  }
  solve();
  
}


int root(int a){
  if(comp[a]==0)return a;
  int u=root(comp[a]);
  comp[a]=u;
  return u;
}

void input(){
  int a,b;
  scanf("%d%d",&a,&b);
  int u=root(a),v=root(b);
  if(u==v){
    printf("%s%d %d%s\n",df?"[":"",a,b,df?"]":"");
    odeg[a]^=1;
    return;
  }
  list[a]=realloc(list[a],sizeof(int)*(++deg[a]));
  list[b]=realloc(list[b],sizeof(int)*(++deg[b]));
  list[a][deg[a]-1]=b;
  list[b][deg[b]-1]=a;
  int w=(height[u]<height[v])?u:v;
  comp[u+v-w]=w;
  height[w]=height[u+v-w]+1;
}


void dfs(int v){
  int i;
  for(i=0;i<deg[v];i++){
    int u=list[v][i];
    if(u==parent[v])continue;
    parent[u]=v;
    dfs(u);
  }
}

int solve(){
  int i;
  for(i=1;i<=n;i++){
    if(deg[i]==1)enque(i);
  }
  if(df)print_que();
  while(head-tail){
    int v=deque(), u=parent[v];
    if(v==1)continue;
    if(odeg[v]){
      printf("%s%d %d%s\n",df?"[":"",v,u,df?"]":"");
    }else{
      printf("%s%d %d%s\n",df?"[":"",u,v,df?"]":"");
      odeg[u]^=1;
    }
    searched[u]++;
    if(deg[u]-searched[u]==1)enque(u);
  }
}
void write_tree(int v){
  printf("%2d",v);
  dep[v]=depth++;
  int i,u;
  for(i=0;i<deg[v];i++){
    u=list[v][i];
    if(u==parent[v])continue;
    
      int j;
      if(i==0 || (i==1 && list[v][0]==parent[v]))printf("-");
      else{
	for(j=0;j<dep[v]*3+2;j++)printf(" ");
	printf("L");
      }
    
    write_tree(u);
    if(deg[u]==1)printf("\n");
  }
  depth--;
}
void print_list(){
  int i,v;
  for(v=1;v<=n;v++){
    printf("%d:[ ",v);
    for(i=0;i<deg[v];i++){
      printf("%d ",list[v][i]);
    }
    printf("] deg:%d\n",deg[v]);
  }
}

/// confirm df==0 ///