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: Accepted)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*);
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};
#[allow(unused_imports)]
use std::io::{stdout, stdin, BufWriter, Write};

use std::collections::VecDeque;
use std::collections::HashSet;

fn build_tree(n: usize, edges: &Vec<Vec<usize>>) -> Vec<Vec<usize>> {
    let mut q = VecDeque::new();
    q.push_back((0, n));
    let mut vis = vec![false; n];

    let mut tree_edges = vec![vec![]; n];
    while q.len() > 0 {
        let (node, par) = q.pop_front().unwrap();
        if vis[node] {
            continue;
        }
        vis[node] = true;
        if par < n {
            tree_edges[par].push(node);
        }
        for &nd in edges[node].iter() {
            if !vis[nd] {
                q.push_back((nd, node));
            }
        }
    }
    return tree_edges
}

fn dfs(edges: &Vec<Vec<usize>>, cur: usize, par: usize, used: &mut HashSet<(usize,usize)>, g: &Vec<Vec<usize>>, ans: &mut Vec<(usize,usize)>) {
    for &nd in edges[cur].iter() {
        dfs(edges, nd, cur, used, g, ans);
    }

    let mut cnt = 0;
    for &nd in g[cur].iter() {
        if nd != par {
            let pair = if cur < nd { (cur, nd) } else { (nd, cur) };
            if !used.contains(&pair) {
                // debug!(cur, par, nd, used);
                cnt += 1;
                used.insert(pair);
                ans.push((cur, nd));
            }
        }
    }
    if cnt%2 == 1 && cur != 0 {
        let pair = if cur < par { (cur, par) } else { (par, cur) };
        used.insert(pair);
        ans.push((cur, par));
    }
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (writeln!(out,$($format)*).unwrap());
    }

    input!{
      n: usize,
      m: usize,
      abs: [(usize1,usize1); m],
    }
    if m%2 == 1 {
        return puts!("{}", "-1");
    }
    let mut edges = vec![vec![]; n];
    for &(a,b) in abs.iter() {
        edges[a].push(b);
        edges[b].push(a);
    }

    let tree_edges = build_tree(n, &edges);
    let mut used = HashSet::new();
    let mut ans = vec![];
    dfs(&tree_edges, 0, n, &mut used, &edges, &mut ans);

    for a in ans {
        // puts!("{} {}", a.0, a.1);
        puts!("{} {}", a.0+1, a.1+1);
    }
}