//#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],fd[N],fs[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]]++;
fd[st[p]]=0; fs[st[p]]=0;
fd[ed[p]]=0; fs[ed[p]]=0;
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 (cnt[ed[p]]==1&&insub[ed[p]]==2){
for (int i=vend.size()-1;i>=0;i--){
topo.addedge(ed[p],vend[i]);
i=fd[vend[i]];
fd[ed[p]]=i;
}
for (int i=start.size()-1;i>=0;i--){
topo.addedge(start[i],ed[p]);
i=fs[start[i]];
}
}
else fd[ed[p]]=vend.size();
vend.push_back(ed[p]);
}
if (st[p]){
// cout<<"add : "<<st[p]<<'\n';
if (cnt[st[p]]==1&&insub[st[p]]==2){
for (int i=vend.size()-1;i>=0;i--){
topo.addedge(st[p],vend[i]);
i=fd[vend[i]];
}
for (int i=start.size()-1;i>=0;i--){
topo.addedge(start[i],st[p]);
i=fs[start[i]];
fs[st[p]]=i;
}
}
else fs[st[p]]=start.size();
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;
fs[st[i]]=0; fs[ed[i]]=0;
fd[st[i]]=0; fd[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;
fd[st[i]]=0; fs[st[i]]=0;
fd[ed[i]]=0; fs[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;
fd[i]=0; fs[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 |
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 |
28 ms |
6236 KB |
Output is correct |
5 |
Correct |
50 ms |
6776 KB |
Output is correct |
6 |
Correct |
8 ms |
5972 KB |
Output is correct |
7 |
Correct |
7 ms |
5972 KB |
Output is correct |
8 |
Correct |
7 ms |
6116 KB |
Output is correct |
9 |
Correct |
95 ms |
8372 KB |
Output is correct |
10 |
Correct |
141 ms |
26264 KB |
Output is correct |
11 |
Correct |
13 ms |
6100 KB |
Output is correct |
12 |
Correct |
76 ms |
6912 KB |
Output is correct |
13 |
Correct |
227 ms |
60220 KB |
Output is correct |
14 |
Correct |
254 ms |
72220 KB |
Output is correct |
15 |
Correct |
316 ms |
46092 KB |
Output is correct |
16 |
Correct |
626 ms |
79796 KB |
Output is correct |
17 |
Correct |
182 ms |
31680 KB |
Output is correct |
18 |
Correct |
194 ms |
36632 KB |
Output is correct |
19 |
Runtime error |
1351 ms |
1048576 KB |
Execution killed with signal 9 |
20 |
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 |
7 ms |
5972 KB |
Output is correct |
4 |
Correct |
7 ms |
6092 KB |
Output is correct |
5 |
Correct |
6 ms |
5988 KB |
Output is correct |
6 |
Correct |
6 ms |
5992 KB |
Output is correct |
7 |
Correct |
6 ms |
5984 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
5972 KB |
Output is correct |
11 |
Correct |
6 ms |
5984 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
5 ms |
6068 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 |
7 ms |
5972 KB |
Output is correct |
4 |
Correct |
7 ms |
6092 KB |
Output is correct |
5 |
Correct |
6 ms |
5988 KB |
Output is correct |
6 |
Correct |
6 ms |
5992 KB |
Output is correct |
7 |
Correct |
6 ms |
5984 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
5972 KB |
Output is correct |
11 |
Correct |
6 ms |
5984 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
5 ms |
6068 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
5984 KB |
Output is correct |
17 |
Correct |
7 ms |
5972 KB |
Output is correct |
18 |
Correct |
6 ms |
5972 KB |
Output is correct |
19 |
Correct |
4 ms |
5972 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
3 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
6 ms |
6064 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
4 ms |
5972 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 |
7 ms |
5972 KB |
Output is correct |
4 |
Correct |
7 ms |
6092 KB |
Output is correct |
5 |
Correct |
6 ms |
5988 KB |
Output is correct |
6 |
Correct |
6 ms |
5992 KB |
Output is correct |
7 |
Correct |
6 ms |
5984 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
5972 KB |
Output is correct |
11 |
Correct |
6 ms |
5984 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
5 ms |
6068 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
5984 KB |
Output is correct |
17 |
Correct |
7 ms |
5972 KB |
Output is correct |
18 |
Correct |
6 ms |
5972 KB |
Output is correct |
19 |
Correct |
4 ms |
5972 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
3 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
6 ms |
6064 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
4 ms |
5972 KB |
Output is correct |
29 |
Correct |
7 ms |
6116 KB |
Output is correct |
30 |
Correct |
7 ms |
6108 KB |
Output is correct |
31 |
Correct |
7 ms |
6116 KB |
Output is correct |
32 |
Correct |
8 ms |
6100 KB |
Output is correct |
33 |
Correct |
7 ms |
5972 KB |
Output is correct |
34 |
Correct |
5 ms |
6100 KB |
Output is correct |
35 |
Correct |
6 ms |
6100 KB |
Output is correct |
36 |
Correct |
6 ms |
6100 KB |
Output is correct |
37 |
Correct |
5 ms |
5972 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 |
7 ms |
5972 KB |
Output is correct |
4 |
Correct |
7 ms |
6092 KB |
Output is correct |
5 |
Correct |
6 ms |
5988 KB |
Output is correct |
6 |
Correct |
6 ms |
5992 KB |
Output is correct |
7 |
Correct |
6 ms |
5984 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
5972 KB |
Output is correct |
11 |
Correct |
6 ms |
5984 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
5 ms |
6068 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
5984 KB |
Output is correct |
17 |
Correct |
7 ms |
5972 KB |
Output is correct |
18 |
Correct |
6 ms |
5972 KB |
Output is correct |
19 |
Correct |
4 ms |
5972 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
3 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
6 ms |
6064 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
4 ms |
5972 KB |
Output is correct |
29 |
Correct |
7 ms |
6116 KB |
Output is correct |
30 |
Correct |
7 ms |
6108 KB |
Output is correct |
31 |
Correct |
7 ms |
6116 KB |
Output is correct |
32 |
Correct |
8 ms |
6100 KB |
Output is correct |
33 |
Correct |
7 ms |
5972 KB |
Output is correct |
34 |
Correct |
5 ms |
6100 KB |
Output is correct |
35 |
Correct |
6 ms |
6100 KB |
Output is correct |
36 |
Correct |
6 ms |
6100 KB |
Output is correct |
37 |
Correct |
5 ms |
5972 KB |
Output is correct |
38 |
Correct |
103 ms |
8384 KB |
Output is correct |
39 |
Correct |
144 ms |
26288 KB |
Output is correct |
40 |
Correct |
107 ms |
8268 KB |
Output is correct |
41 |
Correct |
92 ms |
7940 KB |
Output is correct |
42 |
Correct |
50 ms |
8552 KB |
Output is correct |
43 |
Correct |
115 ms |
8076 KB |
Output is correct |
44 |
Correct |
17 ms |
6440 KB |
Output is correct |
45 |
Correct |
236 ms |
19308 KB |
Output is correct |
46 |
Correct |
291 ms |
19400 KB |
Output is correct |
47 |
Correct |
158 ms |
23096 KB |
Output is correct |
48 |
Correct |
153 ms |
23080 KB |
Output is correct |
49 |
Correct |
189 ms |
19556 KB |
Output is correct |
50 |
Correct |
199 ms |
19544 KB |
Output is correct |
51 |
Correct |
153 ms |
20616 KB |
Output is correct |
52 |
Correct |
190 ms |
20636 KB |
Output is correct |
53 |
Correct |
32 ms |
7256 KB |
Output is correct |
54 |
Correct |
459 ms |
20040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5976 KB |
Output is correct |
3 |
Correct |
4 ms |
5976 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
14 ms |
6180 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
4 ms |
5972 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
5 ms |
6100 KB |
Output is correct |
13 |
Correct |
36 ms |
6620 KB |
Output is correct |
14 |
Correct |
61 ms |
6796 KB |
Output is correct |
15 |
Correct |
47 ms |
6672 KB |
Output is correct |
16 |
Correct |
232 ms |
19824 KB |
Output is correct |
17 |
Correct |
422 ms |
28620 KB |
Output is correct |
18 |
Correct |
716 ms |
45972 KB |
Output is correct |
19 |
Correct |
274 ms |
20192 KB |
Output is correct |
20 |
Correct |
246 ms |
20120 KB |
Output is correct |
21 |
Correct |
328 ms |
20100 KB |
Output is correct |
22 |
Correct |
250 ms |
25220 KB |
Output is correct |
23 |
Correct |
230 ms |
24488 KB |
Output is correct |
24 |
Correct |
230 ms |
24060 KB |
Output is correct |
25 |
Correct |
214 ms |
24124 KB |
Output is correct |
26 |
Correct |
244 ms |
24096 KB |
Output is correct |
27 |
Correct |
188 ms |
29440 KB |
Output is correct |
28 |
Correct |
200 ms |
37300 KB |
Output is correct |
29 |
Correct |
230 ms |
31764 KB |
Output is correct |
30 |
Correct |
148 ms |
26580 KB |
Output is correct |
31 |
Correct |
185 ms |
27480 KB |
Output is correct |
32 |
Correct |
215 ms |
24916 KB |
Output is correct |
33 |
Correct |
180 ms |
27456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
28 ms |
6236 KB |
Output is correct |
5 |
Correct |
50 ms |
6776 KB |
Output is correct |
6 |
Correct |
8 ms |
5972 KB |
Output is correct |
7 |
Correct |
7 ms |
5972 KB |
Output is correct |
8 |
Correct |
7 ms |
6116 KB |
Output is correct |
9 |
Correct |
95 ms |
8372 KB |
Output is correct |
10 |
Correct |
141 ms |
26264 KB |
Output is correct |
11 |
Correct |
13 ms |
6100 KB |
Output is correct |
12 |
Correct |
76 ms |
6912 KB |
Output is correct |
13 |
Correct |
227 ms |
60220 KB |
Output is correct |
14 |
Correct |
254 ms |
72220 KB |
Output is correct |
15 |
Correct |
316 ms |
46092 KB |
Output is correct |
16 |
Correct |
626 ms |
79796 KB |
Output is correct |
17 |
Correct |
182 ms |
31680 KB |
Output is correct |
18 |
Correct |
194 ms |
36632 KB |
Output is correct |
19 |
Runtime error |
1351 ms |
1048576 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |