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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
import Data.Char (isSpace)
import Data.List (unfoldr)
import Control.Monad
import Control.Monad.ST
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import qualified Data.ByteString.Char8 as BS
import System.Exit
import qualified Data.IntSet as IntSet
import Data.Monoid
import Data.Foldable

solve :: Int -> Int -> U.Vector (Int,Int) -> IO ()
solve !n !m !edges = do
  let graph :: V.Vector [Int]
      graph = V.create $ do
        vec <- VM.replicate n []
        U.forM_ edges $ \(a,b) -> do
          p <- VM.read vec a
          VM.write vec a $! b : p
          q <- VM.read vec b
          VM.write vec b $! a : q
        return vec
  let graphS :: V.Vector IntSet.IntSet
      graphS = V.create $ do
        vec <- VM.replicate n IntSet.empty
        U.forM_ edges $ \(a,b) -> do
          p <- VM.read vec a
          VM.write vec a $! IntSet.insert b p
          q <- VM.read vec b
          VM.write vec b $! IntSet.insert a q
        return vec
  let treeParent :: U.Vector Int
      treeDepth :: U.Vector Int
      treeSorted :: U.Vector Int
      (treeParent, treeDepth, treeSorted) = runST $ do
        parentVec <- UM.replicate n (-1)
        depthVec <- UM.replicate n (-1)
        sorted <- UM.new n
        iRef <- UM.replicate 1 n
        let pushfront x = do i <- UM.read iRef 0
                             UM.write sorted (i-1) x
                             UM.write iRef 0 (i-1)
        let dfs !d !i = do
              forM_ (graph V.! i) $ \j -> do
                p <- UM.read parentVec j
                when (p == -1) $ do
                  UM.write parentVec j i
                  UM.write depthVec j d
                  dfs (d+1) j
              pushfront i
        UM.write parentVec 0 0
        UM.write depthVec 0 0
        dfs 0 0
        liftM3 (,,) (U.unsafeFreeze parentVec) (U.unsafeFreeze depthVec) (U.unsafeFreeze sorted)
  graphM <- V.thaw graphS
  U.forM_ (U.reverse treeSorted) $ \i -> do
    e <- VM.read graphM i
    if even (IntSet.size e)
      then do forM_IntSet e $ \j -> do
                putStrLn (show (i+1) ++ " " ++ show (j+1))
                VM.modify graphM (IntSet.delete i) j
      else do let p = treeParent U.! i
              forM_IntSet (IntSet.delete p e) $ \j -> do
                putStrLn (show (i+1) ++ " " ++ show (j+1))
                VM.modify graphM (IntSet.delete i) j


main = do
  [n,m] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  edges <- U.replicateM m $ do
    [a,b] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
    return (a-1,b-1)
  if even m then solve n m edges else putStrLn "-1"

foldMap_IntSet :: (Monoid n) => (Int -> n) -> IntSet.IntSet -> n
foldMap_IntSet f set = go set
  where
    go set = case IntSet.splitRoot set of
               [] -> mempty
               [x] -> foldMap f (IntSet.toList x)
               xs -> foldMap go xs

forM_IntSet :: Monad m => IntSet.IntSet -> (Int -> m ()) -> m ()
forM_IntSet set f = go set
  where
    go set = case IntSet.splitRoot set of
               [] -> return ()
               [x] -> forM_ (IntSet.toList x) f
               xs -> forM_ xs go

foldMapM_IntSet :: (Monoid n, Monad m) => (Int -> m n) -> IntSet.IntSet -> m n
foldMapM_IntSet f set = go set
  where
    go set = case IntSet.splitRoot set of
               [] -> return mempty
               [x] -> foldlM (\x v -> mappend x <$> f v) mempty (IntSet.toList x)
               xs -> foldlM (\x set' -> mappend x <$> go set') mempty xs