#![allow(unused_imports)] #![allow(non_snake_case, unused)] // ---------- begin input macro ---------- // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より 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)*} }; } 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)*} }; } 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::>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::>() }; ($next:expr, usize1) => { read_value!($next, usize) - 1 }; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- use std::cmp::*; use std::collections::*; use std::ops::*; use std::marker::*; const INF: i64 = std::i64::MAX/100; const MOD: i64 = 1_000_000_007; // const MOD: i64 = 998_244_353; pub struct UnionFind { data: Vec, } impl UnionFind { pub fn new(n: usize) -> Self{ UnionFind { data: vec![-1;n] } } pub fn root(&mut self, x: usize) -> usize{ if self.data[x]<0 { x } else { let p = self.data[x] as usize; self.data[x] = self.root(p) as i32; self.data[x] as usize } } pub fn unite(&mut self, x: usize, y: usize){ let rx = self.root(x); let ry = self.root(y); if rx!=ry { let (rx,ry) = if self.data[rx]<=self.data[ry] { (rx,ry) } else{ (ry,rx) }; self.data[rx] += self.data[ry]; self.data[ry] = rx as i32; } } pub fn same(&mut self, x: usize, y: usize) -> bool{ self.root(x) == self.root(y) } pub fn size(&mut self, x: usize) -> i32 { let r = self.root(x); - self.data[r] } } fn main() { input! { n: usize, m: usize, uv: [(usize,usize);m], } let mut uf = UnionFind::new(n); let mut e = vec![vec![];n]; for &(u,v) in &uv { uf.unite(u-1,v-1); e[u-1].push(v-1); } let mut count = 0; let mut map = HashMap::new(); for i in 0..n { if uf.root(i)==i { map.insert(i,count); count += 1; } } let mut edge = vec![vec![];count]; let mut set = vec![HashSet::new();count]; for &(u,v) in &uv { let idx = *map.get(&(uf.root(u-1))).unwrap(); edge[idx].push((u-1,v-1)); set[idx].insert(u-1); set[idx].insert(v-1); } let mut ans = vec![]; let mut inner = vec![0;n]; for i in 0..count { let mut ret = vec![]; for &(u,v) in &edge[i] { inner[v] += 1; } let mut q = VecDeque::new(); let mut l = vec![]; for &s in set[i].iter() { l.push(s); if inner[s]==0 { q.push_back(s); } } while let Some(v) = q.pop_front() { ret.push(v); for &e in &e[v] { inner[e] -= 1; if inner[e]==0 { q.push_back(e); } } } if ret.len()==set[i].len() { for j in 0..ret.len()-1 { ans.push((ret[j],ret[j+1])); } } else { for j in 0..l.len() { ans.push((l[j],l[(j+1)%l.len()])); } } } println!("{}",ans.len()); for &(s,t) in &ans { println!("{} {}",s+1,t+1); } }