#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=12e4+5;
vector <int> g[N],g2[N];
int p[N],d[N],jmp[20][N],cl[N];
void dfs(int v,int par){
p[v]=par;
jmp[0][v]=par;
for(auto to : g[v]){
if(!p[to]){
d[to]=d[v]+1;
dfs(to,v);
}
}
}
int lca(int v,int u){
if(d[v]<d[u])swap(v,u);
for(int k=19;k>=0;k--){
if(d[jmp[k][v]]>=d[u]){
v=jmp[k][v];
}
}
if(u==v)return u;
for(int k=19;k>=0;k--){
if(jmp[k][v]!=jmp[k][u]){
v=jmp[k][v];
u=jmp[k][u];
}
}
return p[v];
}
bool dfs2(int v){
cl[v]=1;
for(auto to : g2[v]){
if(cl[to]==1)return true;
if(!cl[to]){
if(dfs2(to))return true;
}
}
cl[v]=2;
return false;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int q;
cin>>q;
while(q--){
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
int m;
cin>>m;
vector <int> s(m+1),t(m+1),st(n+1),ed(n+1);
for(int i=1;i<=m;i++){
cin>>s[i]>>t[i];
st[s[i]]=i;
ed[t[i]]=i;
}
dfs(1,1);
for(int k=1;k<20;k++){
for(int i=1;i<=n;i++){
jmp[k][i]=jmp[k-1][jmp[k-1][i]];
}
}
for(int i=1;i<=m;i++){
int l=lca(s[i],t[i]);
int x=s[i];
while(x!=l){
if(st[x] && i!=st[x]){
g2[st[x]].pb(i);
}
if(ed[x] && i!=ed[x]){
g2[i].pb(ed[x]);
}
x=p[x];
}
x=t[i];
while(x!=l){
if(st[x] && i!=st[x]){
g2[st[x]].pb(i);
}
if(ed[x] && i!=ed[x]){
g2[i].pb(ed[x]);
}
x=p[x];
}
if(st[l] && st[l]!=i){
g2[st[l]].pb(i);
}
if(ed[l] && ed[l]!=i){
g2[i].pb(ed[l]);
}
}
bool ok=1;
for(int i=1;i<=m;i++){
if(!cl[i]){
if(dfs2(i))ok=0;
}
}
if(ok)cout<<"Yes\n";
else cout<<"No\n";
for(int i=1;i<=n;i++){
g[i].clear();
g2[i].clear();
p[i]=0;d[i]=0;cl[i]=0;
for(int k=0;k<20;k++)jmp[k][i]=0;
}
}
}
/*
1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
2
3 4
4 8
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27484 KB |
Output is correct |
2 |
Correct |
3 ms |
27484 KB |
Output is correct |
3 |
Correct |
3 ms |
27508 KB |
Output is correct |
4 |
Correct |
9 ms |
27880 KB |
Output is correct |
5 |
Correct |
17 ms |
28268 KB |
Output is correct |
6 |
Correct |
3 ms |
27484 KB |
Output is correct |
7 |
Correct |
4 ms |
27484 KB |
Output is correct |
8 |
Correct |
5 ms |
27740 KB |
Output is correct |
9 |
Correct |
50 ms |
33144 KB |
Output is correct |
10 |
Correct |
120 ms |
43092 KB |
Output is correct |
11 |
Correct |
7 ms |
27484 KB |
Output is correct |
12 |
Correct |
25 ms |
28716 KB |
Output is correct |
13 |
Correct |
104 ms |
100312 KB |
Output is correct |
14 |
Correct |
133 ms |
127996 KB |
Output is correct |
15 |
Runtime error |
2184 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27740 KB |
Output is correct |
2 |
Correct |
3 ms |
27488 KB |
Output is correct |
3 |
Correct |
4 ms |
27496 KB |
Output is correct |
4 |
Correct |
4 ms |
27744 KB |
Output is correct |
5 |
Correct |
4 ms |
27496 KB |
Output is correct |
6 |
Correct |
4 ms |
27492 KB |
Output is correct |
7 |
Correct |
4 ms |
27492 KB |
Output is correct |
8 |
Correct |
4 ms |
27492 KB |
Output is correct |
9 |
Correct |
4 ms |
27492 KB |
Output is correct |
10 |
Correct |
5 ms |
27492 KB |
Output is correct |
11 |
Correct |
4 ms |
27488 KB |
Output is correct |
12 |
Correct |
3 ms |
27484 KB |
Output is correct |
13 |
Correct |
3 ms |
27488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27740 KB |
Output is correct |
2 |
Correct |
3 ms |
27488 KB |
Output is correct |
3 |
Correct |
4 ms |
27496 KB |
Output is correct |
4 |
Correct |
4 ms |
27744 KB |
Output is correct |
5 |
Correct |
4 ms |
27496 KB |
Output is correct |
6 |
Correct |
4 ms |
27492 KB |
Output is correct |
7 |
Correct |
4 ms |
27492 KB |
Output is correct |
8 |
Correct |
4 ms |
27492 KB |
Output is correct |
9 |
Correct |
4 ms |
27492 KB |
Output is correct |
10 |
Correct |
5 ms |
27492 KB |
Output is correct |
11 |
Correct |
4 ms |
27488 KB |
Output is correct |
12 |
Correct |
3 ms |
27484 KB |
Output is correct |
13 |
Correct |
3 ms |
27488 KB |
Output is correct |
14 |
Correct |
3 ms |
27480 KB |
Output is correct |
15 |
Correct |
3 ms |
27484 KB |
Output is correct |
16 |
Correct |
6 ms |
27488 KB |
Output is correct |
17 |
Correct |
4 ms |
27496 KB |
Output is correct |
18 |
Correct |
5 ms |
27580 KB |
Output is correct |
19 |
Correct |
4 ms |
27484 KB |
Output is correct |
20 |
Correct |
4 ms |
27480 KB |
Output is correct |
21 |
Correct |
5 ms |
27484 KB |
Output is correct |
22 |
Correct |
4 ms |
27456 KB |
Output is correct |
23 |
Correct |
3 ms |
27488 KB |
Output is correct |
24 |
Correct |
4 ms |
27488 KB |
Output is correct |
25 |
Correct |
4 ms |
27584 KB |
Output is correct |
26 |
Correct |
3 ms |
27484 KB |
Output is correct |
27 |
Correct |
4 ms |
27484 KB |
Output is correct |
28 |
Correct |
4 ms |
27484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27740 KB |
Output is correct |
2 |
Correct |
3 ms |
27488 KB |
Output is correct |
3 |
Correct |
4 ms |
27496 KB |
Output is correct |
4 |
Correct |
4 ms |
27744 KB |
Output is correct |
5 |
Correct |
4 ms |
27496 KB |
Output is correct |
6 |
Correct |
4 ms |
27492 KB |
Output is correct |
7 |
Correct |
4 ms |
27492 KB |
Output is correct |
8 |
Correct |
4 ms |
27492 KB |
Output is correct |
9 |
Correct |
4 ms |
27492 KB |
Output is correct |
10 |
Correct |
5 ms |
27492 KB |
Output is correct |
11 |
Correct |
4 ms |
27488 KB |
Output is correct |
12 |
Correct |
3 ms |
27484 KB |
Output is correct |
13 |
Correct |
3 ms |
27488 KB |
Output is correct |
14 |
Correct |
3 ms |
27480 KB |
Output is correct |
15 |
Correct |
3 ms |
27484 KB |
Output is correct |
16 |
Correct |
6 ms |
27488 KB |
Output is correct |
17 |
Correct |
4 ms |
27496 KB |
Output is correct |
18 |
Correct |
5 ms |
27580 KB |
Output is correct |
19 |
Correct |
4 ms |
27484 KB |
Output is correct |
20 |
Correct |
4 ms |
27480 KB |
Output is correct |
21 |
Correct |
5 ms |
27484 KB |
Output is correct |
22 |
Correct |
4 ms |
27456 KB |
Output is correct |
23 |
Correct |
3 ms |
27488 KB |
Output is correct |
24 |
Correct |
4 ms |
27488 KB |
Output is correct |
25 |
Correct |
4 ms |
27584 KB |
Output is correct |
26 |
Correct |
3 ms |
27484 KB |
Output is correct |
27 |
Correct |
4 ms |
27484 KB |
Output is correct |
28 |
Correct |
4 ms |
27484 KB |
Output is correct |
29 |
Correct |
5 ms |
27740 KB |
Output is correct |
30 |
Correct |
4 ms |
27484 KB |
Output is correct |
31 |
Correct |
5 ms |
27484 KB |
Output is correct |
32 |
Correct |
5 ms |
27484 KB |
Output is correct |
33 |
Correct |
4 ms |
27552 KB |
Output is correct |
34 |
Correct |
4 ms |
27488 KB |
Output is correct |
35 |
Correct |
5 ms |
27696 KB |
Output is correct |
36 |
Correct |
4 ms |
27484 KB |
Output is correct |
37 |
Correct |
4 ms |
27484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27740 KB |
Output is correct |
2 |
Correct |
3 ms |
27488 KB |
Output is correct |
3 |
Correct |
4 ms |
27496 KB |
Output is correct |
4 |
Correct |
4 ms |
27744 KB |
Output is correct |
5 |
Correct |
4 ms |
27496 KB |
Output is correct |
6 |
Correct |
4 ms |
27492 KB |
Output is correct |
7 |
Correct |
4 ms |
27492 KB |
Output is correct |
8 |
Correct |
4 ms |
27492 KB |
Output is correct |
9 |
Correct |
4 ms |
27492 KB |
Output is correct |
10 |
Correct |
5 ms |
27492 KB |
Output is correct |
11 |
Correct |
4 ms |
27488 KB |
Output is correct |
12 |
Correct |
3 ms |
27484 KB |
Output is correct |
13 |
Correct |
3 ms |
27488 KB |
Output is correct |
14 |
Correct |
3 ms |
27480 KB |
Output is correct |
15 |
Correct |
3 ms |
27484 KB |
Output is correct |
16 |
Correct |
6 ms |
27488 KB |
Output is correct |
17 |
Correct |
4 ms |
27496 KB |
Output is correct |
18 |
Correct |
5 ms |
27580 KB |
Output is correct |
19 |
Correct |
4 ms |
27484 KB |
Output is correct |
20 |
Correct |
4 ms |
27480 KB |
Output is correct |
21 |
Correct |
5 ms |
27484 KB |
Output is correct |
22 |
Correct |
4 ms |
27456 KB |
Output is correct |
23 |
Correct |
3 ms |
27488 KB |
Output is correct |
24 |
Correct |
4 ms |
27488 KB |
Output is correct |
25 |
Correct |
4 ms |
27584 KB |
Output is correct |
26 |
Correct |
3 ms |
27484 KB |
Output is correct |
27 |
Correct |
4 ms |
27484 KB |
Output is correct |
28 |
Correct |
4 ms |
27484 KB |
Output is correct |
29 |
Correct |
5 ms |
27740 KB |
Output is correct |
30 |
Correct |
4 ms |
27484 KB |
Output is correct |
31 |
Correct |
5 ms |
27484 KB |
Output is correct |
32 |
Correct |
5 ms |
27484 KB |
Output is correct |
33 |
Correct |
4 ms |
27552 KB |
Output is correct |
34 |
Correct |
4 ms |
27488 KB |
Output is correct |
35 |
Correct |
5 ms |
27696 KB |
Output is correct |
36 |
Correct |
4 ms |
27484 KB |
Output is correct |
37 |
Correct |
4 ms |
27484 KB |
Output is correct |
38 |
Correct |
54 ms |
33116 KB |
Output is correct |
39 |
Correct |
144 ms |
43092 KB |
Output is correct |
40 |
Correct |
34 ms |
31320 KB |
Output is correct |
41 |
Correct |
24 ms |
29280 KB |
Output is correct |
42 |
Correct |
29 ms |
31568 KB |
Output is correct |
43 |
Correct |
19 ms |
29024 KB |
Output is correct |
44 |
Correct |
9 ms |
28064 KB |
Output is correct |
45 |
Correct |
37 ms |
35172 KB |
Output is correct |
46 |
Correct |
36 ms |
35164 KB |
Output is correct |
47 |
Correct |
46 ms |
37968 KB |
Output is correct |
48 |
Correct |
47 ms |
37988 KB |
Output is correct |
49 |
Correct |
35 ms |
35312 KB |
Output is correct |
50 |
Correct |
39 ms |
35268 KB |
Output is correct |
51 |
Correct |
29 ms |
36184 KB |
Output is correct |
52 |
Correct |
29 ms |
36180 KB |
Output is correct |
53 |
Correct |
10 ms |
28508 KB |
Output is correct |
54 |
Correct |
38 ms |
35836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27480 KB |
Output is correct |
2 |
Correct |
3 ms |
27488 KB |
Output is correct |
3 |
Correct |
3 ms |
27484 KB |
Output is correct |
4 |
Correct |
3 ms |
27484 KB |
Output is correct |
5 |
Correct |
7 ms |
27484 KB |
Output is correct |
6 |
Correct |
4 ms |
27484 KB |
Output is correct |
7 |
Correct |
3 ms |
27484 KB |
Output is correct |
8 |
Correct |
3 ms |
27484 KB |
Output is correct |
9 |
Correct |
3 ms |
27484 KB |
Output is correct |
10 |
Correct |
3 ms |
27484 KB |
Output is correct |
11 |
Correct |
3 ms |
27484 KB |
Output is correct |
12 |
Correct |
4 ms |
27480 KB |
Output is correct |
13 |
Correct |
17 ms |
28164 KB |
Output is correct |
14 |
Correct |
24 ms |
28420 KB |
Output is correct |
15 |
Correct |
21 ms |
28248 KB |
Output is correct |
16 |
Correct |
47 ms |
36032 KB |
Output is correct |
17 |
Correct |
115 ms |
46672 KB |
Output is correct |
18 |
Correct |
273 ms |
84144 KB |
Output is correct |
19 |
Correct |
61 ms |
36336 KB |
Output is correct |
20 |
Correct |
45 ms |
36176 KB |
Output is correct |
21 |
Correct |
45 ms |
36152 KB |
Output is correct |
22 |
Correct |
91 ms |
47560 KB |
Output is correct |
23 |
Correct |
82 ms |
48448 KB |
Output is correct |
24 |
Correct |
95 ms |
47820 KB |
Output is correct |
25 |
Correct |
81 ms |
47812 KB |
Output is correct |
26 |
Correct |
108 ms |
47872 KB |
Output is correct |
27 |
Correct |
116 ms |
44388 KB |
Output is correct |
28 |
Correct |
121 ms |
48332 KB |
Output is correct |
29 |
Correct |
118 ms |
45256 KB |
Output is correct |
30 |
Correct |
69 ms |
40904 KB |
Output is correct |
31 |
Correct |
69 ms |
41420 KB |
Output is correct |
32 |
Correct |
70 ms |
40140 KB |
Output is correct |
33 |
Correct |
75 ms |
41420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27484 KB |
Output is correct |
2 |
Correct |
3 ms |
27484 KB |
Output is correct |
3 |
Correct |
3 ms |
27508 KB |
Output is correct |
4 |
Correct |
9 ms |
27880 KB |
Output is correct |
5 |
Correct |
17 ms |
28268 KB |
Output is correct |
6 |
Correct |
3 ms |
27484 KB |
Output is correct |
7 |
Correct |
4 ms |
27484 KB |
Output is correct |
8 |
Correct |
5 ms |
27740 KB |
Output is correct |
9 |
Correct |
50 ms |
33144 KB |
Output is correct |
10 |
Correct |
120 ms |
43092 KB |
Output is correct |
11 |
Correct |
7 ms |
27484 KB |
Output is correct |
12 |
Correct |
25 ms |
28716 KB |
Output is correct |
13 |
Correct |
104 ms |
100312 KB |
Output is correct |
14 |
Correct |
133 ms |
127996 KB |
Output is correct |
15 |
Runtime error |
2184 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |