#include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++) using ll = long long; using Graph = vector>; struct UnionFind { vector par; UnionFind(int n) : par(n, -1) {} int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } void unite(int x, int y) { x = root(x); y = root(y); if (x != y) { if (par[y] < par[x]) swap(x, y); par[x] += par[y]; par[y] = x; } } bool same(int x, int y) { return root(x) == root(y); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N, Q; cin >> N >> Q; vector P(N); for (int i = 0; i < N; i++) { cin >> P[i]; P[i]--; } UnionFind uf(N); for (int i = 0; i < N; i++) { if (P[i] != -2) uf.unite(i, P[i]); } while (Q--) { int A, B; cin >> A >> B; A--; B--; if (uf.same(A, B)) cout << "Yes" << "\n"; else cout << "No" << "\n"; } return 0; }