#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
const int maxn = 12*1e4+5;
int q,n,m;
vector<int>g[maxn],v[maxn];
int s[maxn],t[maxn],used[maxn];
int jump[maxn][21],lvl[maxn];
bool ans;
void precom()
{
for(int i=1;i<=n;i++)
{
g[i].clear();
used[i] = 0;
v[i].clear();
}
}
void read()
{
cin >> n;
precom();
int a,b;
for(int i=1;i<n;i++)
{
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> m;
for(int i=1;i<=m;i++)
{
cin >> s[i] >> t[i];
}
}
void dfs_make(int beg, int par)
{
jump[beg][0] = par;
lvl[beg] = lvl[par] + 1;
for(int nb:g[beg]) if(nb!=par) dfs_make(nb,beg);
}
void make_lca()
{
lvl[1] = 0;
dfs_make(1,1);
for(int j=1;j<=20;j++)
{
for(int i=1;i<=n;i++)
{
jump[i][j] = jump[jump[i][j-1]][j-1];
}
}
}
int get_lca(int a, int b)
{
int raz = abs(lvl[a]-lvl[b]);
for(int j=20;j>=0;j--)
{
if(raz&(1<<j))
{
if(lvl[a]>lvl[b]) a = jump[a][j];
else b = jump[b][j];
}
}
if(a==b) return a;
for(int j=20;j>=0;j--)
{
if(jump[a][j]!=jump[b][j])
{
a = jump[a][j];
b = jump[b][j];
}
}
return jump[a][0];
}
bool check(int a, int b, int c)
{
int lca = get_lca(a,b);
if(get_lca(c,lca)!=lca) return false;
return (get_lca(c,a)==c || get_lca(c,b) == c);
}
void dfs(int beg)
{
used[beg] = 1;
for(int nb:v[beg])
{
if(used[nb]==1) ans = false;
if(!used[nb]) dfs(nb);
}
used[beg] = 2;
}
void solve()
{
make_lca();
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
{
if(i==j) continue;
int chis = 0;
if(check(s[i],t[i],s[j])) chis += 2;
if(check(s[i],t[i],t[j])) chis += 1;
if((chis&2)) v[j].push_back(i);
if((chis&1)) v[i].push_back(j);
}
}
ans = true;
for(int i=1;i<=m;i++) used[i] = 0;
for(int i=1;i<=m;i++)
{
if(!used[i]) dfs(i);
}
if(ans) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> q;
for(int i=1;i<=q;i++)
{
read();
solve();
}
return 0;
}
/*
1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
4
1 5
2 6
3 7
4 8
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11500 KB |
Output is correct |
2 |
Correct |
2 ms |
11440 KB |
Output is correct |
3 |
Correct |
2 ms |
11356 KB |
Output is correct |
4 |
Correct |
11 ms |
11764 KB |
Output is correct |
5 |
Correct |
17 ms |
12256 KB |
Output is correct |
6 |
Correct |
3 ms |
11352 KB |
Output is correct |
7 |
Correct |
3 ms |
11508 KB |
Output is correct |
8 |
Correct |
41 ms |
11860 KB |
Output is correct |
9 |
Correct |
1419 ms |
15076 KB |
Output is correct |
10 |
Correct |
116 ms |
30020 KB |
Output is correct |
11 |
Correct |
16 ms |
11504 KB |
Output is correct |
12 |
Correct |
481 ms |
12368 KB |
Output is correct |
13 |
Execution timed out |
5051 ms |
29484 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
3 ms |
11356 KB |
Output is correct |
4 |
Correct |
3 ms |
11608 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
3 ms |
11356 KB |
Output is correct |
7 |
Correct |
3 ms |
11548 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
4 ms |
11352 KB |
Output is correct |
10 |
Correct |
3 ms |
11356 KB |
Output is correct |
11 |
Correct |
3 ms |
11356 KB |
Output is correct |
12 |
Correct |
3 ms |
11524 KB |
Output is correct |
13 |
Correct |
3 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
3 ms |
11356 KB |
Output is correct |
4 |
Correct |
3 ms |
11608 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
3 ms |
11356 KB |
Output is correct |
7 |
Correct |
3 ms |
11548 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
4 ms |
11352 KB |
Output is correct |
10 |
Correct |
3 ms |
11356 KB |
Output is correct |
11 |
Correct |
3 ms |
11356 KB |
Output is correct |
12 |
Correct |
3 ms |
11524 KB |
Output is correct |
13 |
Correct |
3 ms |
11356 KB |
Output is correct |
14 |
Correct |
3 ms |
11352 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
3 ms |
11356 KB |
Output is correct |
17 |
Correct |
3 ms |
11356 KB |
Output is correct |
18 |
Correct |
3 ms |
11512 KB |
Output is correct |
19 |
Correct |
2 ms |
11352 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
3 ms |
11356 KB |
Output is correct |
22 |
Correct |
3 ms |
11356 KB |
Output is correct |
23 |
Correct |
2 ms |
11356 KB |
Output is correct |
24 |
Correct |
2 ms |
11356 KB |
Output is correct |
25 |
Correct |
4 ms |
11356 KB |
Output is correct |
26 |
Correct |
2 ms |
11356 KB |
Output is correct |
27 |
Correct |
3 ms |
11356 KB |
Output is correct |
28 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
3 ms |
11356 KB |
Output is correct |
4 |
Correct |
3 ms |
11608 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
3 ms |
11356 KB |
Output is correct |
7 |
Correct |
3 ms |
11548 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
4 ms |
11352 KB |
Output is correct |
10 |
Correct |
3 ms |
11356 KB |
Output is correct |
11 |
Correct |
3 ms |
11356 KB |
Output is correct |
12 |
Correct |
3 ms |
11524 KB |
Output is correct |
13 |
Correct |
3 ms |
11356 KB |
Output is correct |
14 |
Correct |
3 ms |
11352 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
3 ms |
11356 KB |
Output is correct |
17 |
Correct |
3 ms |
11356 KB |
Output is correct |
18 |
Correct |
3 ms |
11512 KB |
Output is correct |
19 |
Correct |
2 ms |
11352 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
3 ms |
11356 KB |
Output is correct |
22 |
Correct |
3 ms |
11356 KB |
Output is correct |
23 |
Correct |
2 ms |
11356 KB |
Output is correct |
24 |
Correct |
2 ms |
11356 KB |
Output is correct |
25 |
Correct |
4 ms |
11356 KB |
Output is correct |
26 |
Correct |
2 ms |
11356 KB |
Output is correct |
27 |
Correct |
3 ms |
11356 KB |
Output is correct |
28 |
Correct |
2 ms |
11356 KB |
Output is correct |
29 |
Correct |
42 ms |
11604 KB |
Output is correct |
30 |
Correct |
28 ms |
11548 KB |
Output is correct |
31 |
Correct |
27 ms |
11356 KB |
Output is correct |
32 |
Correct |
18 ms |
11352 KB |
Output is correct |
33 |
Correct |
4 ms |
11356 KB |
Output is correct |
34 |
Correct |
60 ms |
11544 KB |
Output is correct |
35 |
Correct |
73 ms |
11356 KB |
Output is correct |
36 |
Correct |
45 ms |
11548 KB |
Output is correct |
37 |
Correct |
28 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
3 ms |
11356 KB |
Output is correct |
4 |
Correct |
3 ms |
11608 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
3 ms |
11356 KB |
Output is correct |
7 |
Correct |
3 ms |
11548 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
4 ms |
11352 KB |
Output is correct |
10 |
Correct |
3 ms |
11356 KB |
Output is correct |
11 |
Correct |
3 ms |
11356 KB |
Output is correct |
12 |
Correct |
3 ms |
11524 KB |
Output is correct |
13 |
Correct |
3 ms |
11356 KB |
Output is correct |
14 |
Correct |
3 ms |
11352 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
3 ms |
11356 KB |
Output is correct |
17 |
Correct |
3 ms |
11356 KB |
Output is correct |
18 |
Correct |
3 ms |
11512 KB |
Output is correct |
19 |
Correct |
2 ms |
11352 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
3 ms |
11356 KB |
Output is correct |
22 |
Correct |
3 ms |
11356 KB |
Output is correct |
23 |
Correct |
2 ms |
11356 KB |
Output is correct |
24 |
Correct |
2 ms |
11356 KB |
Output is correct |
25 |
Correct |
4 ms |
11356 KB |
Output is correct |
26 |
Correct |
2 ms |
11356 KB |
Output is correct |
27 |
Correct |
3 ms |
11356 KB |
Output is correct |
28 |
Correct |
2 ms |
11356 KB |
Output is correct |
29 |
Correct |
42 ms |
11604 KB |
Output is correct |
30 |
Correct |
28 ms |
11548 KB |
Output is correct |
31 |
Correct |
27 ms |
11356 KB |
Output is correct |
32 |
Correct |
18 ms |
11352 KB |
Output is correct |
33 |
Correct |
4 ms |
11356 KB |
Output is correct |
34 |
Correct |
60 ms |
11544 KB |
Output is correct |
35 |
Correct |
73 ms |
11356 KB |
Output is correct |
36 |
Correct |
45 ms |
11548 KB |
Output is correct |
37 |
Correct |
28 ms |
11356 KB |
Output is correct |
38 |
Correct |
1385 ms |
15080 KB |
Output is correct |
39 |
Correct |
109 ms |
29776 KB |
Output is correct |
40 |
Correct |
1812 ms |
14136 KB |
Output is correct |
41 |
Correct |
1528 ms |
12972 KB |
Output is correct |
42 |
Correct |
1460 ms |
14264 KB |
Output is correct |
43 |
Correct |
33 ms |
12884 KB |
Output is correct |
44 |
Correct |
1430 ms |
12108 KB |
Output is correct |
45 |
Correct |
158 ms |
23036 KB |
Output is correct |
46 |
Correct |
155 ms |
22916 KB |
Output is correct |
47 |
Correct |
199 ms |
25680 KB |
Output is correct |
48 |
Correct |
189 ms |
25680 KB |
Output is correct |
49 |
Correct |
209 ms |
23120 KB |
Output is correct |
50 |
Correct |
198 ms |
23120 KB |
Output is correct |
51 |
Correct |
48 ms |
23904 KB |
Output is correct |
52 |
Correct |
54 ms |
23884 KB |
Output is correct |
53 |
Correct |
1042 ms |
12068 KB |
Output is correct |
54 |
Correct |
118 ms |
22868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11492 KB |
Output is correct |
3 |
Correct |
3 ms |
11356 KB |
Output is correct |
4 |
Correct |
2 ms |
11356 KB |
Output is correct |
5 |
Correct |
15 ms |
11660 KB |
Output is correct |
6 |
Correct |
2 ms |
11456 KB |
Output is correct |
7 |
Correct |
2 ms |
11356 KB |
Output is correct |
8 |
Correct |
3 ms |
11356 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11352 KB |
Output is correct |
11 |
Correct |
3 ms |
11356 KB |
Output is correct |
12 |
Correct |
45 ms |
11356 KB |
Output is correct |
13 |
Correct |
572 ms |
12368 KB |
Output is correct |
14 |
Correct |
266 ms |
12368 KB |
Output is correct |
15 |
Correct |
461 ms |
11996 KB |
Output is correct |
16 |
Execution timed out |
5014 ms |
24044 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11500 KB |
Output is correct |
2 |
Correct |
2 ms |
11440 KB |
Output is correct |
3 |
Correct |
2 ms |
11356 KB |
Output is correct |
4 |
Correct |
11 ms |
11764 KB |
Output is correct |
5 |
Correct |
17 ms |
12256 KB |
Output is correct |
6 |
Correct |
3 ms |
11352 KB |
Output is correct |
7 |
Correct |
3 ms |
11508 KB |
Output is correct |
8 |
Correct |
41 ms |
11860 KB |
Output is correct |
9 |
Correct |
1419 ms |
15076 KB |
Output is correct |
10 |
Correct |
116 ms |
30020 KB |
Output is correct |
11 |
Correct |
16 ms |
11504 KB |
Output is correct |
12 |
Correct |
481 ms |
12368 KB |
Output is correct |
13 |
Execution timed out |
5051 ms |
29484 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |