#include #include #include using namespace std; class UnionFind { protected: vector root_of, size_of; size_t group; virtual size_t retrieve_true_root_of(const size_t index) noexcept { return root_of[index] == index ? index : root_of[index] = retrieve_true_root_of(root_of[index]); } public: UnionFind(const size_t N) : root_of(N), size_of(N, 1), group(N) { for (size_t i = 0; i != N; ++i) root_of[i] = i; } virtual bool is_connected(const size_t a, const size_t b) noexcept { return retrieve_true_root_of(a) == retrieve_true_root_of(b); } virtual bool merge(const size_t a, const size_t b) noexcept { if (retrieve_true_root_of(a) != retrieve_true_root_of(b)) { size_of[root_of[a]] += size_of[root_of[b]]; root_of[b] = root_of[root_of[b]] = root_of[a]; --group; return true; } else return false; } size_t size_of_group_of(const size_t index) noexcept { return size_of[retrieve_true_root_of(index)]; } size_t groups() const noexcept { return group; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int32_t N, Q, i; cin >> N >> Q; vector P(N), A(Q), B(Q); for (i = 0; i != N; ++i) cin >> P[i]; for (i = 0; i != Q; ++i) cin >> A[i] >> B[i]; UnionFind uf(N); for (i = 0; i != N; ++i) if (P[i] != -1) uf.merge(i, P[i] - 1); for (i = 0; i != Q; ++i) { if (uf.is_connected(A[i] - 1, B[i] - 1)) cout << "Yes\n"; else cout << "No\n"; } return 0; }