#include using namespace std; int main() { int N, Q; cin >> N >> Q; vector P(N + 1), root(N + 1, -1); auto dfs = [&](auto dfs, int x) -> int { if (root.at(x) != -1) return root.at(x); return root.at(x) = dfs(dfs, P.at(x)); }; for (int i = 1; i <= N; i++) { cin >> P.at(i); if (P.at(i) == -1) root.at(i) = i; } for (int i = 1; i <= N; i++) { if (root.at(i) != -1) continue; root.at(i) = dfs(dfs, P.at(i)); } for (int i = 0; i < Q; i++) { int A, B; cin >> A >> B; cout << (root.at(A) == root.at(B) ? "Yes" : "No") << endl; } }