#![allow(non_snake_case, unused_imports)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; } let N = input!(usize); let Q = input!(usize); let mut uf = unionfind::new(N); for i in 0..N { let p = input!(isize); if p == -1 { continue; } let q = p as usize - 1; uf.unite(i, q); } for _ in 0..Q { let a = input!(usize) - 1; let b = input!(usize) - 1; if uf.issame(a, b) { writeln!(out, "Yes"); } else { writeln!(out, "No"); } } } pub mod unionfind { pub struct UnionFind { n: usize, parent: Vec, rank: Vec, size: Vec, } /// 大きさ n のUnionFindを生成する. pub fn new(n: usize) -> UnionFind { let uf = UnionFind { n: n, parent: vec![n; n], rank: vec![0; n], size: vec![1; n], }; uf } impl UnionFind { /// x と y が同じ集合に含まれるかを検索する. pub fn issame(&mut self, x: usize, y: usize) -> bool { return self.root(x) == self.root(y); } /// x と y が含まれる集合をそれぞれ合併する. pub fn unite(&mut self, mut x: usize, mut y: usize) { x = self.root(x); y = self.root(y); if x != y { if self.rank[x] < self.rank[y] { std::mem::swap(&mut x, &mut y); } self.parent[y] = x; if self.rank[x] == self.rank[y] { self.rank[x] += 1; } self.size[x] += self.size[y]; } } /// x が含まれる集合の大きさを求める. pub fn size(&mut self, x: usize) -> usize { let r = self.root(x); return self.size[r]; } fn root(&mut self, x: usize) -> usize { if self.parent[x] == self.n { return x; } else { self.parent[x] = self.root(self.parent[x]); return self.parent[x]; } } } } struct Scanner { reader: R, buf_str: Vec, buf_iter: str::SplitWhitespace<'static>, } impl Scanner { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }