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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
using System.Text;//テキストの高速出力に必要
class Program
{
static string[] input = Console.ReadLine().Split(' ');
static int n = int.Parse(input[0]);
static int m = int.Parse(input[1]);
static List<int>[] nextList = new List<int>[n];
static List<int>[] childrenList = new List<int>[n];
static int[,] selectTreeEdges = new int[m,2];
static int selectTreeEdgesCount = 0;
static bool[] lookedVertexes = new bool[n];//木の逆走を防ぐ
static int[] outDegree = new int[n];
static List<int> eulerTourList = new List<int>();
static int[] depths = new int[n];//木の深さ

	static void Main()
	{
  
  if(m % 2 == 0)
  {
    for(int i = 0; i < n; i++)
    {
      nextList[i] = new List<int>();
      childrenList[i] = new List<int>();
    }
    int[,] edges = new int[m,2];

    for(int i = 0; i < m; i++)
    {
      int[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse);
      nextList[nums[0]-1].Add(nums[1]-1);
      nextList[nums[1]-1].Add(nums[0]-1);
      edges[i,0] = nums[0]-1;
      edges[i,1] = nums[1]-1;
    }

    SelectTreeEdges(0);
    int[,] notSelectEdges = NotSelectEdges(edges, selectTreeEdges);

    int[][] answers = new int[m][];
    int answerCount = 0;
    for(int i = 0; i < m; i++) answers[i] = new int[2];

    for(int i = 0; i < notSelectEdges.GetLength(0); i++)
    {
      outDegree[notSelectEdges[i,0]]++;
      answers[answerCount][0] = notSelectEdges[i,0]+1;
      answers[answerCount][1] = notSelectEdges[i,1]+1;
      answerCount++;
    }

    for(int i = 0; i < n; i++)
    {
      lookedVertexes[i] = false;
      nextList[i].Clear();
    }
    for(int i = 0; i < n-1; i++)
    {
      nextList[selectTreeEdges[i,0]].Add(selectTreeEdges[i,1]);
      nextList[selectTreeEdges[i,1]].Add(selectTreeEdges[i,0]);
    }
    
    MakeEulerTour(0);
    for(int i = 0; i < n; i++) lookedVertexes[i] = false;
    MakeChildrenList(0);
    SearchDepths(0,0);
    
    for(int i = 1; i < eulerTourList.Count(); i++)
    {
      if(depths[eulerTourList[i-1]] > depths[eulerTourList[i]])
      {
        if(outDegree[eulerTourList[i-1]] % 2 == 1)
        {
          answers[answerCount][0] = eulerTourList[i-1]+1;
          answers[answerCount][1] = eulerTourList[i]+1;
          answerCount++;
        }else
        {
          answers[answerCount][0] = eulerTourList[i]+1;
          answers[answerCount][1] = eulerTourList[i-1]+1;
          answerCount++;
          outDegree[eulerTourList[i]]++;
        }
      }
    }
    
    var answerText = new StringBuilder();
    for(long i = 0; i < answers.Length; i++)
    {
      answerText.AppendLine(string.Join(" ", answers[i]));
    }
    Console.Write(answerText);
    
  }else
  {
    Console.WriteLine(-1);
  }
    
	}

  static void SelectTreeEdges(int rootVertex)
  {//連結グラフのnextListから、0-indexで根をrootVertexとする木の辺をO(頂点数)で1つ返す
    selectTreeEdgesCount = 0;
    for(int i = 0; i < n; i++) lookedVertexes[i] = false;
    for(int i = 0; i < m; i++)
    {
      selectTreeEdges[i,0] = -1;
      selectTreeEdges[i,1] = -1;
    }
    SelectTreeEdgesSub(rootVertex);
  }

  static void SelectTreeEdgesSub(int rootVertex)
  {
    lookedVertexes[rootVertex] = true;
    for(int i = 0; i < nextList[rootVertex].Count(); i++)
    {
      if(!lookedVertexes[nextList[rootVertex][i]])
      {
        selectTreeEdges[selectTreeEdgesCount,0] = rootVertex;
        selectTreeEdges[selectTreeEdgesCount,1] = nextList[rootVertex][i];
        selectTreeEdgesCount++;   
        SelectTreeEdgesSub(nextList[rootVertex][i]);
      }
    }
  }

  static void MakeEulerTour(int vertexNum)
  {//0-indexで根の頂点番号から始めて、木のオイラーツアーをO(頂点数)で生成
    eulerTourList.Clear();

    for(int i = 0; i < n; i++)
    {
      lookedVertexes[i] = false;
    }
    
    EulerTour(vertexNum);
  }

  static void EulerTour(int vertexNum)
  {
    eulerTourList.Add(vertexNum);
    lookedVertexes[vertexNum] = true;//木の逆走を防ぐ
    
    for(int i = 0; i < nextList[vertexNum].Count(); i++)
    {
      if(!lookedVertexes[nextList[vertexNum][i]])//木の逆走を防ぐ
      {
        EulerTour(nextList[vertexNum][i]);
        eulerTourList.Add(vertexNum);
      }
    }
  }

  static int[,] NotSelectEdges(int[,] edges, int[,] removes)
  {//edgesからremovesを除いた辺をO(辺数)で返す
    int x = edges.GetLength(0);
    int y = 0;
    for(int i = 0; i < x; i++)
    {
      if(removes[i,0] != -1) y++;
    }
    int[,] answers = new int[x-y,2];
    int[][] edgesSub = new int[x][];
    int[][] removesSub = new int[y][];
    for(int i = 0; i < x; i++)
    {
      edgesSub[i] = new int[2];
      edgesSub[i][0] = edges[i,0];
      edgesSub[i][1] = edges[i,1];
    }
    for(int i = 0; i < y; i++)
    {
      removesSub[i] = new int[2];
      removesSub[i][0] = removes[i,0];
      removesSub[i][1] = removes[i,1];
    }
    
    for(int i = 0; i < x; i++)
    {
      //Console.WriteLine(edgesSub[i][0] +" "+ edgesSub[i][1]+"in");
      if(edgesSub[i][0] > edgesSub[i][1])
      {
        int swapMemo = edgesSub[i][0];
        edgesSub[i][0] = edgesSub[i][1];
        edgesSub[i][1] = swapMemo;
      }
    }
    for(int i = 0; i < y; i++)
    {
      if(removesSub[i][0] > removesSub[i][1])
      {
        int swapMemo = removesSub[i][0];
        removesSub[i][0] = removesSub[i][1];
        removesSub[i][1] = swapMemo;
      }
    }

    var sorted = edgesSub.OrderBy(a => a[0]).ThenBy(a => a[1]).ToList();
    var sortedSub = removesSub.OrderBy(a => a[0]).ThenBy(a => a[1]).ToList();

    int edgeMemo = 0;
    int addMemo = 0;
    for(int i = 0; i < x; i++)
    {
      while((sorted[i][0] > sortedSub[edgeMemo][0])
        || (sorted[i][0] == sortedSub[edgeMemo][0] && sorted[i][1] > sortedSub[edgeMemo][1]))
      {
        if(edgeMemo == y-1) break;
        else edgeMemo++;
      }
      if(sorted[i][0] != sortedSub[edgeMemo][0] || sorted[i][1] != sortedSub[edgeMemo][1])
      {
        answers[addMemo,0] = sorted[i][0];
        answers[addMemo,1] = sorted[i][1];
        addMemo++;
      }
    }

    return answers;
  }

  static void MakeChildrenList(int rootNum)
  {//nextListからchildrenListを、引数を0-indexの根の番号としてO(頂点数)で作る
    lookedVertexes[rootNum] = true;//木の逆走を防ぐ
    for(int i = 0; i < nextList[rootNum].Count(); i++)
    {
      if(!lookedVertexes[nextList[rootNum][i]])
      {
        childrenList[rootNum].Add(nextList[rootNum][i]);
        MakeChildrenList(nextList[rootNum][i]);
      }
    }
  }

  static void SearchDepths(int vertexNum, int depth)
  {//頂点数nとchildrenListから、引数の頂点の深さ及び、その子の深さをO(頂点数)で設定
    depths[vertexNum] = depth;//深さを設定

    for(int i = 0; i < childrenList[vertexNum].Count; i++)
    {
      SearchDepths(childrenList[vertexNum][i],depth+1);//子の深さを設定
    }
  }

}