-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProblemD.cpp
58 lines (54 loc) · 1.43 KB
/
ProblemD.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
57
58
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int MAXN = 150005;
int t, n, m, dfn[MAXN], siz[MAXN], pos;
vector<int> g[MAXN]; bool fa[MAXN], vis[MAXN];
inline int read(){
register int x = 0, t = 1;
register int ch = getchar();
while(ch < '0' || ch > '9'){
if(ch == '-') t = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * t;
}
void dfs(int u) {
dfn[u] = ++pos;
for(int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if(vis[v]) continue;
vis[v] = true;
dfs(v);
siz[u] += siz[v];
}
}
int main() {
t = read();
while(t--) {
int n = read(), m = read(); pos = 0;
for(int i = 1; i <= n; i++) { g[i].clear(); fa[i] = vis[i] = false; siz[i] = 1; }
for(int i = 0; i < n - 1; i++) {
int x = read(), y = read();
g[y].push_back(x);
g[x].push_back(y);
fa[x] = true;
}
int root;
for(int i = 1; i <= n; i++) if(!fa[i]) { root = i; break; }
vis[root] = true;
dfs(root);
for(int i = 0; i < m; i++) {
int x = read(), y = read();
int left = dfn[y], right = dfn[y] + siz[y] - 1;
if(dfn[x] >= left && dfn[x] <= right) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}