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 Rust (1.15.1) (judge verdict: Runtime Error)

#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(z) = it.next() {
            Some(it.fold(z, |x, y| min!(x, y)))
        } else {
            None
        }
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(z) = it.next() {
            Some(it.fold(z, |x, y| max!(x, y)))
        } else {
            None
        }
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}


macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let m: usize = sc.cin();
    if m % 2 == 1 {
        put!(-1);
        return;
    }

    let mut g = vec![vec![]; n];
    let mut edges = vec![];
    use std::collections::HashSet;
    let mut directed_edges = HashSet::new();  // contains(u, v) iff u->v

    for _ in 0..m {
        let a = sc.cin::<usize>() - 1;
        let b = sc.cin::<usize>() - 1;
        g[a].push(b);
        g[b].push(a);
        edges.push((a, b));
    }

    use std::collections::BinaryHeap;

    let mut degs: Vec<usize> = (0..n).map(|i| g[i].len()).collect();  // un-assigned
    let mut q = BinaryHeap::new();
    for i in 0..n { q.push((-(degs[i] as i32), i)); }

    fn direction(u: usize, v: usize, memo: &HashSet<(usize, usize)>) -> Option<bool> {
        if memo.contains(&(u, v)) {
            Some(true)
        } else if memo.contains(&(v, u)) {
            Some(false)
        } else {
            None
        }
    }

    while let Some((_, u)) = q.pop() {
        if degs[u] == 0 { continue }
        let mut count = 0;
        let mut unk = vec![];
        for &v in g[u].iter() {
            match direction(u, v, &directed_edges) {
                Some(true) => {
                    count += 1;
                }
                Some(false) => {
                }
                None => {
                    unk.push(v);
                }
            }
        }
        degs[u] = 0;
        match (count % 2 == 0, unk.len()) {
            (true, 0) => {}
            (false, 0) => {
                put!(-1);
                return;  // failed
            }
            (true, _) => {
                let m = (unk.len() / 2) * 2;
                for &v in unk[0..m].iter() {
                    directed_edges.insert((u, v));
                    degs[v] -= 1;
                    q.push((-(degs[v] as i32), v));
                }
                for &v in unk[m..].iter() {
                    directed_edges.insert((v, u));
                    degs[v] -= 1;
                    q.push((-(degs[v] as i32), v));
                }
            }
            (false, _) => {
                let m = ((unk.len() - 1) / 2) * 2 + 1;
                for &v in unk[0..m].iter() {
                    directed_edges.insert((u, v));
                    degs[v] -= 1;
                    q.push((-(degs[v] as i32), v));
                }
                for &v in unk[m..].iter() {
                    directed_edges.insert((v, u));
                    degs[v] -= 1;
                    q.push((-(degs[v] as i32), v));
                }
            }
        }
    }

    for u in 0..n {
        let mut count = 0;
        for &v in g[u].iter() {
            match direction(u, v, &directed_edges) {
                Some(true) => { count += 1 }
                Some(false) => {}
                _ => { panic!() }
            }
        }
        if count % 2 != 0 {
            panic!("invalid");
        }
    }

    let mut ans = vec![];
    for (u, v) in edges {
        match direction(u, v, &directed_edges) {
            Some(true) => { ans.push((u, v)); }
            Some(false) => { ans.push((v, u)); }
            _ => {
                put!(-1);
                return;
            }
        }
    }

    for (u, v) in ans {
        put!(u + 1, v + 1);
    }

}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}