//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 120015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
struct tarjan{
int dfn[N],low[N],dft=0,nscc=0,n;
bool ins[N];
stack<int>st;
vector<int>g[N];
void init(int _n){
while (!st.empty()) st.pop();
n=_n; dft=0; nscc=0;
for (int i=0;i<=n+5;i++){
dfn[i]=0; low[i]=0; ins[i]=0;
g[i].clear();
}
}
void addedge(int u,int v){ g[u].push_back(v); }
void dfs(int p){
dfn[p]=low[p]=++dft;
st.push(p); ins[p]=1;
for (auto i:g[p]){
if (!dfn[i]){
dfs(i);
low[p]=min(low[p],low[i]);
}
else if (ins[i]&&dfn[i]<dfn[p])
low[p]=min(low[p],dfn[i]);
}
if (low[p]==dfn[p]){
nscc++;
for (int x=0;x!=p;st.pop()){
x=st.top(); ins[x]=0;
}
}
}
int solve(){
for (int i=1;i<=n;i++){
if (!dfn[i]) dfs(i);
}
return nscc;
}
}topo;
int n,m,st[N],ed[N],son[N],cnt[N],insub[N];
bool vis[N];
vector<int>g[N],path,start,vend,all;
int get_size(int p,int lp){
son[p]=1;
insub[st[p]]++; insub[ed[p]]++;
all.push_back(p);
for (auto i:g[p]){
if (i!=lp&&!vis[i]) son[p]+=get_size(i,p);
}
return son[p];
}
int get_centroid(int p,int lp,int sz){
for (auto i:g[p]){
if (!vis[i]&&i!=lp&&son[i]>sz/2)
return get_centroid(i,p,sz);
}
return p;
}
void same_sub(int p,int lp){
if (st[p]) cnt[st[p]]++;
if (ed[p]) cnt[ed[p]]++;
path.push_back(p);
for (auto i:g[p]){
if (!vis[i]&&i!=lp) same_sub(i,p);
}
}
void build(int p,int lp){
// 結束點先放
if (ed[p]){
//cout<<"add : "<<ed[p]<<'\n';
if (!vend.empty()&&cnt[ed[p]]==1&&insub[ed[p]]==2){
// cout<<"edge1 : "<<ed[p]<<" to "<<vend.back()<<'\n';
topo.addedge(ed[p],vend.back());
}
if (!start.empty()&&cnt[ed[p]]==1&&insub[ed[p]]==2){
// cout<<"edge2 : "<<start.back()<<" to "<<ed[p]<<'\n';
topo.addedge(start.back(),ed[p]);
}
vend.push_back(ed[p]);
}
if (st[p]){
// cout<<"add : "<<st[p]<<'\n';
if (!vend.empty()&&cnt[st[p]]==1&&insub[st[p]]==2){
// cout<<"edge3 : "<<st[p]<<" to "<<vend.back()<<'\n';
topo.addedge(st[p],vend.back());
}
if (!start.empty()&&cnt[st[p]]==1&&insub[st[p]]==2){
// cout<<"edge4 : "<<start.back()<<" to "<<st[p]<<'\n';
topo.addedge(start.back(),st[p]);
}
start.push_back(st[p]);
}
for (auto i:g[p]){
if (!vis[i]&&i!=lp) build(i,p);
}
if (ed[p]) vend.pop_back();
if (st[p]) start.pop_back();
}
void reset_path(){
for (auto i:path){
cnt[st[i]]=0;
cnt[ed[i]]=0;
}
path.clear();
}
void decompose(int p,int lp){
int sz=get_size(p,lp);
int mid=get_centroid(p,lp,sz);
// cout<<p<<" "<<lp<<" -> "<<mid<<' '<<sz<<'\n';
vis[mid]=1;
start.clear(); vend.clear();
if (ed[mid]){
vend.push_back(ed[mid]);
}
if (st[mid]){
if (!vend.empty()) topo.addedge(st[mid],vend.back());
start.push_back(st[mid]);
}
for (auto i:g[mid]){
if (!vis[i]&&i!=lp){
same_sub(i,mid);
build(i,mid);
reset_path();
}
}
for (auto i:all){
insub[st[i]]=0; insub[ed[i]]=0;
cnt[st[i]]=0; cnt[ed[i]]=0;
}
all.clear();
/*
cout<<"after : \n";
for (int i=1;i<=m;i++){
cout<<i<<" -> ";
for (auto j:topo.g[i])
cout<<j<<" ";
cout<<'\n';
}
cout<<"\n";
*/
for (auto i:g[mid]){
if (!vis[i]&&i!=lp)
decompose(i,mid);
}
}
void reset(int n){
start.clear(); vend.clear(); path.clear();
for (int i=0;i<=n+5;i++){
g[i].clear();
st[i]=0; ed[i]=0; vis[i]=0;
son[i]=0; cnt[i]=0;
}
}
signed main(){
fast
int t; cin>>t;
while (t--){
cin>>n;
reset(n);
for (int i=1;i<n;i++){
int a,b; cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
cin>>m;
for (int i=1;i<=m;i++){
int a,b; cin>>a>>b;
st[a]=i; ed[b]=i;
}
topo.init(m);
decompose(1,0);
/*
for (int i=1;i<=m;i++){
cout<<i<<" -> ";
for (auto j:topo.g[i])
cout<<j<<" ";
cout<<'\n';
}
*/
int ans=topo.solve();
if (ans==m) cout<<"Yes\n";
else cout<<"No\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5980 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Incorrect |
24 ms |
6400 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
5980 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
7 ms |
5984 KB |
Output is correct |
8 |
Correct |
6 ms |
6100 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
6064 KB |
Output is correct |
11 |
Correct |
5 ms |
5984 KB |
Output is correct |
12 |
Correct |
4 ms |
5984 KB |
Output is correct |
13 |
Correct |
5 ms |
5984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
5980 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
7 ms |
5984 KB |
Output is correct |
8 |
Correct |
6 ms |
6100 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
6064 KB |
Output is correct |
11 |
Correct |
5 ms |
5984 KB |
Output is correct |
12 |
Correct |
4 ms |
5984 KB |
Output is correct |
13 |
Correct |
5 ms |
5984 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
5 ms |
5980 KB |
Output is correct |
17 |
Correct |
6 ms |
5972 KB |
Output is correct |
18 |
Incorrect |
7 ms |
5984 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
5980 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
7 ms |
5984 KB |
Output is correct |
8 |
Correct |
6 ms |
6100 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
6064 KB |
Output is correct |
11 |
Correct |
5 ms |
5984 KB |
Output is correct |
12 |
Correct |
4 ms |
5984 KB |
Output is correct |
13 |
Correct |
5 ms |
5984 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
5 ms |
5980 KB |
Output is correct |
17 |
Correct |
6 ms |
5972 KB |
Output is correct |
18 |
Incorrect |
7 ms |
5984 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
5980 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
7 ms |
5984 KB |
Output is correct |
8 |
Correct |
6 ms |
6100 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
6064 KB |
Output is correct |
11 |
Correct |
5 ms |
5984 KB |
Output is correct |
12 |
Correct |
4 ms |
5984 KB |
Output is correct |
13 |
Correct |
5 ms |
5984 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
5 ms |
5980 KB |
Output is correct |
17 |
Correct |
6 ms |
5972 KB |
Output is correct |
18 |
Incorrect |
7 ms |
5984 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
4 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5964 KB |
Output is correct |
5 |
Incorrect |
12 ms |
6100 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5980 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Incorrect |
24 ms |
6400 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |