#![allow(non_snake_case)] #![allow(unused_imports)] #![allow(unused_macros)] #![allow(clippy::needless_range_loop)] #![allow(clippy::comparison_chain)] #![allow(clippy::nonminimal_bool)] #![allow(clippy::neg_multiply)] #![allow(dead_code)] use std::cmp::Reverse; use std::collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque}; #[derive(Default)] struct Solver {} impl Solver { fn solve(&mut self) { input! { N: usize, Q: usize, P: [isize; N], AB: [(usize1, usize1); Q] } let mut uf = UnionFind::new(N); for (i, &p) in P.iter().enumerate() { if p != -1 { uf.unite(i, p as usize - 1); } } for &(a, b) in &AB { if uf.is_same(a, b) { println!("Yes"); } else { println!("No"); } } } } fn join_to_string(v: &[T], sep: &str) -> String { v.iter() .fold("".to_string(), |s, x| s + x.to_string().as_str() + sep) } #[derive(Debug, Clone)] struct UnionFind { parent: Vec, size: usize, } impl UnionFind { fn new(n: usize) -> Self { UnionFind { parent: vec![-1; n], size: n, } } fn find(&mut self, x: usize) -> usize { if self.parent[x] < 0 { return x; } let root = self.find(self.parent[x] as usize); self.parent[x] = root as isize; root } fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> { let root_x = self.find(x); let root_y = self.find(y); if root_x == root_y { return None; } let size_x = -self.parent[root_x]; let size_y = -self.parent[root_y]; self.size -= 1; if size_x >= size_y { self.parent[root_x] -= size_y; self.parent[root_y] = root_x as isize; Some((root_x, root_y)) } else { self.parent[root_y] -= size_x; self.parent[root_x] = root_y as isize; Some((root_y, root_x)) } } fn is_same(&mut self, x: usize, y: usize) -> bool { self.find(x) == self.find(y) } fn is_root(&mut self, x: usize) -> bool { self.find(x) == x } fn get_union_size(&mut self, x: usize) -> usize { let root = self.find(x); -self.parent[root] as usize } fn get_size(&self) -> usize { self.size } fn roots(&self) -> Vec { (0..self.parent.len()) .filter(|i| self.parent[*i] < 0) .collect::>() } fn members(&mut self, x: usize) -> Vec { let root = self.find(x); (0..self.parent.len()) .filter(|i| self.find(*i) == root) .collect::>() } fn all_group_members(&mut self) -> BTreeMap> { let mut groups_map: BTreeMap> = BTreeMap::new(); for x in 0..self.parent.len() { let r = self.find(x); groups_map.entry(r).or_default().push(x); } groups_map } } fn main() { std::thread::Builder::new() .stack_size(128 * 1024 * 1024) .spawn(|| Solver::default().solve()) .unwrap() .join() .unwrap(); } #[macro_export] macro_rules! input { () => {}; (mut $var:ident: $t:tt, $($rest:tt)*) => { let mut $var = __input_inner!($t); input!($($rest)*) }; ($var:ident: $t:tt, $($rest:tt)*) => { let $var = __input_inner!($t); input!($($rest)*) }; (mut $var:ident: $t:tt) => { let mut $var = __input_inner!($t); }; ($var:ident: $t:tt) => { let $var = __input_inner!($t); }; } #[macro_export] macro_rules! __input_inner { (($($t:tt),*)) => { ($(__input_inner!($t)),*) }; ([$t:tt; $n:expr]) => { (0..$n).map(|_| __input_inner!($t)).collect::>() }; ([$t:tt]) => {{ let n = __input_inner!(usize); (0..n).map(|_| __input_inner!($t)).collect::>() }}; (chars) => { __input_inner!(String).chars().collect::>() }; (bytes) => { __input_inner!(String).into_bytes() }; (usize1) => { __input_inner!(usize) - 1 }; ($t:ty) => { $crate::read::<$t>() }; } #[macro_export] macro_rules! println { () => { $crate::write(|w| { use std::io::Write; std::writeln!(w).unwrap() }) }; ($($arg:tt)*) => { $crate::write(|w| { use std::io::Write; std::writeln!(w, $($arg)*).unwrap() }) }; } #[macro_export] macro_rules! print { ($($arg:tt)*) => { $crate::write(|w| { use std::io::Write; std::write!(w, $($arg)*).unwrap() }) }; } #[macro_export] macro_rules! flush { () => { $crate::write(|w| { use std::io::Write; w.flush().unwrap() }) }; } pub fn read() -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, { use std::cell::RefCell; use std::io::*; thread_local! { pub static STDIN: RefCell> = RefCell::new(stdin().lock()); } STDIN.with(|r| { let mut r = r.borrow_mut(); let mut s = vec![]; loop { let buf = r.fill_buf().unwrap(); if buf.is_empty() { break; } if let Some(i) = buf.iter().position(u8::is_ascii_whitespace) { s.extend_from_slice(&buf[..i]); r.consume(i + 1); if !s.is_empty() { break; } } else { s.extend_from_slice(buf); let n = buf.len(); r.consume(n); } } std::str::from_utf8(&s).unwrap().parse().unwrap() }) } pub fn write(f: F) where F: FnOnce(&mut std::io::BufWriter), { use std::cell::RefCell; use std::io::*; thread_local! { pub static STDOUT: RefCell>> = RefCell::new(BufWriter::new(stdout().lock())); } STDOUT.with(|w| f(&mut w.borrow_mut())) }