#include #include using namespace std; using ll = long long; #define rep(i,n) for(ll i=0;i<(n);++i) struct UnionFind{ vector par; vector size; UnionFind(ll n){ par.resize(n+1,-1); size.resize(n+1,1); } ll root(ll x){ if(par[x]==-1) return x; return par[x]=root(par[x]); } void unite(ll u,ll v){ ll RootU=root(u),RootV=root(v); if(RootU==RootV){ return; } if(size[RootU]> n >> q; UnionFind uf(n); rep(i,n){ ll p; cin >> p; if(p!=-1) uf.unite(i+1,p); } rep(i,q){ ll a,b; cin >> a >> b; cout << (uf.same(a,b)?"Yes":"No") << endl; } }