#include #include #define elif else if #define ll long long #define vll vector #define vec vector #define embk emplace_back #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, n, k) for (ll i = k; i < n; i++) #define all(a) a.begin(), a.end() #define YNeos(bool) (bool ? "Yes" : "No") using namespace std; using namespace atcoder; const ll INF = 1LL << 60; const ll mod = 998244353; const double pi = acos(-1); struct UnionFind { vector root, size; //コンストラクタ(初期化処理) UnionFind(ll n) : root(n), size(n, 1) { for (int i = 0; i < n; i++) root[i] = i; } //xの根を求める ll getroot (ll x) { if (root.at(x) == x) return x; else return root.at(x) = getroot(root.at(x)); //経路圧縮 } //xとyが同一グループか判定 bool issame(ll x, ll y) { return (getroot(x) == getroot(y)); } //xとyを繋げる void unite(ll x, ll y) { ll rootx = getroot(x), rooty = getroot(y); //xとyの根を求める if (rootx == rooty) return; //根が同じなら何もしない //union by size if (size.at(rootx) < size.at(rooty)) swap(rootx, rooty); //サイズの大小を保障 root.at(rooty) = rootx; size.at(rootx) += size.at(rooty); return; } //uniteを与えられたグラフに対して一気に行う void unite_graph(vector u, vector v) { for(int i = 0; i < u.size(); i++) unite(u[i], v[i]); } //xに含まれる頂点数を求める ll getsize(ll x) { return size.at(root.at(x)); } //連結成分の数を求める ll getunion() { ll uni = 0; for (int i = 0; i < root.size(); i++) if (root.at(i) == i) uni++; return uni; } }; int main() { ll n, q; cin >> n >> q; vll p(n); rep(i, n) cin >> p[i]; UnionFind UF(n); rep(i, n) { if (p[i] != -1) UF.unite(i, p[i]-1); } rep(itr, q) { ll a, b; cin >> a >> b; cout << YNeos(UF.issame(a-1, b-1)) << endl;; } }