#include using namespace std; // #include // using namespace atcoder; using ll = long long; #define reps(i, a, n) for(ll i = (a); i < (ll)(n); ++i) #define rep(i,n) reps(i,0,n) #define all(a) (a).begin(), (a).end(); #define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" << endl; //なんかstd::vectorの挙動を修正できるやつ /* alias */ using ull = unsigned long long; using ll = long long; using vi = vector; using vl = vector; using vll = vector; using vvi = vector; using vvl = vector; using vvll = vector; using vs = vector; using pii = pair; //UnionFind木 // @see https://zenn.dev/reputeless/books/standard-cpp-for-competitive-programming/viewer/union-find class UnionFind{ public: UnionFind() = default; // n 要素数 explicit UnionFind(size_t n) : m_parentsOrSize(n, -1){} int root(int i){ if (m_parentsOrSize[i] < 0) return i; // 経路圧縮 return (m_parentsOrSize[i] = root(m_parentsOrSize[i])); } void unite(int a, int b){ a = root(a); b = root(b); if(a != b){ // 小さいほうが子になる if(-m_parentsOrSize[a] < -m_parentsOrSize[b]) swap(a,b); m_parentsOrSize[a] += m_parentsOrSize[b]; m_parentsOrSize[b] = a; } } bool same(int a, int b){ return (root(a) == root(b));} // i が属するグループの要素数を返す int size(int i){return -m_parentsOrSize[root(i)];} private: // m_parentsOrSize[i]は i の親, // ただし root の場合は (-1 * そのグループに属する要素数) vi m_parentsOrSize; // using vi = vector }; int main(){ int n, q; cin >> n >> q; vi a(n); UnionFind uf(n); for(int i = 0; i < n; i++){ cin >> a[i]; if(a[i] != -1) { a[i]--; uf.unite(i,a[i]); } } rep(i, q){ int x, y; cin >> x >> y; x--; y--; if(uf.same(x,y)) cout << "Yes\n"; else cout << "No\n"; } }