#include using namespace std; struct UnionFind { vector par; vector size; UnionFind() {} UnionFind(int n) { par.resize(n); size.resize(n,1); for(int i = 0; i < n; i++) { par[i] = i; } } int find(int x) { if(par[x] == x) { return x; } return par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } int consize(int x) { return size[find(x)]; } bool unite(int x, int y) { x = find(x); y = find(y); if(x == y) { return false; } if(size[x] > size[y]) { swap(x,y); } par[x] = y; size[y] += size[x]; return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N,Q; cin >> N >> Q; UnionFind uf(N); for(int i = 0; i < N; i++) { int P; cin >> P; P--; if(P >= 0) uf.unite(i,P); } for(int i = 0; i < Q; i++) { int a,b; cin >> a >> b; a--; b--; cout << ((uf.same(a,b))?"Yes":"No") << "\n"; } }