This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int M=1e9+7;
const int N=300+1;
void add(int &a,int b){
a+=b;
if(a>=M) a-=M;
else if(a<0) a+=M;
}
int mult(int a,int b){
return 1ll*a*b%M;
}
vec<int> gr[N],g[N];
int used[N],ok;
void dfs(int v){
used[v]=1;
for(auto &z : gr[v]){
if(used[z]){
ok&=(used[z]==2);
continue;
}
dfs(z);
}
used[v]=2;
}
int par[N],in[N],out[N],tt;
void dfs1(int v,int p){
par[v]=p;
in[v]=tt++;
for(auto &z : g[v]){
if(z==p) continue;
dfs1(z,v);
}
out[v]=tt-1;
}
bool is(int a,int b){
return in[a]<=in[b]&&out[a]>=out[b];
}
void solve(){
int n,m;
cin>>n;
for(int i=0;i<n;i++) g[i].clear();
ok=1;
for(int i=1;i<n;i++){
int v,u;
cin>>v>>u;--v;--u;
g[v].pb(u);g[u].pb(v);
}
dfs1(0,0);
cin>>m;
vec<int> st(m),f(m);
for(int i=0;i<m;i++) used[i]=0,gr[i].clear();
for(int i=0;i<m;i++){
cin>>st[i]>>f[i];--st[i];--f[i];
}
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if(i==j) continue;
/// shoot
int v=st[i],u=f[i];
while(!is(v,u)){
if(v==st[j]) gr[j].pb(i);
if(v==f[j]) gr[i].pb(j);
v=par[v];
}
if(v==st[j]) gr[j].pb(i);
if(v==f[j]) gr[i].pb(j);
v=st[i];
swap(v,u);
while(!is(v,u)){
if(v==st[j]) gr[j].pb(i);
if(v==f[j]) gr[i].pb(j);
v=par[v];
}
}
}
for(int i=0;i<m;i++){
if(!used[i])
dfs(i);
}
cout<<(ok?"Yes":"No")<<'\n';
}
signed main(){
fast_resp;
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |