Code
#include <bits/stdc++.h>
using namespace std;
class UnionFind {
public:
int n;
vector<int> parent;
UnionFind(int n) {
this->n = n;
parent.resize(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
public:
int find_set(int u) {
if (parent[u] == u) {
return u;
}
int root_x = find_set(parent[u]);
parent[u] = root_x;
return root_x;
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
parent[a] = b;
}
}
};
void solve() {
int n, m;
cin >> n >> m;
UnionFind dsu(n);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
dsu.union_sets(x, y);
}
int k;
cin >> k;
set<pair<int, int>> restricted;
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
restricted.insert({dsu.find_set(x), dsu.find_set(y)});
restricted.insert({dsu.find_set(y), dsu.find_set(x)});
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int p, q;
cin >> p >> q;
p--;
q--;
if (restricted.find({dsu.find_set(p), dsu.find_set(q)}) !=
restricted.end()) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
}
}
int main() {
solve();
return 0;
}