#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define mpa make_pair
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define bc back()
#define ar array
#define vll vector<ll>
using namespace std;/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;*/
template <class _T>
bool chmin(_T &x, const _T &y){
if(x>y){
x=y;
return true;
}
return false;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag=false;
if (x<y){
x=y;flag|=true;
}
return flag;
}
//#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=2e18+7;
const ll mod=1e9+7;
const ll N=2e5+7;
const ld eps=1e-9;
vector<vll> g(N);
vector<pair<ll,ll>> pr(N);
ll d[N],p[N][20];
ll tin[N],tout[N],t;
void dfs(ll v){
tin[v]=++t;
for(ll i=1;i<19;i++) p[v][i]=p[p[v][i-1]][i-1];
for(auto i : g[v]){
if(p[v][0]==i)continue;
p[i][0]=v;
d[i]=d[v]+1;
dfs(i);
}
tout[v]=++t;
}
ll lca(ll a,ll b){
if(d[b]>d[a])swap(a,b);
ll i;
for(i=18;i>=0;i--){
if(d[a]-d[b]>=(1ll<<i)){
a=p[a][i];
}
}
if(a==b)return b;
for(i=18;i>=0;i--){
if(p[a][i]!=p[b][i]){
a=p[a][i];
b=p[b][i];
}
}
return p[a][0];
}
bool par(ll p,ll ch){
return (tin[p]<=tin[ch] && tout[ch]<=tout[p]);
}
bool on_path(ll a,ll x,ll y){
ll lc=lca(x,y);
if(!par(lc,a))return false;
if(par(a,x) || par(a,y))return true;
return false;
}
void solve(){
ll i,j;
ll n,m;
ll a,b;
cin>>n;
for(i=1;i<=n;i++){
g[i].clear();
for(j=0;j<20;j++)p[i][j]=0;
d[i]=tin[i]=t=tout[i]=0;
}
bool bamboo=true;
for(i=1;i<n;i++){
cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
if(a+1!=b)bamboo=false;
}
if(bamboo){
cin>>m;
ll l=0;
vector<pair<ll,ll>> v;
for(i=0;i<m;i++){
cin>>a>>b;
v.pb({a,b});
}
sort(all(v));
for(i=0;i<m;i++){
a=v[i].fr,b=v[i].sc;
if(b<=l){
cout<<"No"<<endl;
return;
}
l=max(l,b);
}
cout<<"Yes"<<endl;
return;
}
dfs(1);
cin>>m;
map<ll,ll> mp,mp2;
for(i=0;i<m;i++) {
cin>>pr[i].fr>>pr[i].sc;
if(mp[pr[i].fr]){
cout<<"No"<<endl;
return;
}mp[pr[i].fr]++;
if(mp2[pr[i].sc]){
cout<<"No"<<endl;
return;
}mp2[pr[i].sc]++;
}
a=pr[0].fr,b=pr[0].sc;
ll x=pr[1].fr,y=pr[1].sc;
if((on_path(x,a,b) && on_path(y,a,b)) || (on_path(a,x,y) && on_path(b,x,y))){
cout<<"No"<<endl;
}
else if(!on_path(pr[0].sc,pr[1].fr,pr[1].sc) || !on_path(pr[1].sc,pr[0].fr,pr[0].sc)){
cout<<"Yes"<<endl;
}else cout<<"No"<<endl;
/*bool dp[(1ll<<(m+5))];
memset(dp,false,sizeof(dp));
dp[0]=true;
vector<vll> v(m+5);
for(ll mask=0;mask<(1ll<<m);mask++){
ll c=0;
for(i=0;i<m;i++){
if(mask&(1ll<<i))c++;
}
v[c].pb(mask);
}
for(i=1;i<=m;i++){
for(auto j : v[i]){
vector<ll> cur;
for(ll bit=0;bit<m;bit++){
if(j&(1ll<<bit)){
cur.pb(bit);
}
}
for(auto x : cur){
if(!dp[j^(1ll<<x)])continue;
bool flag=true;
for(auto y : cur){
if(x==y)continue;
if(!can_pass(x,y) && !can_pass(y,x)){flag=false;break;}
}
if(flag){dp[j]=true;break;}
}
}
}
if(dp[(1ll<<m)-1])cout<<"Yes"<<endl;
else cout<<"No"<<endl;*/
}
signed main(){
//start();
ll t=1;
cin>>t;
while(t--) solve();
return 0;
}
/*
1
7
1 2
2 3
3 4
4 5
3 6
6 7
2
4 1
5 7
*/
Compilation message
jail.cpp: In function 'void fre(std::string)':
jail.cpp:43:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:43:64: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
19 ms |
14424 KB |
Output is correct |
5 |
Correct |
38 ms |
14428 KB |
Output is correct |
6 |
Correct |
4 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
49 ms |
14684 KB |
Output is correct |
10 |
Correct |
65 ms |
37200 KB |
Output is correct |
11 |
Correct |
12 ms |
14428 KB |
Output is correct |
12 |
Correct |
47 ms |
14444 KB |
Output is correct |
13 |
Correct |
83 ms |
38092 KB |
Output is correct |
14 |
Correct |
85 ms |
38096 KB |
Output is correct |
15 |
Correct |
85 ms |
38064 KB |
Output is correct |
16 |
Correct |
122 ms |
39144 KB |
Output is correct |
17 |
Correct |
88 ms |
38116 KB |
Output is correct |
18 |
Correct |
110 ms |
39100 KB |
Output is correct |
19 |
Correct |
90 ms |
38124 KB |
Output is correct |
20 |
Correct |
86 ms |
38348 KB |
Output is correct |
21 |
Correct |
94 ms |
38292 KB |
Output is correct |
22 |
Correct |
86 ms |
38092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Incorrect |
5 ms |
14428 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Incorrect |
5 ms |
14428 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Incorrect |
5 ms |
14428 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Incorrect |
5 ms |
14428 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14424 KB |
Output is correct |
2 |
Incorrect |
3 ms |
14428 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
19 ms |
14424 KB |
Output is correct |
5 |
Correct |
38 ms |
14428 KB |
Output is correct |
6 |
Correct |
4 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
49 ms |
14684 KB |
Output is correct |
10 |
Correct |
65 ms |
37200 KB |
Output is correct |
11 |
Correct |
12 ms |
14428 KB |
Output is correct |
12 |
Correct |
47 ms |
14444 KB |
Output is correct |
13 |
Correct |
83 ms |
38092 KB |
Output is correct |
14 |
Correct |
85 ms |
38096 KB |
Output is correct |
15 |
Correct |
85 ms |
38064 KB |
Output is correct |
16 |
Correct |
122 ms |
39144 KB |
Output is correct |
17 |
Correct |
88 ms |
38116 KB |
Output is correct |
18 |
Correct |
110 ms |
39100 KB |
Output is correct |
19 |
Correct |
90 ms |
38124 KB |
Output is correct |
20 |
Correct |
86 ms |
38348 KB |
Output is correct |
21 |
Correct |
94 ms |
38292 KB |
Output is correct |
22 |
Correct |
86 ms |
38092 KB |
Output is correct |
23 |
Correct |
3 ms |
14428 KB |
Output is correct |
24 |
Correct |
3 ms |
14680 KB |
Output is correct |
25 |
Correct |
5 ms |
14428 KB |
Output is correct |
26 |
Incorrect |
5 ms |
14428 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |