#include<bits/stdc++.h>
using namespace std;
const int maxn=120000+10,maxl=18;
vector<int>adj[maxn];
int n,m,timea=0,alln[maxn],lca[maxl][maxn],ted[maxn],high[maxn];
pair<int,int>allq[maxn],stf[maxn];
int zird(int u,int v){
return stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second;
}
int getlca(int u,int v){
if(zird(u,v)){
return v;
}
if(zird(v,u)){
return u;
}
for(int i=maxl-1;i>=0;i--){
if(lca[i][u]!=0&&zird(v,lca[i][u])==0){
u=lca[i][u];
}
}
return lca[0][u];
}
void callca(){
for(int i=1;i<maxl;i++){
for(int j=1;j<=n;j++){
lca[i][j]=lca[i-1][lca[i-1][j]];
}
}
}
void dfs(int u=1,int par=0){
lca[0][u]=par;
timea++;
stf[u].first=timea;
for(auto x:adj[u]){
if(x!=par){
high[x]=high[u]+1;
dfs(x,u);
}
}
stf[u].second=timea;
}
void vorod(){
timea=0;
cin>>n;
// seg.clear();
for(int i=1;i<=n;i++){
adj[i].clear();
alln[i]=-1;
}
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin>>m;
for(int i=1;i<=m;i++){
ted[i]=0;
cin>>allq[i].first>>allq[i].second;
alln[allq[i].first]=i;
alln[allq[i].second]=i;
}
}
int dis(int u,int v){
int uv=getlca(u,v);
return high[u]+high[v]-2*high[uv];
}
int din(int uv,int u,int v){
int a=dis(u,v),b=dis(uv,u),c=dis(uv,v);
//cout<<uv<<" "<<u<<" "<<v<<" "<<a<<" "<<b<<" "<<c<<endl;
if(b+c==a){
return 1;
}
return 0;
}
void psolve(){
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
if(i!=j){
//cout<<i<<" "<<j<<" wtf "<<allq[i].first<<" "<<allq[i].second<<" "<<allq[j].first<<" "<<allq[j].second<<"\n";
if(din(allq[j].first,allq[i].first,allq[i].second)){
ted[i]++;
}
if(din(allq[i].second,allq[j].first,allq[j].second)){
ted[i]++;
}
}
}
}
set<pair<int,int>>st;
for(int i=1;i<=m;i++){
//cout<<"now: "<<ted[i]<<" "<<i<<"\n";
st.insert(make_pair(ted[i],i));
}
while((int)st.size()>0){
auto x=*st.begin();
if(x.first!=0){
//cout<<x.first<<" "<<x.second<<endl;
cout<<"No\n";
return ;
}
st.erase(x);
int i=x.second;
for(int j=1;j<=m;j++){
if(i!=j){
if(din(allq[i].first,allq[j].first,allq[j].second)){
st.erase(make_pair(ted[j],j));
ted[j]--;
st.insert(make_pair(ted[j],j));
}
if(din(allq[j].second,allq[i].first,allq[i].second)){
st.erase(make_pair(ted[j],j));
ted[j]--;
st.insert(make_pair(ted[j],j));
}
}
}
}
cout<<"Yes\n";
return ;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
for(int asd=0;asd<t;asd++){
vorod();
dfs();
callca();
psolve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14824 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
8 ms |
15196 KB |
Output is correct |
5 |
Correct |
15 ms |
15452 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
9 ms |
14924 KB |
Output is correct |
9 |
Correct |
183 ms |
16852 KB |
Output is correct |
10 |
Correct |
67 ms |
27476 KB |
Output is correct |
11 |
Correct |
7 ms |
14940 KB |
Output is correct |
12 |
Correct |
78 ms |
15800 KB |
Output is correct |
13 |
Execution timed out |
5061 ms |
28176 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14828 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14892 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14828 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14892 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14900 KB |
Output is correct |
23 |
Correct |
2 ms |
14684 KB |
Output is correct |
24 |
Correct |
2 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
2 ms |
14684 KB |
Output is correct |
27 |
Correct |
3 ms |
14680 KB |
Output is correct |
28 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14828 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14892 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14900 KB |
Output is correct |
23 |
Correct |
2 ms |
14684 KB |
Output is correct |
24 |
Correct |
2 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
2 ms |
14684 KB |
Output is correct |
27 |
Correct |
3 ms |
14680 KB |
Output is correct |
28 |
Correct |
2 ms |
14684 KB |
Output is correct |
29 |
Correct |
9 ms |
14916 KB |
Output is correct |
30 |
Correct |
20 ms |
15164 KB |
Output is correct |
31 |
Correct |
15 ms |
14680 KB |
Output is correct |
32 |
Correct |
20 ms |
14836 KB |
Output is correct |
33 |
Correct |
5 ms |
14684 KB |
Output is correct |
34 |
Correct |
40 ms |
14888 KB |
Output is correct |
35 |
Correct |
45 ms |
14680 KB |
Output is correct |
36 |
Correct |
41 ms |
14680 KB |
Output is correct |
37 |
Correct |
36 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14828 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14892 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14900 KB |
Output is correct |
23 |
Correct |
2 ms |
14684 KB |
Output is correct |
24 |
Correct |
2 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
2 ms |
14684 KB |
Output is correct |
27 |
Correct |
3 ms |
14680 KB |
Output is correct |
28 |
Correct |
2 ms |
14684 KB |
Output is correct |
29 |
Correct |
9 ms |
14916 KB |
Output is correct |
30 |
Correct |
20 ms |
15164 KB |
Output is correct |
31 |
Correct |
15 ms |
14680 KB |
Output is correct |
32 |
Correct |
20 ms |
14836 KB |
Output is correct |
33 |
Correct |
5 ms |
14684 KB |
Output is correct |
34 |
Correct |
40 ms |
14888 KB |
Output is correct |
35 |
Correct |
45 ms |
14680 KB |
Output is correct |
36 |
Correct |
41 ms |
14680 KB |
Output is correct |
37 |
Correct |
36 ms |
14684 KB |
Output is correct |
38 |
Correct |
181 ms |
16680 KB |
Output is correct |
39 |
Correct |
67 ms |
27716 KB |
Output is correct |
40 |
Correct |
1405 ms |
16720 KB |
Output is correct |
41 |
Correct |
1734 ms |
16536 KB |
Output is correct |
42 |
Correct |
1261 ms |
16784 KB |
Output is correct |
43 |
Correct |
36 ms |
16212 KB |
Output is correct |
44 |
Correct |
968 ms |
15188 KB |
Output is correct |
45 |
Correct |
194 ms |
20048 KB |
Output is correct |
46 |
Correct |
189 ms |
20060 KB |
Output is correct |
47 |
Correct |
546 ms |
23960 KB |
Output is correct |
48 |
Correct |
521 ms |
23964 KB |
Output is correct |
49 |
Correct |
287 ms |
20632 KB |
Output is correct |
50 |
Correct |
326 ms |
20388 KB |
Output is correct |
51 |
Correct |
77 ms |
21448 KB |
Output is correct |
52 |
Correct |
77 ms |
21328 KB |
Output is correct |
53 |
Correct |
1025 ms |
15404 KB |
Output is correct |
54 |
Correct |
161 ms |
20212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14832 KB |
Output is correct |
3 |
Correct |
2 ms |
14820 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
8 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14828 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14832 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
41 ms |
14900 KB |
Output is correct |
13 |
Correct |
304 ms |
15196 KB |
Output is correct |
14 |
Correct |
275 ms |
15712 KB |
Output is correct |
15 |
Correct |
317 ms |
15472 KB |
Output is correct |
16 |
Execution timed out |
5043 ms |
20316 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14824 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
8 ms |
15196 KB |
Output is correct |
5 |
Correct |
15 ms |
15452 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
9 ms |
14924 KB |
Output is correct |
9 |
Correct |
183 ms |
16852 KB |
Output is correct |
10 |
Correct |
67 ms |
27476 KB |
Output is correct |
11 |
Correct |
7 ms |
14940 KB |
Output is correct |
12 |
Correct |
78 ms |
15800 KB |
Output is correct |
13 |
Execution timed out |
5061 ms |
28176 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |