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 PyPy3 (2.4.0) (judge verdict: Wrong Answer)

from collections import defaultdict
import sys
input = sys.stdin.readline
INF = float('inf')

def main():
    class SegmentTree:
        def __init__(self,N):
            self.NN = 1
            while self.NN < N:
                self.NN *= 2
            self.SegTree = [[0,i-self.NN+1] for i in range(self.NN*2-1)]

        def update(self,i,x): # i の値を +x
            i += self.NN - 1
            tmp = self.SegTree[i][0] + x
            if tmp == 0:
                tmp = INF
            self.SegTree[i] = [tmp,i-self.NN+1]
            while i>0:
                i = (i-1)//2
                if self.SegTree[i*2+1][0] <= self.SegTree[i*2+2][0]:
                    self.SegTree[i] = self.SegTree[i*2+1]
                else:
                    self.SegTree[i] = self.SegTree[i*2+2]

        def query(self,a,b,k=0,l=0,r=None): #[A,B)の値, 呼ぶときはquery(a,b)
            if r == None: r = self.NN
            if r <= a or b <= l: #完全に含まない
                return [INF,i]
            elif a <= l and r <= b : #完全に含む
                return self.SegTree[k]
            else: #交差する
                vl,il = self.query(a,b,k*2+1,l,(l+r)//2)
                vr,ir = self.query(a,b,k*2+2,(l+r)//2,r)
                if vl <= vr:
                    return [vl,il]
                else:
                    return [vr,ir]
                #return self.process(il,ir)

    N,M = map(int, input().split())
    cnts = [0]*N
    lines = defaultdict(set)
    for _ in range(M):
        A,B = map(int, input().split())
        A -= 1
        B -= 1
        cnts[A] += 1
        cnts[B] += 1
        lines[A].add(B)
        lines[B].add(A)

    if M%2 == 1:
        print(-1)
    else:
        ST = SegmentTree(N)
        for i,n in enumerate(cnts):
            ST.update(i,n)


        cnts = [0]*N # 出てる辺の数
        for _ in range(M):
            n,s = ST.query(0,N)
            if n == INF:
                break
            if cnts[s]%2 == 0: # s 出てる辺 == 偶数
                for t in lines[s]: # t -> s に全部
                    print(t+1,s+1)
                    cnts[t] += 1
                    ST.update(t,-1)
                    lines[t] -= {s}
                #lines[s] = {}
                ST.update(s,-n)

            else: # s 出てる辺 == 奇数
                first = True
                for t in lines[s]: # s -> t に1つ 他t->s
                    if first:
                        print(s+1,t+1)
                        cnts[s] += 1
                        first = False
                    else:
                        print(t+1,s+1)
                        cnts[t] += 1
                    ST.update(t,-1)
                    lines[t] -= {s}
                #lines[s] = {}
                ST.update(s,-n)



if __name__ == '__main__':
    main()