#include #include #include using namespace std; int main() { int n, q; cin >> n >> q; vector p(n + 1); for (int i = 0; i < n; ++i) { cin >> p[i]; } auto f = [&](int k) { int t = k; while (p[t] > 0) { t = p[t]; } while (p[k] > 0) { int k2 = k; k = p[k]; p[k2] = t; } return t; }; while (q--) { int a, b; cin >> a >> b; cout << (f(a) == f(b)? "Yes": "No") << '\n'; } }