#[macro_export] macro_rules! setup { { mut $input:ident: SplitWhitespace $(,)? } => { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut $input = buf.split_whitespace(); }; } #[macro_export] macro_rules! parse_next { ($str_iter:expr) => { $str_iter.next().unwrap().parse().unwrap() }; } fn main() { setup! { mut input: SplitWhitespace }; let _n: usize = parse_next!(input); let m: usize = parse_next!(input); let s: usize = parse_next!(input); let g: usize = parse_next!(input); let mut f: Vec = vec![]; let mut t: Vec = vec![]; (0..m).for_each(|_| { f.push(parse_next!(input)); t.push(parse_next!(input)); }); let u: usize = parse_next!(input); let i: Vec = (0..u).map(|_| parse_next!(input)).collect(); let ans = match (|| { use std::collections::{HashSet, VecDeque}; let mut queue = VecDeque::new(); let mut visited = HashSet::new(); let mut depth = 0; { let root = s; queue.push_back(root); visited.insert(root); } while !queue.is_empty() { for _ in 0..queue.len() { let from = match queue.pop_front() { Some(value) => value, None => unreachable!(), }; if from == g { return Some(depth); } let children = (0..m) .filter(|&k| f[k] == from) .map(|k| t[k]) .chain((0..m).filter(|&k| t[k] == from).map(|k| f[k])) .filter(|&island| !i.contains(&island)) .collect::>(); for &to in children.iter() { if visited.contains(&to) { continue; } queue.push_back(to); visited.insert(to); } } depth += 1; } None })() { Some(_) => "Yes", None => "No", }; println!("{}", ans); }