-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathL2-025_分而治之.cpp
56 lines (51 loc) · 1.01 KB
/
L2-025_分而治之.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
using namespace std;
int FloodFill(vector<vector<int>> &g, vector<bool> &vis) {
int cnt = 0;
for (int i = 1; i < g.size(); i++) {
if (!vis[i]) {
cnt++;
vector<int> q;
q.push_back(i);
vis[i] = true;
while (!q.empty()) {
int u = q.back();
q.pop_back();
for (int j = 0; j < g[u].size(); j++) {
int v = g[u][j];
if (!vis[v]) {
q.push_back(v);
vis[v] = true;
}
}
}
}
}
return cnt;
}
int main() {
ios::sync_with_stdio(false);
int n, m, k, x, y;
cin >> n >> m;
vector<vector<int>> g(n + 1, vector<int>());
vector<bool> vis(n + 1, false);
for (int i = 0; i < m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
cin >> k;
for (int i = 0; i < k; i++) {
cin >> n;
for (int j = 0; j < n; j++) {
cin >> x;
vis[x] = true;
}
int cnt = FloodFill(g, vis);
fill(vis.begin(), vis.end(), false);
int remains = g.size() - 1 - n;
cout << (cnt == remains ? "YES" : "NO") << endl;
}
return 0;
}