#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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16472 KB |
Output is correct |
2 |
Correct |
3 ms |
16472 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
11 ms |
16476 KB |
Output is correct |
5 |
Correct |
25 ms |
17156 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16476 KB |
Output is correct |
8 |
Correct |
5 ms |
16560 KB |
Output is correct |
9 |
Correct |
25 ms |
20636 KB |
Output is correct |
10 |
Correct |
45 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
16728 KB |
Output is correct |
12 |
Correct |
28 ms |
17500 KB |
Output is correct |
13 |
Correct |
66 ms |
58820 KB |
Output is correct |
14 |
Correct |
52 ms |
55936 KB |
Output is correct |
15 |
Correct |
58 ms |
55052 KB |
Output is correct |
16 |
Correct |
68 ms |
58304 KB |
Output is correct |
17 |
Correct |
83 ms |
60848 KB |
Output is correct |
18 |
Correct |
112 ms |
65732 KB |
Output is correct |
19 |
Correct |
64 ms |
60540 KB |
Output is correct |
20 |
Correct |
57 ms |
58212 KB |
Output is correct |
21 |
Correct |
69 ms |
56772 KB |
Output is correct |
22 |
Correct |
56 ms |
56776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16476 KB |
Output is correct |
2 |
Correct |
3 ms |
16476 KB |
Output is correct |
3 |
Correct |
4 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
16476 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16628 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
7 ms |
16476 KB |
Output is correct |
10 |
Correct |
4 ms |
16472 KB |
Output is correct |
11 |
Correct |
4 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
16476 KB |
Output is correct |
13 |
Correct |
4 ms |
16624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16476 KB |
Output is correct |
2 |
Correct |
3 ms |
16476 KB |
Output is correct |
3 |
Correct |
4 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
16476 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16628 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
7 ms |
16476 KB |
Output is correct |
10 |
Correct |
4 ms |
16472 KB |
Output is correct |
11 |
Correct |
4 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
16476 KB |
Output is correct |
13 |
Correct |
4 ms |
16624 KB |
Output is correct |
14 |
Correct |
4 ms |
16476 KB |
Output is correct |
15 |
Correct |
3 ms |
16476 KB |
Output is correct |
16 |
Correct |
4 ms |
16628 KB |
Output is correct |
17 |
Correct |
4 ms |
16476 KB |
Output is correct |
18 |
Correct |
4 ms |
16476 KB |
Output is correct |
19 |
Correct |
3 ms |
16476 KB |
Output is correct |
20 |
Correct |
4 ms |
16476 KB |
Output is correct |
21 |
Correct |
5 ms |
16476 KB |
Output is correct |
22 |
Correct |
4 ms |
16476 KB |
Output is correct |
23 |
Correct |
4 ms |
16476 KB |
Output is correct |
24 |
Correct |
4 ms |
16472 KB |
Output is correct |
25 |
Correct |
6 ms |
14680 KB |
Output is correct |
26 |
Correct |
4 ms |
16476 KB |
Output is correct |
27 |
Correct |
4 ms |
14684 KB |
Output is correct |
28 |
Correct |
4 ms |
16608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16476 KB |
Output is correct |
2 |
Correct |
3 ms |
16476 KB |
Output is correct |
3 |
Correct |
4 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
16476 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16628 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
7 ms |
16476 KB |
Output is correct |
10 |
Correct |
4 ms |
16472 KB |
Output is correct |
11 |
Correct |
4 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
16476 KB |
Output is correct |
13 |
Correct |
4 ms |
16624 KB |
Output is correct |
14 |
Correct |
4 ms |
16476 KB |
Output is correct |
15 |
Correct |
3 ms |
16476 KB |
Output is correct |
16 |
Correct |
4 ms |
16628 KB |
Output is correct |
17 |
Correct |
4 ms |
16476 KB |
Output is correct |
18 |
Correct |
4 ms |
16476 KB |
Output is correct |
19 |
Correct |
3 ms |
16476 KB |
Output is correct |
20 |
Correct |
4 ms |
16476 KB |
Output is correct |
21 |
Correct |
5 ms |
16476 KB |
Output is correct |
22 |
Correct |
4 ms |
16476 KB |
Output is correct |
23 |
Correct |
4 ms |
16476 KB |
Output is correct |
24 |
Correct |
4 ms |
16472 KB |
Output is correct |
25 |
Correct |
6 ms |
14680 KB |
Output is correct |
26 |
Correct |
4 ms |
16476 KB |
Output is correct |
27 |
Correct |
4 ms |
14684 KB |
Output is correct |
28 |
Correct |
4 ms |
16608 KB |
Output is correct |
29 |
Correct |
4 ms |
14680 KB |
Output is correct |
30 |
Correct |
27 ms |
16732 KB |
Output is correct |
31 |
Correct |
29 ms |
14804 KB |
Output is correct |
32 |
Correct |
22 ms |
16472 KB |
Output is correct |
33 |
Correct |
8 ms |
16724 KB |
Output is correct |
34 |
Correct |
52 ms |
16736 KB |
Output is correct |
35 |
Correct |
67 ms |
16876 KB |
Output is correct |
36 |
Correct |
41 ms |
16692 KB |
Output is correct |
37 |
Correct |
28 ms |
16476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16476 KB |
Output is correct |
2 |
Correct |
3 ms |
16476 KB |
Output is correct |
3 |
Correct |
4 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
16476 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16628 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
7 ms |
16476 KB |
Output is correct |
10 |
Correct |
4 ms |
16472 KB |
Output is correct |
11 |
Correct |
4 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
16476 KB |
Output is correct |
13 |
Correct |
4 ms |
16624 KB |
Output is correct |
14 |
Correct |
4 ms |
16476 KB |
Output is correct |
15 |
Correct |
3 ms |
16476 KB |
Output is correct |
16 |
Correct |
4 ms |
16628 KB |
Output is correct |
17 |
Correct |
4 ms |
16476 KB |
Output is correct |
18 |
Correct |
4 ms |
16476 KB |
Output is correct |
19 |
Correct |
3 ms |
16476 KB |
Output is correct |
20 |
Correct |
4 ms |
16476 KB |
Output is correct |
21 |
Correct |
5 ms |
16476 KB |
Output is correct |
22 |
Correct |
4 ms |
16476 KB |
Output is correct |
23 |
Correct |
4 ms |
16476 KB |
Output is correct |
24 |
Correct |
4 ms |
16472 KB |
Output is correct |
25 |
Correct |
6 ms |
14680 KB |
Output is correct |
26 |
Correct |
4 ms |
16476 KB |
Output is correct |
27 |
Correct |
4 ms |
14684 KB |
Output is correct |
28 |
Correct |
4 ms |
16608 KB |
Output is correct |
29 |
Correct |
4 ms |
14680 KB |
Output is correct |
30 |
Correct |
27 ms |
16732 KB |
Output is correct |
31 |
Correct |
29 ms |
14804 KB |
Output is correct |
32 |
Correct |
22 ms |
16472 KB |
Output is correct |
33 |
Correct |
8 ms |
16724 KB |
Output is correct |
34 |
Correct |
52 ms |
16736 KB |
Output is correct |
35 |
Correct |
67 ms |
16876 KB |
Output is correct |
36 |
Correct |
41 ms |
16692 KB |
Output is correct |
37 |
Correct |
28 ms |
16476 KB |
Output is correct |
38 |
Correct |
25 ms |
20644 KB |
Output is correct |
39 |
Correct |
52 ms |
51392 KB |
Output is correct |
40 |
Correct |
1725 ms |
23804 KB |
Output is correct |
41 |
Correct |
1271 ms |
20228 KB |
Output is correct |
42 |
Correct |
1266 ms |
23600 KB |
Output is correct |
43 |
Correct |
42 ms |
20608 KB |
Output is correct |
44 |
Correct |
1152 ms |
17236 KB |
Output is correct |
45 |
Correct |
153 ms |
44500 KB |
Output is correct |
46 |
Correct |
152 ms |
44372 KB |
Output is correct |
47 |
Correct |
270 ms |
47952 KB |
Output is correct |
48 |
Correct |
250 ms |
48124 KB |
Output is correct |
49 |
Correct |
197 ms |
44624 KB |
Output is correct |
50 |
Correct |
195 ms |
44648 KB |
Output is correct |
51 |
Correct |
70 ms |
45648 KB |
Output is correct |
52 |
Correct |
72 ms |
45672 KB |
Output is correct |
53 |
Correct |
862 ms |
19676 KB |
Output is correct |
54 |
Correct |
116 ms |
45136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16476 KB |
Output is correct |
2 |
Correct |
3 ms |
16472 KB |
Output is correct |
3 |
Correct |
3 ms |
16472 KB |
Output is correct |
4 |
Correct |
3 ms |
16476 KB |
Output is correct |
5 |
Correct |
8 ms |
16472 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16476 KB |
Output is correct |
8 |
Correct |
3 ms |
16476 KB |
Output is correct |
9 |
Correct |
3 ms |
16472 KB |
Output is correct |
10 |
Correct |
4 ms |
16476 KB |
Output is correct |
11 |
Correct |
4 ms |
16472 KB |
Output is correct |
12 |
Correct |
42 ms |
16696 KB |
Output is correct |
13 |
Correct |
448 ms |
17004 KB |
Output is correct |
14 |
Correct |
208 ms |
17236 KB |
Output is correct |
15 |
Correct |
331 ms |
17232 KB |
Output is correct |
16 |
Execution timed out |
5051 ms |
45776 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16472 KB |
Output is correct |
2 |
Correct |
3 ms |
16472 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
11 ms |
16476 KB |
Output is correct |
5 |
Correct |
25 ms |
17156 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16476 KB |
Output is correct |
8 |
Correct |
5 ms |
16560 KB |
Output is correct |
9 |
Correct |
25 ms |
20636 KB |
Output is correct |
10 |
Correct |
45 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
16728 KB |
Output is correct |
12 |
Correct |
28 ms |
17500 KB |
Output is correct |
13 |
Correct |
66 ms |
58820 KB |
Output is correct |
14 |
Correct |
52 ms |
55936 KB |
Output is correct |
15 |
Correct |
58 ms |
55052 KB |
Output is correct |
16 |
Correct |
68 ms |
58304 KB |
Output is correct |
17 |
Correct |
83 ms |
60848 KB |
Output is correct |
18 |
Correct |
112 ms |
65732 KB |
Output is correct |
19 |
Correct |
64 ms |
60540 KB |
Output is correct |
20 |
Correct |
57 ms |
58212 KB |
Output is correct |
21 |
Correct |
69 ms |
56772 KB |
Output is correct |
22 |
Correct |
56 ms |
56776 KB |
Output is correct |
23 |
Correct |
3 ms |
16476 KB |
Output is correct |
24 |
Correct |
3 ms |
16476 KB |
Output is correct |
25 |
Correct |
4 ms |
16476 KB |
Output is correct |
26 |
Correct |
4 ms |
16476 KB |
Output is correct |
27 |
Correct |
4 ms |
16476 KB |
Output is correct |
28 |
Correct |
4 ms |
16476 KB |
Output is correct |
29 |
Correct |
4 ms |
16628 KB |
Output is correct |
30 |
Correct |
4 ms |
14684 KB |
Output is correct |
31 |
Correct |
7 ms |
16476 KB |
Output is correct |
32 |
Correct |
4 ms |
16472 KB |
Output is correct |
33 |
Correct |
4 ms |
16476 KB |
Output is correct |
34 |
Correct |
4 ms |
16476 KB |
Output is correct |
35 |
Correct |
4 ms |
16624 KB |
Output is correct |
36 |
Correct |
4 ms |
16476 KB |
Output is correct |
37 |
Correct |
3 ms |
16476 KB |
Output is correct |
38 |
Correct |
4 ms |
16628 KB |
Output is correct |
39 |
Correct |
4 ms |
16476 KB |
Output is correct |
40 |
Correct |
4 ms |
16476 KB |
Output is correct |
41 |
Correct |
3 ms |
16476 KB |
Output is correct |
42 |
Correct |
4 ms |
16476 KB |
Output is correct |
43 |
Correct |
5 ms |
16476 KB |
Output is correct |
44 |
Correct |
4 ms |
16476 KB |
Output is correct |
45 |
Correct |
4 ms |
16476 KB |
Output is correct |
46 |
Correct |
4 ms |
16472 KB |
Output is correct |
47 |
Correct |
6 ms |
14680 KB |
Output is correct |
48 |
Correct |
4 ms |
16476 KB |
Output is correct |
49 |
Correct |
4 ms |
14684 KB |
Output is correct |
50 |
Correct |
4 ms |
16608 KB |
Output is correct |
51 |
Correct |
4 ms |
14680 KB |
Output is correct |
52 |
Correct |
27 ms |
16732 KB |
Output is correct |
53 |
Correct |
29 ms |
14804 KB |
Output is correct |
54 |
Correct |
22 ms |
16472 KB |
Output is correct |
55 |
Correct |
8 ms |
16724 KB |
Output is correct |
56 |
Correct |
52 ms |
16736 KB |
Output is correct |
57 |
Correct |
67 ms |
16876 KB |
Output is correct |
58 |
Correct |
41 ms |
16692 KB |
Output is correct |
59 |
Correct |
28 ms |
16476 KB |
Output is correct |
60 |
Correct |
25 ms |
20644 KB |
Output is correct |
61 |
Correct |
52 ms |
51392 KB |
Output is correct |
62 |
Correct |
1725 ms |
23804 KB |
Output is correct |
63 |
Correct |
1271 ms |
20228 KB |
Output is correct |
64 |
Correct |
1266 ms |
23600 KB |
Output is correct |
65 |
Correct |
42 ms |
20608 KB |
Output is correct |
66 |
Correct |
1152 ms |
17236 KB |
Output is correct |
67 |
Correct |
153 ms |
44500 KB |
Output is correct |
68 |
Correct |
152 ms |
44372 KB |
Output is correct |
69 |
Correct |
270 ms |
47952 KB |
Output is correct |
70 |
Correct |
250 ms |
48124 KB |
Output is correct |
71 |
Correct |
197 ms |
44624 KB |
Output is correct |
72 |
Correct |
195 ms |
44648 KB |
Output is correct |
73 |
Correct |
70 ms |
45648 KB |
Output is correct |
74 |
Correct |
72 ms |
45672 KB |
Output is correct |
75 |
Correct |
862 ms |
19676 KB |
Output is correct |
76 |
Correct |
116 ms |
45136 KB |
Output is correct |
77 |
Correct |
3 ms |
16476 KB |
Output is correct |
78 |
Correct |
3 ms |
16472 KB |
Output is correct |
79 |
Correct |
3 ms |
16472 KB |
Output is correct |
80 |
Correct |
3 ms |
16476 KB |
Output is correct |
81 |
Correct |
8 ms |
16472 KB |
Output is correct |
82 |
Correct |
4 ms |
16476 KB |
Output is correct |
83 |
Correct |
4 ms |
16476 KB |
Output is correct |
84 |
Correct |
3 ms |
16476 KB |
Output is correct |
85 |
Correct |
3 ms |
16472 KB |
Output is correct |
86 |
Correct |
4 ms |
16476 KB |
Output is correct |
87 |
Correct |
4 ms |
16472 KB |
Output is correct |
88 |
Correct |
42 ms |
16696 KB |
Output is correct |
89 |
Correct |
448 ms |
17004 KB |
Output is correct |
90 |
Correct |
208 ms |
17236 KB |
Output is correct |
91 |
Correct |
331 ms |
17232 KB |
Output is correct |
92 |
Execution timed out |
5051 ms |
45776 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |