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: Wrong Answer)

package main
 
import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
  "container/heap"
)
 
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 Item struct {
  Comparison  int
  Index int
}
type PriorityQueue []*Item
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
  return pq[i].Comparison < pq[j].Comparison
}
func (pq PriorityQueue) Swap(i, j int) {
  pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Pop() interface{} {
  old := *pq
  n := len(old)
  item := old[n - 1]
  *pq = old[0:n - 1]
  return item
}
func (pq *PriorityQueue) Push(x interface{}) {
  item := x.(*Item)
  *pq = append(*pq, item)
}

type Node struct {
  Index int
  Degree int
  Edge []int
}
func Node2Item(N Node) *Item {
  I := Item{len(N.Edge), N.Index}
  return &I
}

func Search(A *[]int, a int) int {
  N := len((*A))
  if (*A)[0] == a { return 0 }
  if (*A)[N - 1] == a { return N - 1 }
  high, low := N - 1, 0
  for high - low > 1 {
    med := (high + low) / 2
    if (*A)[med] == a { return med }
    if a < (*A)[med] {
      high = med
    } else {
      low = med
    }
  }
  return -1
}

func main() {
  NM := NextIntVec()
  N, M := NM[0], NM[1]
  G := make([]Node, N)
  for i := range G {
    e := make([]int, 0)
    G[i] = Node{i, 0, e}
  }
  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)
  }
  var PQ PriorityQueue
  for _, node := range G {
    heap.Push(&PQ, Node2Item(node))
  }
  for len(PQ) > 0 {
    s := heap.Pop(&PQ).(*Item).Index
    if len(G[s].Edge) == 0 { continue }
    for _, e := range G[s].Edge {
      t := E[e][0]
      if t == s { t = E[e][1] }
      if G[s].Degree % 2 > 0 {
        G[s].Degree++
        E[e] = []int{s, t}
      } else {
        G[t].Degree++
        E[e] = []int{t, s}
      }
      j := Search(&G[t].Edge, e)
      if j < len(G[t].Edge) - 1 {
        G[t].Edge = append(G[t].Edge[:j], G[t].Edge[j + 1:]...)
      } else {
        G[t].Edge = G[t].Edge[:j]
      }
      heap.Push(&PQ, Node2Item(G[t]))
    }
    G[s].Edge = make([]int, 0)
  }
  directable := true
  D := make([]int, N)
  for _, e := range E {
    D[e[0]]++
  }
  for _, d := range D {
    if d % 2 > 0 {
      Write(-1)
      directable = false
      break
    }
  }
  if directable {
    for i, e := range E {
      E[i] = []int{e[0] + 1, e[1] + 1}
      WriteIntVec(&E[i])
    }
  }
  Output()
}