use UnionFind as uf; struct UnionFind { par: Vec, chd: Vec, rank: Vec } impl UnionFind { pub fn new(n: usize) -> Self { UnionFind { par: (0..n).collect(), chd: (0..n).collect(), rank: vec![0; n] } } pub fn root(&mut self, x: usize) -> usize { if self.par[x] == x { return x; } else { self.par[x] = self.root(self.par[x]); return self.par[x]; } } pub fn is_same(&mut self, x: usize, y: usize) -> bool { return self.root(x) == self.root(y); } pub fn merge(&mut self, x: usize, y: usize) { let a = self.root(x); let b = self.root(y); if a != b { if self.rank[a] < self.rank[b] { self.par[a] = b; self.chd[b] = a; } else { self.par[b] = a; self.chd[a] = b; if self.rank[a] == self.rank[b] { self.rank[a] += 1; } } } } pub fn group(&self) -> usize { let mut res = 0; for i in 0..self.par.len() { if self.par[i] == i { res += 1 } } res } } fn main() { let (n, q): (usize, usize) = { // 定数個の整数を受け取り, 定数個の変数に束縛する let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap() ) }; let p: Vec = { // 可変個の整数を受け取り, xsに束縛する let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|x| x.parse().unwrap()) .collect() }; let mut uf = uf::new(n); for i in 0..n { if p[i] != -1 { uf.merge(i, p[i] as usize - 1); } } for _ in 0..q { let (a, b): (usize, usize) = { // 定数個の整数を受け取り, 定数個の変数に束縛する let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap() ) }; if uf.is_same(a - 1, b - 1) { println!("Yes"); } else { println!("No"); } } }