#include using namespace std; using ll = long long; #define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i) #define all(a) (a).begin(), (a).end() struct UnionFind { vector par, siz; int v, group; UnionFind(int n) { par = vector(n, -1); siz = vector(n, 1); v = n; group = n; } int root(int x) { if (par[x] == -1) return x; else return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); par[y] = x; siz[x] += siz[y]; group--; return true; } int size(int x) { return siz[root(x)]; } }; int main() { cin.tie(nullptr); int N, Q; cin >> N >> Q; UnionFind uf(N); rep(i, 0, N) { int P; cin >> P; if (P == -1) continue; P--; uf.unite(i, P); } rep(query, 0, Q) { int A, B; cin >> A >> B; A--, B--; if (uf.same(A, B)) cout << "Yes\n"; else cout << "No\n"; } }