struct BipartitieMatching { graph: Vec>, left: usize, right: usize, } impl BipartitieMatching { fn new(left: usize, right: usize) -> Self { assert!(left > 0 && right > 0); BipartitieMatching { graph: vec![vec![]; left], left: left, right: right, } } fn add_edge(&mut self, a: usize, b: usize) { assert!(a < self.left && b ], depth: &mut [u32]) { let mut que = std::collections::VecDeque::new(); for (v, (&used, depth)) in used.iter().zip(depth.iter_mut()).enumerate() { if !used { *depth = 0; que.push_back(v); } } while let Some(v) = que.pop_front() { let d = depth[v] + 1; for &u in self.graph[v].iter() { if let Some(k) = assign[u] { if depth[k] > d { depth[k] = d; que.push_back(k); } } } } } fn dfs(&self, v: usize, it: &mut [usize], used: &mut [bool], assign: &mut [Option], depth: &[u32]) -> bool { let d = depth[v] + 1; for (k, &u) in self.graph[v].iter().enumerate().skip(it[v]) { let ok = assign[u].map_or(true, |k| { assert!(used[k]); depth[k] == d && self.dfs(k, it, used, assign, depth) }); if ok { assign[u] = Some(v); used[v] = true; return true; } it[v] = k + 1; } false } fn solve(&self) -> Vec<(usize, usize)> { let mut used = vec![false; self.left]; let mut assign = vec![None; self.right]; let mut depth = Vec::with_capacity(self.left); let mut it = Vec::with_capacity(self.left); loop { depth.clear(); depth.resize(self.left, std::u32::MAX / 2); self.bfs(&used, &assign, &mut depth); it.clear(); it.resize(self.left, 0); let mut update = false; for v in 0..self.left { if !used[v] { update |= self.dfs(v, &mut it, &mut used, &mut assign, &depth); } } if !update { break; } } let mut ans = vec![]; for (r, a) in assign.into_iter().enumerate() { if let Some(l) = a { ans.push((l, r)); } } ans } } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 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)*} }; } 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)*} }; } 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::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- fn run() { input! { n: usize, e: [(usize1, usize1); n], } let mut g = BipartitieMatching::new(n, n); for (i, &(a, b)) in e.iter().enumerate() { assert!(a != b); g.add_edge(i, a); g.add_edge(i, b); } let f = g.solve(); if f.len() != n { println!("No"); return; } use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); writeln!(out, "Yes").ok(); let mut ans = vec![0; n]; for e in f { ans[e.0] = e.1 + 1; } for a in ans { writeln!(out, "{}", a).ok(); } } fn main() { run(); }