-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1328E.cpp
More file actions
executable file
·57 lines (49 loc) · 979 Bytes
/
Copy path1328E.cpp
File metadata and controls
executable file
·57 lines (49 loc) · 979 Bytes
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
57
// Problem Code: 1328E
#include <iostream>
#include <vector>
using namespace std;
int t = 0, root = 1;
vector<vector<int>> adj;
vector<int> d, p, tin, tout;
void dfs(int s, int parent = 0) {
tin[s] = t++;
p[s] = parent;
d[s] = 1 + d[parent];
for (int u: adj[s])
if (u != parent)
dfs(u, s);
tout[s] = t++;
}
string tree_query(int k, vector<int>& q) {
int far = root;
for (int s: q)
if (d[far] < d[s])
far = s;
for (int s: q)
if (s != far && s != root) {
s = p[s];
if (tin[s] > tin[far] || tout[s] < tin[far])
return "NO";
}
return "YES";
}
int main() {
int n, m, u, v, k;
cin >> n >> m;
adj = vector<vector<int>>(n + 1);
d = p = tin = tout = vector<int>(n + 1);
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(root);
for (int i = 0; i < m; i++) {
cin >> k;
vector<int> q(k);
for (int j = 0; j < k; j++)
cin >> q[j];
cout << tree_query(k, q) << endl;
}
return 0;
}