#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #define rep(i,n) for(int i=0;i<(n);i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) ((int)(x).size()) #define pb push_back using ll = long long; using namespace std; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b par, rnk; void init(int N){ par.resize(N); rnk.assign(N,0); rep(i,N) {par[i] = i;} } int root(int x) { return par[x] == x? x : par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y){ x = root(x); y = root(y); if(x==y) return; if(rnk[x] < rnk[y]) par[x] = y; else{ par[y] = x; if(rnk[x] == rnk[y]) rnk[x]++; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,Q; cin >> N >> Q; init(N); rep(i,N){ int a; cin >> a; if(a!=-1) unite(i,a-1); } while(Q--){ int a,b; cin >> a >> b; a--; b--; if(same(a,b)) cout << "Yes\n"; else cout << "No\n"; } return 0; }