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: Time Limit Exceeded)

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 List<int[]> selectTreeEdges;
static int[] lookedVertexes = new int[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)
  {
    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 < n; i++)
    {
      nextList[i] = new List<int>();
      childrenList[i] = new List<int>();
    }
    List<int[]> edges = new List<int[]>();

    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.Add(new int[2]{nums[0]-1, nums[1]-1});
    }

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

    for(int i = 0; i < notSelectEdges.Count(); 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] = 0;
      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] = 0;
    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つ返す
    selectTreeEdges = new List<int[]>();
    for(int i = 0; i < n; i++) lookedVertexes[i] = 0;
    SelectTreeEdgesSub(rootVertex);
  }

  static void SelectTreeEdgesSub(int rootVertex)
  {
    lookedVertexes[rootVertex] = 1;
    for(int i = 0; i < nextList[rootVertex].Count(); i++)
    {
      if(lookedVertexes[nextList[rootVertex][i]] == 0)
      {
        if(rootVertex < nextList[rootVertex][i])
          selectTreeEdges.Add(new int[2]{rootVertex, nextList[rootVertex][i]});
        else selectTreeEdges.Add(new int[2]{nextList[rootVertex][i], rootVertex});
        
        SelectTreeEdgesSub(nextList[rootVertex][i]);
      }
    }
  }

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

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

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

  static List<int[]> NotSelectEdges(List<int[]> edges, List<int[]> removes)
  {//edgesからremovesを除いた辺をO(辺数)で返す
    List<int[]> answers = new List<int[]>();
    
    for(int i = 0; i < edges.Count(); i++)
    {
      if(edges[i][0] > edges[i][1])
      {
        int swapMemo = edges[i][0];
        edges[i][0] = edges[i][1];
        edges[i][1] = swapMemo;
      }
    }
    for(int i = 0; i < removes.Count(); i++)
    {
      if(removes[i][0] > removes[i][1])
      {
        int swapMemo = removes[i][0];
        removes[i][0] = removes[i][1];
        removes[i][1] = swapMemo;
      }
    }
    var sorted = edges.OrderBy(a => a[0]).ThenBy(a => a[1]).ToList();
    var sortedSub = removes.OrderBy(a => a[0]).ThenBy(a => a[1]).ToList();

    int edgeMemo = 0;
    for(int i = 0; i < edges.Count(); i++)
    {
      while((sorted[i][0] > sortedSub[edgeMemo][0])
        || (sorted[i][0] == sortedSub[edgeMemo][0] && sorted[i][1] > sortedSub[edgeMemo][1]))
      {
        if(edgeMemo == removes.Count()-1) break;
        else edgeMemo++;
      }

      if(sorted[i][0] != sortedSub[edgeMemo][0] || sorted[i][1] != sortedSub[edgeMemo][1])
      {
        answers.Add(new int[2]{sorted[i][0], sorted[i][1]});
      }
    }

    return answers;
  }

  static void MakeChildrenList(int rootNum)
  {//nextListからchildrenListを、引数を0-indexの根の番号としてO(頂点数)で作る
    lookedVertexes[rootNum]++;//木の逆走を防ぐ
    for(int i = 0; i < nextList[rootNum].Count(); i++)
    {
      if(lookedVertexes[nextList[rootNum][i]] == 0)
      {
        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);//子の深さを設定
    }
  }

}