#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef pair<int, int> pii;
typedef long long ll;
const int N=1e6+5;
int n, m, nm, sq, s[N], t[N], dis[N], pr[N][18];
vector<int>g[N], h[3*N], nd;
bool mk[3*N];
pii st[N];
void dfs(const int &v, const int &p=-1){
for(int u: g[v]){
if(u!=p){
pr[u][0]=v;
for(int i=1;i<17;++i)
pr[u][i]=pr[pr[u][i-1]][i-1];
dis[u]=dis[v]+1;
dfs(u, v);
}
}
}
int par(int v, int p){
for(int i=0;p>0;++i){
if(p&1)
v=pr[v][i];
p>>=1;
}
return v;
}
int lca(int u, int v){
u=par(u, dis[u]-dis[v]);
v=par(v, dis[v]-dis[u]);
if(u==v)
return v;
for(int i=16;i>=0;--i){
if(pr[u][i]!=pr[v][i]){
v=pr[v][i];
u=pr[u][i];
}
}
return pr[v][0];
}
void pre(){
nm=2*n+m;
for(int i=0;i<nm;++i)
h[i].clear();
for(int v=0;v<n;++v){
int u=v, w=par(v, sq);
while(u!=w){
if(s[u]!=-1)
h[s[u]+2*n].push_back(v);
if(t[u]!=-1)
h[v+n].push_back(t[u]+2*n);
u=pr[u][0];
}
}
}
void opr(int i, int v, int u){
int w=lca(u, v);
int x=pr[v][0];
int y=par(x, sq);
while(dis[y]>=dis[w]){
h[x].push_back(i);
x=y;
y=par(x, sq);
if(x==w)
break;
}
for(;dis[x]>=dis[w];x=pr[x][0]){
if(s[x]!=-1)
h[s[x]+2*n].push_back(i);
if(x==w)
break;
}
x=v;
y=par(v, sq);
while(dis[y]>=dis[w]){
h[i].push_back(x+n);
x=y;
y=par(x, sq);
if(x==w)
break;
}
for(;dis[x]>=dis[w];x=pr[x][0]){
if(t[x]!=-1)
h[i].push_back(t[x]+2*n);
if(x==w)
break;
}
x=pr[u][0];
y=par(x, sq);
while(dis[y]>=dis[w]){
h[i].push_back(x+n);
x=y;
y=par(x, sq);
if(x==w)
break;
}
for(;dis[x]>dis[w];x=pr[x][0]){
if(t[x]!=-1)
h[i].push_back(t[x]+2*n);
}
x=u;
y=par(x, sq);
while(dis[y]>=dis[w]){
h[x].push_back(i);
x=y;
y=par(x, sq);
if(x==w)
break;
}
for(;dis[x]>dis[w];x=pr[x][0]){
if(s[x]!=-1)
h[s[x]+2*n].push_back(i);
}
}
void sfd(const int &v){
mk[v]=1;
for(int u: h[v]){
if(!mk[u])
sfd(u);
}
nd.push_back(v);
}
void sol(){
cin>>n;
sq=sqrt(n);
for(int i=0;i<n;++i)
g[i].clear();
for(int i=1;i<n;++i){
int u, v;
cin>>u>>v;
g[--v].push_back(--u);
g[u].push_back(v);
}
cin>>m;
memset(s, -1, 4*n);
memset(t, -1, 4*n);
for(int i=0;i<m;++i){
int u, v;
cin>>u>>v;
s[--u]=i;
t[--v]=i;
st[i]={u, v};
}
dfs(0);
pre();
for(int i=0;i<m;++i)
opr(2*n+i, st[i].F, st[i].S);
nd.clear();
memset(mk, 0, 4*nm);
for(int i=0;i<nm;++i){
if(!mk[i])
sfd(i);
}
memset(mk, 0, 4*nm);
for(int i=0;i<nm;++i){
mk[nd[i]]=1;
for(int u: h[nd[i]]){
if(!mk[u]){
cout<<"No\n";
return;
}
}
}
cout<<"Yes\n";
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int T;
cin>>T;
while(T--)
sol();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
94284 KB |
Output is correct |
2 |
Correct |
43 ms |
94196 KB |
Output is correct |
3 |
Correct |
43 ms |
94156 KB |
Output is correct |
4 |
Correct |
61 ms |
94264 KB |
Output is correct |
5 |
Correct |
66 ms |
94312 KB |
Output is correct |
6 |
Correct |
46 ms |
94412 KB |
Output is correct |
7 |
Correct |
50 ms |
94284 KB |
Output is correct |
8 |
Correct |
44 ms |
94396 KB |
Output is correct |
9 |
Correct |
126 ms |
97132 KB |
Output is correct |
10 |
Correct |
253 ms |
127696 KB |
Output is correct |
11 |
Correct |
54 ms |
94428 KB |
Output is correct |
12 |
Correct |
84 ms |
94736 KB |
Output is correct |
13 |
Correct |
759 ms |
391832 KB |
Output is correct |
14 |
Correct |
742 ms |
406028 KB |
Output is correct |
15 |
Correct |
4386 ms |
495596 KB |
Output is correct |
16 |
Execution timed out |
5113 ms |
779112 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
94292 KB |
Output is correct |
2 |
Correct |
49 ms |
94284 KB |
Output is correct |
3 |
Correct |
48 ms |
94296 KB |
Output is correct |
4 |
Correct |
49 ms |
94256 KB |
Output is correct |
5 |
Correct |
49 ms |
94260 KB |
Output is correct |
6 |
Correct |
49 ms |
94360 KB |
Output is correct |
7 |
Correct |
50 ms |
94352 KB |
Output is correct |
8 |
Correct |
50 ms |
94292 KB |
Output is correct |
9 |
Correct |
51 ms |
94284 KB |
Output is correct |
10 |
Correct |
49 ms |
94356 KB |
Output is correct |
11 |
Correct |
50 ms |
94284 KB |
Output is correct |
12 |
Correct |
48 ms |
94332 KB |
Output is correct |
13 |
Correct |
48 ms |
94336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
94292 KB |
Output is correct |
2 |
Correct |
49 ms |
94284 KB |
Output is correct |
3 |
Correct |
48 ms |
94296 KB |
Output is correct |
4 |
Correct |
49 ms |
94256 KB |
Output is correct |
5 |
Correct |
49 ms |
94260 KB |
Output is correct |
6 |
Correct |
49 ms |
94360 KB |
Output is correct |
7 |
Correct |
50 ms |
94352 KB |
Output is correct |
8 |
Correct |
50 ms |
94292 KB |
Output is correct |
9 |
Correct |
51 ms |
94284 KB |
Output is correct |
10 |
Correct |
49 ms |
94356 KB |
Output is correct |
11 |
Correct |
50 ms |
94284 KB |
Output is correct |
12 |
Correct |
48 ms |
94332 KB |
Output is correct |
13 |
Correct |
48 ms |
94336 KB |
Output is correct |
14 |
Correct |
51 ms |
94228 KB |
Output is correct |
15 |
Correct |
49 ms |
94236 KB |
Output is correct |
16 |
Correct |
50 ms |
94388 KB |
Output is correct |
17 |
Correct |
51 ms |
94256 KB |
Output is correct |
18 |
Correct |
51 ms |
94268 KB |
Output is correct |
19 |
Correct |
49 ms |
94192 KB |
Output is correct |
20 |
Correct |
50 ms |
94284 KB |
Output is correct |
21 |
Correct |
49 ms |
94272 KB |
Output is correct |
22 |
Correct |
49 ms |
94364 KB |
Output is correct |
23 |
Correct |
49 ms |
94228 KB |
Output is correct |
24 |
Correct |
50 ms |
94256 KB |
Output is correct |
25 |
Correct |
50 ms |
94364 KB |
Output is correct |
26 |
Correct |
48 ms |
94284 KB |
Output is correct |
27 |
Correct |
49 ms |
94256 KB |
Output is correct |
28 |
Correct |
49 ms |
94284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
94292 KB |
Output is correct |
2 |
Correct |
49 ms |
94284 KB |
Output is correct |
3 |
Correct |
48 ms |
94296 KB |
Output is correct |
4 |
Correct |
49 ms |
94256 KB |
Output is correct |
5 |
Correct |
49 ms |
94260 KB |
Output is correct |
6 |
Correct |
49 ms |
94360 KB |
Output is correct |
7 |
Correct |
50 ms |
94352 KB |
Output is correct |
8 |
Correct |
50 ms |
94292 KB |
Output is correct |
9 |
Correct |
51 ms |
94284 KB |
Output is correct |
10 |
Correct |
49 ms |
94356 KB |
Output is correct |
11 |
Correct |
50 ms |
94284 KB |
Output is correct |
12 |
Correct |
48 ms |
94332 KB |
Output is correct |
13 |
Correct |
48 ms |
94336 KB |
Output is correct |
14 |
Correct |
51 ms |
94228 KB |
Output is correct |
15 |
Correct |
49 ms |
94236 KB |
Output is correct |
16 |
Correct |
50 ms |
94388 KB |
Output is correct |
17 |
Correct |
51 ms |
94256 KB |
Output is correct |
18 |
Correct |
51 ms |
94268 KB |
Output is correct |
19 |
Correct |
49 ms |
94192 KB |
Output is correct |
20 |
Correct |
50 ms |
94284 KB |
Output is correct |
21 |
Correct |
49 ms |
94272 KB |
Output is correct |
22 |
Correct |
49 ms |
94364 KB |
Output is correct |
23 |
Correct |
49 ms |
94228 KB |
Output is correct |
24 |
Correct |
50 ms |
94256 KB |
Output is correct |
25 |
Correct |
50 ms |
94364 KB |
Output is correct |
26 |
Correct |
48 ms |
94284 KB |
Output is correct |
27 |
Correct |
49 ms |
94256 KB |
Output is correct |
28 |
Correct |
49 ms |
94284 KB |
Output is correct |
29 |
Correct |
53 ms |
94376 KB |
Output is correct |
30 |
Correct |
50 ms |
94356 KB |
Output is correct |
31 |
Correct |
50 ms |
94348 KB |
Output is correct |
32 |
Correct |
58 ms |
94304 KB |
Output is correct |
33 |
Correct |
53 ms |
94364 KB |
Output is correct |
34 |
Correct |
48 ms |
94300 KB |
Output is correct |
35 |
Correct |
51 ms |
94396 KB |
Output is correct |
36 |
Correct |
49 ms |
94368 KB |
Output is correct |
37 |
Correct |
49 ms |
94356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
94292 KB |
Output is correct |
2 |
Correct |
49 ms |
94284 KB |
Output is correct |
3 |
Correct |
48 ms |
94296 KB |
Output is correct |
4 |
Correct |
49 ms |
94256 KB |
Output is correct |
5 |
Correct |
49 ms |
94260 KB |
Output is correct |
6 |
Correct |
49 ms |
94360 KB |
Output is correct |
7 |
Correct |
50 ms |
94352 KB |
Output is correct |
8 |
Correct |
50 ms |
94292 KB |
Output is correct |
9 |
Correct |
51 ms |
94284 KB |
Output is correct |
10 |
Correct |
49 ms |
94356 KB |
Output is correct |
11 |
Correct |
50 ms |
94284 KB |
Output is correct |
12 |
Correct |
48 ms |
94332 KB |
Output is correct |
13 |
Correct |
48 ms |
94336 KB |
Output is correct |
14 |
Correct |
51 ms |
94228 KB |
Output is correct |
15 |
Correct |
49 ms |
94236 KB |
Output is correct |
16 |
Correct |
50 ms |
94388 KB |
Output is correct |
17 |
Correct |
51 ms |
94256 KB |
Output is correct |
18 |
Correct |
51 ms |
94268 KB |
Output is correct |
19 |
Correct |
49 ms |
94192 KB |
Output is correct |
20 |
Correct |
50 ms |
94284 KB |
Output is correct |
21 |
Correct |
49 ms |
94272 KB |
Output is correct |
22 |
Correct |
49 ms |
94364 KB |
Output is correct |
23 |
Correct |
49 ms |
94228 KB |
Output is correct |
24 |
Correct |
50 ms |
94256 KB |
Output is correct |
25 |
Correct |
50 ms |
94364 KB |
Output is correct |
26 |
Correct |
48 ms |
94284 KB |
Output is correct |
27 |
Correct |
49 ms |
94256 KB |
Output is correct |
28 |
Correct |
49 ms |
94284 KB |
Output is correct |
29 |
Correct |
53 ms |
94376 KB |
Output is correct |
30 |
Correct |
50 ms |
94356 KB |
Output is correct |
31 |
Correct |
50 ms |
94348 KB |
Output is correct |
32 |
Correct |
58 ms |
94304 KB |
Output is correct |
33 |
Correct |
53 ms |
94364 KB |
Output is correct |
34 |
Correct |
48 ms |
94300 KB |
Output is correct |
35 |
Correct |
51 ms |
94396 KB |
Output is correct |
36 |
Correct |
49 ms |
94368 KB |
Output is correct |
37 |
Correct |
49 ms |
94356 KB |
Output is correct |
38 |
Correct |
132 ms |
97484 KB |
Output is correct |
39 |
Correct |
266 ms |
128072 KB |
Output is correct |
40 |
Correct |
115 ms |
96944 KB |
Output is correct |
41 |
Correct |
88 ms |
96008 KB |
Output is correct |
42 |
Correct |
95 ms |
96804 KB |
Output is correct |
43 |
Correct |
103 ms |
95932 KB |
Output is correct |
44 |
Correct |
65 ms |
94856 KB |
Output is correct |
45 |
Correct |
253 ms |
111492 KB |
Output is correct |
46 |
Correct |
232 ms |
111304 KB |
Output is correct |
47 |
Correct |
310 ms |
116748 KB |
Output is correct |
48 |
Correct |
299 ms |
116884 KB |
Output is correct |
49 |
Correct |
420 ms |
111448 KB |
Output is correct |
50 |
Correct |
417 ms |
110916 KB |
Output is correct |
51 |
Correct |
348 ms |
114360 KB |
Output is correct |
52 |
Correct |
349 ms |
114796 KB |
Output is correct |
53 |
Correct |
59 ms |
96204 KB |
Output is correct |
54 |
Correct |
166 ms |
110480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
94248 KB |
Output is correct |
2 |
Correct |
50 ms |
94176 KB |
Output is correct |
3 |
Correct |
50 ms |
94192 KB |
Output is correct |
4 |
Correct |
52 ms |
94264 KB |
Output is correct |
5 |
Correct |
55 ms |
94388 KB |
Output is correct |
6 |
Correct |
49 ms |
94304 KB |
Output is correct |
7 |
Correct |
50 ms |
94284 KB |
Output is correct |
8 |
Correct |
55 ms |
94168 KB |
Output is correct |
9 |
Correct |
50 ms |
94280 KB |
Output is correct |
10 |
Correct |
49 ms |
94284 KB |
Output is correct |
11 |
Correct |
51 ms |
94284 KB |
Output is correct |
12 |
Correct |
53 ms |
94312 KB |
Output is correct |
13 |
Correct |
73 ms |
94748 KB |
Output is correct |
14 |
Correct |
85 ms |
94668 KB |
Output is correct |
15 |
Correct |
76 ms |
94696 KB |
Output is correct |
16 |
Correct |
139 ms |
113776 KB |
Output is correct |
17 |
Correct |
269 ms |
124976 KB |
Output is correct |
18 |
Correct |
477 ms |
143392 KB |
Output is correct |
19 |
Correct |
143 ms |
112196 KB |
Output is correct |
20 |
Correct |
148 ms |
113080 KB |
Output is correct |
21 |
Correct |
148 ms |
113096 KB |
Output is correct |
22 |
Correct |
210 ms |
123568 KB |
Output is correct |
23 |
Correct |
194 ms |
123236 KB |
Output is correct |
24 |
Correct |
206 ms |
123184 KB |
Output is correct |
25 |
Correct |
220 ms |
123324 KB |
Output is correct |
26 |
Correct |
215 ms |
123436 KB |
Output is correct |
27 |
Correct |
244 ms |
121480 KB |
Output is correct |
28 |
Correct |
226 ms |
124148 KB |
Output is correct |
29 |
Correct |
220 ms |
122400 KB |
Output is correct |
30 |
Correct |
169 ms |
119052 KB |
Output is correct |
31 |
Correct |
169 ms |
119740 KB |
Output is correct |
32 |
Correct |
180 ms |
117104 KB |
Output is correct |
33 |
Correct |
174 ms |
117860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
94284 KB |
Output is correct |
2 |
Correct |
43 ms |
94196 KB |
Output is correct |
3 |
Correct |
43 ms |
94156 KB |
Output is correct |
4 |
Correct |
61 ms |
94264 KB |
Output is correct |
5 |
Correct |
66 ms |
94312 KB |
Output is correct |
6 |
Correct |
46 ms |
94412 KB |
Output is correct |
7 |
Correct |
50 ms |
94284 KB |
Output is correct |
8 |
Correct |
44 ms |
94396 KB |
Output is correct |
9 |
Correct |
126 ms |
97132 KB |
Output is correct |
10 |
Correct |
253 ms |
127696 KB |
Output is correct |
11 |
Correct |
54 ms |
94428 KB |
Output is correct |
12 |
Correct |
84 ms |
94736 KB |
Output is correct |
13 |
Correct |
759 ms |
391832 KB |
Output is correct |
14 |
Correct |
742 ms |
406028 KB |
Output is correct |
15 |
Correct |
4386 ms |
495596 KB |
Output is correct |
16 |
Execution timed out |
5113 ms |
779112 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |