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)

// https://atcoder.jp/contests/agc035/tasks/agc035_b
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

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

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

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

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

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

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

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

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

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

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

struct UnionFind {
      n: usize,
      parent: Vec<usize>,
      count: Vec<usize>
}

impl UnionFind {
     fn new(n: usize) -> Self {
           let mut p = vec![0; n];
           for i in 0..n {
                 p[i] = i;
           }
           UnionFind { n: n, parent: p, count: vec![1; n] }
     }

     fn find(&mut self, a: usize) -> usize {
           if self.parent[a] == a {
                 return a;
           }
           let par = self.parent[a];
           self.parent[a] = self.find(par);
           self.parent[a]
     }

     fn unite(&mut self, a: usize, b: usize) {
           let a = self.find(a);
           let b = self.find(b);
           if a == b {
                 return;
           }
           let ca = self.count[a];
           let cb = self.count[b];
           let total = ca + cb;
           self.count[b] = total;
           self.count[a] = total;
           if ca < cb {
                 self.parent[a] = b;
           } else {
                 self.parent[b] = a;
           }
     }

     fn same(&mut self, a: usize, b: usize) -> bool {
           self.find(a) == self.find(b)
     }

     fn count(&mut self, a: usize) -> usize {
           let a = self.find(a);
           self.count[a]
     }
}

type Graph = Vec<Vec<usize>>;

fn dfs_cpo(g: &Graph, root: usize) -> (Vec<usize>, Vec<Option<usize>>, Vec<usize>) {
    let n = g.len();
    struct Cpo {
        count: Vec<usize>,
        parent: Vec<Option<usize>>,
        order: Vec<usize>,
        oidx: usize
    };
    let mut cpo = Cpo {
        count: vec![0; n],
        parent: vec![None; n],
        order: vec![0; n],
        oidx: 0
    };

    fn dfs(g: &Graph, now: usize, par: Option<usize>, cpo: &mut Cpo) {
        for &to in &g[now] {
            if Some(to) != par {
                dfs(g, to, Some(now), cpo);
            }
        }
        cpo.count[now] = 1;
        for &to in &g[now] {
            if Some(to) != par {
                cpo.count[now] += cpo.count[to];
            }
        }
        cpo.parent[now] = par;
        cpo.order[cpo.oidx] = now;
        cpo.oidx += 1;

    }
    dfs(&g, root, None, &mut cpo);
    (cpo.count, cpo.parent, cpo.order)
}
//===

fn main() {
    input! {
        n: usize, m: usize,
        edges: [(usize1, usize1); m]
    };
    if m % 2 == 1 {
        println!("-1");
        return;
    }

    let mut uf = UnionFind::new(n);
    let mut tree = vec![vec![]; n];
    let mut deg = vec![0; n];
    let mut ans = vec![];

    let mut edgemap = HashMap::new();
    for (idx, &(u,v)) in edges.iter().enumerate() {
        if uf.same(u, v) {
            deg[u] += 1;
            ans.push((u+1, v+1));
        } else {
            uf.unite(u, v);
            tree[u].push(v);
            tree[v].push(u);
            edgemap.insert((u,v), idx);
            edgemap.insert((v,u), idx);
        }
    }

    let mut visited = vec![0; m];
    let (count, parent, order) = dfs_cpo(&tree, 0);
    for i in 0..n {
        let v = order[i];
        let mut peid = m;
        for &to in &tree[v] {
            let eid = *edgemap.get(&(v, to)).unwrap();
            if visited[eid] != 0 {
                continue;
            }
            if Some(to) == parent[v] {
                peid = eid;
            } else {
                deg[v] += 1;
                ans.push((v+1, to+1));
                visited[eid] = ifv!(v < to, 1, -1);
            }
        }
        if let Some(p) = parent[v] {
            if deg[v] % 2 == 1 {
                deg[v] += 1;
                ans.push((v+1, p+1));
                visited[peid] = ifv!(v < p, 1, -1);
            }
        } else {
            assert!(i == n-1, deg[v] % 2 == 0);
        }
    }

    for &(u, v) in &ans {
        println!("{} {}", u, v);
    }

}