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 Go (1.6) (judge verdict: Accepted)

package main
 
import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
)
 
var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  var line, buffer []byte
  var isPrefix bool = true
  var err error
  for isPrefix {
    line, isPrefix, err = reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
  }
  return string(buffer)
}
func NextInt() int {
  var n int
  n, _ = strconv.Atoi(NextLine())
  return n
}
func NextIntVec() []int {
  L := strings.Split(NextLine(), " ")
  N := make([]int, len(L))
  for i := range N {
    N[i], _ = strconv.Atoi(L[i])
  }
  return N
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A *[]int) {
  S := make([]string, len((*A)))
  for i, a := range (*A) {
    S[i] = strconv.Itoa(a)
  }
  fmt.Fprintln(writer, strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

type Node struct {
  Index int
  Edge []int
  Degree int
  Undefined int
  Dist int
}
func main() {
  NM := NextIntVec()
  N, M := NM[0], NM[1]
  G := make([]Node, N)
  for i := range G {
    G[i] = Node{i, []int{}, 0, -1, -1}
  }
  E := make([][]int, M)
  for i := range E {
    AB := NextIntVec()
    A, B := AB[0] - 1, AB[1] - 1
    E[i] = []int{A, B}
    G[A].Edge = append(G[A].Edge, i)
    G[B].Edge = append(G[B].Edge, i)
  }
  G[0].Dist = 0
  Q := make([]int, N)
  pos := 1
  for i, _ := range Q {
    s := Q[i]
    for _, e := range G[s].Edge {
      if G[s].Undefined == e { continue }
      t := E[e][0]
      if t == s { t = E[e][1] }
      if G[t].Dist < 0 {
        Q[pos] = t
        pos++
        G[t].Undefined = e
        G[t].Dist = G[s].Dist + 1
      } else if E[e][0] == s {
        G[s].Degree++
      }
    }
  }
  for i := N - 1; 0 < i; i-- {
    s := Q[i]
    u := G[s].Undefined
    t := E[u][0]
    if t == s { t = E[u][1] }
    if G[s].Degree % 2 > 0 {
      E[u] = []int{s, t}
    } else {
      E[u] = []int{t, s}
      G[t].Degree++
    }
  }
  if M % 2 > 0 {
    Write(-1)
  } else {
    for i, e := range E {
      E[i] = []int{e[0] + 1, e[1] + 1}
      WriteIntVec(&E[i])
    }
  }
  Output()
}