#include <bits/stdc++.h>
using namespace std;
int q,n,m;
vector<int> f[200001];
void read()
{
cin>>n;
for(int i=1; i<=n; i++)
f[i].clear();
for(int i=1; i<n; i++)
{
int x,y;
cin>>x>>y;
f[x].push_back(y);
f[y].push_back(x);
}
}
int num;
int ts[800001];// number of the vertex corresponding
int te[800001];// to the given tree and segment of the hld
int dw[800001];
int up[800001];
vector<int> v[200001],o[200001];
void make_tree(int i,int l,int r)
{
if(l==r)
{
ts[i]=num++;
dw[l]=ts[i];
te[i]=num++;
up[l]=te[i];
//cout<<l<<" "<<r<<" "<<te[i]<<endl;
return;
}
int mid=(l+r)/2;
ts[i]=num++;
te[i]=num++;
//cout<<l<<" "<<r<<" "<<te[i]<<endl;
make_tree(i*2,l,mid);
make_tree(i*2+1,mid+1,r);
int il=ts[i*2];
int ir=ts[i*2+1];
v[ts[i]].push_back(il);
v[ts[i]].push_back(ir);
o[il].push_back(ts[i]);
o[ir].push_back(ts[i]);
il=te[i*2];
ir=te[i*2+1];
v[il].push_back(te[i]);
v[ir].push_back(te[i]);
o[te[i]].push_back(il);
o[te[i]].push_back(ir);
}
int used[200001];
int jump[200001][21];
int sz[200001];
int pos[200001];// new number of vertex i in original graph
int chain[200001];// leader of the chain in which i is
int maxx[200001];
int lvl[200001];
void necessities(int i,int h)
{
lvl[i]=h;
sz[i]=1;
used[i]=1;
for(int j=0; j<f[i].size(); j++)
{
int nb=f[i][j];
if(!used[nb])
{
necessities(nb,h+1);
jump[nb][0]=i;
sz[i]+=sz[nb];
if(sz[maxx[i]]<sz[nb])maxx[i]=nb;
}
}
}
int cnt=1;
void hld(int i,int lead)
{
chain[i]=lead;
pos[i]=cnt++;
if(maxx[i])hld(maxx[i],lead);
for(int j=0; j<f[i].size(); j++)
{
int nb=f[i][j];
if(pos[nb]==0)hld(nb,nb);
}
}
void prec()
{
for(int j=1; j<=20; j++)
{
for(int i=1; i<=n; i++)
{
jump[i][j]=jump[jump[i][j-1]][j-1];
//cout<<i<<" "<<j<<" "<<jump[i][j]<<endl;
}
}
}
int make_jump(int x,int k)
{
for(int i=20; i>=0; i--)
if(k&(1<<i))
x=jump[x][i];
return x;
}
int lca(int x,int y)
{
if(lvl[x]>lvl[y])swap(x,y);
y=make_jump(y,abs(lvl[y]-lvl[x]));
//cout<<x<<" "<<y<<endl;
if(x==y)return x;
for(int i=20; i>=0; i--)
{
if(jump[x][i]!=jump[y][i])
{
x=jump[x][i];
y=jump[y][i];
}
}
return jump[x][0];
}
void update(int i,int l,int r,int ql,int qr,int pr)
{
if(ql>qr)return;
if(ql<=l&&r<=qr)
{
//cout<<l<<"-"<<r<<endl;
int idx=ts[i];
//cout<<idx<<endl;
v[pr].push_back(idx);
o[idx].push_back(pr);
idx=te[i];
//cout<<idx<<endl;
v[idx].push_back(pr);
o[pr].push_back(idx);
return;
}
int mid=(l+r)/2;
update(i*2,l,mid,ql,min(mid,qr),pr);
update(i*2+1,mid+1,r,max(ql,mid+1),qr,pr);
}
void make_graph()
{
for(int i=1; i<=m; i++)
{
int x,y;
cin>>x>>y;
//cout<<i<<" "<<dw[pos[x]]<<" "<<up[pos[y]]<<endl;
v[dw[pos[x]]].push_back(i);
o[i].push_back(dw[pos[x]]);
v[i].push_back(up[pos[y]]);
o[up[pos[y]]].push_back(i);
int ver=lca(x,y);
int c1=pos[x];
while(c1!=ver)
{
int c2=chain[c1];
if(lvl[c2]<lvl[ver])c2=ver;
update(1,1,n,c2,c1,i);
c1=c2;
if(c2==ver)break;
c1=jump[c1][0];
}
c1=pos[y];
while(c1!=ver)
{
int c2=chain[c1];
if(lvl[c2]<lvl[ver])c2=ver;
update(1,1,n,c2,c1,i);
c1=c2;
if(c2==ver)break;
c1=jump[c1][0];
}
}
}
int used1[200001];
stack<int> st;
void dfs1(int i)
{
used1[i]=1;
for(int j=0; j<v[i].size(); j++)
{
int nb=v[i][j];
if(!used1[nb])
{
dfs1(nb);
}
}
st.push(i);
}
int used2[200001];
void dfs2(int i)
{
used2[i]=1;
for(int j=0; j<o[i].size(); j++)
{
int nb=o[i][j];
if(!used2[nb])
{
dfs2(nb);
}
}
}
void check()
{
memset(used1,0,sizeof(used1));
memset(used2,0,sizeof(used2));
for(int i=1;i<=m;i++)
{
if(!used1[i])dfs1(i);
}
int cnt=0;
while(st.size())
{
int vr=st.top();
st.pop();
if(vr<=m&&!used2[vr])
{
cnt++;
dfs2(vr);
}
}
if(cnt==m)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>q;
while(q--)
{
for(int i=1; i<=num; i++)
{
o[i].clear();
v[i].clear();
}
memset(maxx,0,sizeof(maxx));
memset(used,0,sizeof(used));
read();
cnt=1;
necessities(1,1);
hld(1,1);
prec();
cin>>m;
num=m+1;
make_tree(1,1,n);
make_graph();
/*for(int i=1;i<num;i++)
{
cout<<i<<": ";
for(int j=0;j<v[i].size();j++)
cout<<v[i][j]<<" ";
cout<<endl;
}*/
check();
}
return 0;
}
Compilation message
jail.cpp: In function 'void necessities(int, int)':
jail.cpp:67:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j=0; j<f[i].size(); j++)
| ~^~~~~~~~~~~~
jail.cpp: In function 'void hld(int, int)':
jail.cpp:85:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int j=0; j<f[i].size(); j++)
| ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs1(int)':
jail.cpp:184:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
184 | for(int j=0; j<v[i].size(); j++)
| ~^~~~~~~~~~~~
jail.cpp: In function 'void dfs2(int)':
jail.cpp:198:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
198 | for(int j=0; j<o[i].size(); j++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31116 KB |
Output is correct |
3 |
Correct |
6 ms |
31068 KB |
Output is correct |
4 |
Correct |
39 ms |
31584 KB |
Output is correct |
5 |
Correct |
77 ms |
31836 KB |
Output is correct |
6 |
Correct |
8 ms |
31324 KB |
Output is correct |
7 |
Correct |
8 ms |
31384 KB |
Output is correct |
8 |
Correct |
9 ms |
31324 KB |
Output is correct |
9 |
Correct |
47 ms |
34576 KB |
Output is correct |
10 |
Runtime error |
103 ms |
130644 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31064 KB |
Output is correct |
3 |
Correct |
8 ms |
31320 KB |
Output is correct |
4 |
Incorrect |
8 ms |
31324 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31064 KB |
Output is correct |
3 |
Correct |
8 ms |
31320 KB |
Output is correct |
4 |
Incorrect |
8 ms |
31324 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31064 KB |
Output is correct |
3 |
Correct |
8 ms |
31320 KB |
Output is correct |
4 |
Incorrect |
8 ms |
31324 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31064 KB |
Output is correct |
3 |
Correct |
8 ms |
31320 KB |
Output is correct |
4 |
Incorrect |
8 ms |
31324 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31068 KB |
Output is correct |
3 |
Correct |
7 ms |
31132 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
65 ms |
31432 KB |
Output is correct |
6 |
Incorrect |
7 ms |
31320 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
6 ms |
31116 KB |
Output is correct |
3 |
Correct |
6 ms |
31068 KB |
Output is correct |
4 |
Correct |
39 ms |
31584 KB |
Output is correct |
5 |
Correct |
77 ms |
31836 KB |
Output is correct |
6 |
Correct |
8 ms |
31324 KB |
Output is correct |
7 |
Correct |
8 ms |
31384 KB |
Output is correct |
8 |
Correct |
9 ms |
31324 KB |
Output is correct |
9 |
Correct |
47 ms |
34576 KB |
Output is correct |
10 |
Runtime error |
103 ms |
130644 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |