이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>*/
template<class S,class T>
bool chmin(S &a,const T &b) {
return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
return a<b?(a=b)==b:false;
}
//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=1e18;
const ll mod=998244353;
const ll N=2e5+7;
const ld eps=1e-9;
vector<vll> g(N);
vector<set<ll>> g2(N);
ll vis[N];
ll up[N][20],d[N];
bool flag;
void dfs(ll v){
for(ll i=1;i<20;i++) up[v][i]=up[up[v][i-1]][i-1];
for(auto i : g[v]){
if(i==up[v][0])continue;
up[i][0]=v;
d[i]=d[v]+1;
dfs(i);
}
}
ll lca(ll a,ll b){
if(d[a]<d[b])swap(a,b);
for(ll i=19;i>=0;i--){
if(d[a]-d[b]>=(1ll<<i)){
a=up[a][i];
}
}
if(a==b)return a;
for(ll i=19;i>=0;i--){
if(d[a]>(1ll<<i) && up[a][i]!=up[b][i]){
a=up[a][i];
b=up[b][i];
}
}
return up[a][0];
}
bool on_path(ll a,ll b,ll x){
ll k=lca(a,x),l=lca(b,x);
ll lc=lca(a,b);
if((k==x && l==lc) || (k==lc && l==x))return true;
return false;
}
bool f[N];
void check(ll v){
f[v]=1;
vis[v]=1;
if(flag)return;
for(auto i : g2[v]){
if(f[i])flag=true;
else if(vis[i])continue;
if(flag){f[v]=0;return;}
check(i);
}
f[v]=0;
}
str solve(){
ll i,j;
ll n;
cin>>n;
flag=0;
for(i=0;i<=n;i++){
g[i].clear();
vis[i]=0;
for(j=0;j<20;j++)up[i][j]=0;
}
ll a,b;
bool bamboo=1;
for(i=1;i<n;i++){
cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
if(a+1!=b)bamboo=false;
}
d[i]=1;
dfs(1);
ll m;
cin>>m;
for(i=1;i<=m;i++)g2[i].clear();
vector<pair<ll,ll> > v(1);
vector<vll> v2(n+5);
for(i=0;i<m;i++){
cin>>a>>b;
v.pb({a,b});
v2[a].pb(i+1);
v2[b].pb(i+1);
}
if(bamboo){
bool f=0;
deque<ll> dq;
map<ll,ll> mp;
for(i=1;i<=n;i++){
if(v2[i].sz>1){
if(mp[v2[i][0]]==mp[v2[i][1]]) return "No";
if(mp[v2[i][1]]) swap(v2[i][0],v2[i][1]);
if(dq[0]!=v2[i][0])return "No";
dq.pof;
dq.pb(v2[i][1]);
mp[dq.bc]++;
}
else if(v2[i].sz){
ll a=v2[i][0];
if(mp[a]){
if(dq[0]!=a) return "No";
dq.pof;
}else {
if(!dq.sz)f=(v[a].fr<v[a].sc);
else if(f!=(v[a].fr<v[a].sc))return "No";
dq.pb(a);
mp[a]++;
}
}
}
return "Yes";
}
for(i=1;i<=m;i++){
for(j=1;j<=m;j++){
if(i==j) continue;
bool s=on_path(v[i].fr,v[i].sc,v[j].fr),t=on_path(v[i].fr,v[i].sc,v[j].sc);
if(s)g2[j].ins(i);
if(t)g2[i].ins(j);
}
}
for(i=1;i<=m;i++){
if(!vis[i]){
check(i);
if(flag)return "No";
}
}
return "Yes";
}
signed main(){
start();
ll t=1;
cin>>t;
while(t--) cout<<solve()<<endl;
return 0;
}
/*
1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
4
1 5
2 6
3 7
4 8
*/
# | 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... |