//#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;
int n,m,st[N],ed[N],fa[N],dep[N],dfn[N],dft=0,low[N],nscc=0,lca[N];
int ac[25][N],in[N],out[N],t=0;
bool ins[N],vis[N][2];
stack<int>s;
pii pos[N];
vector<int>g[N],ng[N],scc[N];
void dfs(int p,int lp){
fa[p]=lp; ac[0][p]=lp;
dep[p]=dep[lp]+1; in[p]=++t;
for (auto i:g[p]){
if (i!=lp) dfs(i,p);
}
out[p]=++t;
}
void build(){
for (int i=1;i<=20;i++){
for (int j=1;j<=n;j++)
ac[i][j]=ac[i-1][ac[i-1][j]];
}
}
bool check(int a,int b){
return in[a]<=in[b]&&out[a]>=out[b];
}
int LCA(int a,int b){
if (check(a,b)) return a;
else if (check(b,a)) return b;
for (int i=20;i>=0;i--){
if (!check(ac[i][a],b)) a=ac[i][a];
}
return ac[0][a];
}
void tarjan(int p){
dfn[p]=low[p]=++dft;
s.push(p); ins[p]=1;
for (auto i:ng[p]){
if (!dfn[i]){
tarjan(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;s.pop()){
x=s.top(); ins[x]=0;
scc[nscc].push_back(x);
}
}
}
int find(int i,bool flag){
if (vis[i][flag]) return fa[lca[i]];
vis[i][flag]=1;
int p=0;
if (flag==0) p=pos[i].x;
else p=pos[i].y;
while (p!=0&&dep[p]>=dep[lca[i]]){
if (st[p]){
ng[st[p]].push_back(i);
p=find(st[p],flag);
}
else p=fa[p];
}
if (flag==0) p=pos[i].x;
else p=pos[i].y;
while (p!=0&&dep[p]>=dep[lca[i]]){
if (ed[p]){
ng[i].push_back(ed[p]);
p=find(ed[p],flag);
}
else p=fa[p];
}
return fa[lca[i]];
}
void reset(){
while (!s.empty()) s.pop();
dft=0; nscc=0; t=0;
for (int i=0;i<=n+5;i++){
in[i]=0; out[i]=0; dfn[i]=0;
low[i]=0; ins[i]=0; lca[i]=0;
dep[i]=0; fa[i]=0; st[i]=0;
ed[i]=0; vis[i][0]=0; vis[i][1]=0;
g[i].clear(); ng[i].clear(); scc[i].clear();
for (int j=0;j<=23;j++)
ac[j][i]=0;
}
}
signed main(){
fast
int q; cin>>q;
while (q--){
reset();
cin>>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++){
cin>>pos[i].x>>pos[i].y;
st[pos[i].x]=i;
ed[pos[i].y]=i;
}
dfs(1,0);
build(); in[0]=0; out[0]=1e18;
for (int i=1;i<=m;i++)
lca[i]=LCA(pos[i].x,pos[i].y);
for (int i=1;i<=m;i++){
find(i,0);
find(i,1);
}
/*
for (int i=1;i<=m;i++){
cout<<i<<" -> ";
for (auto j:ng[i])
cout<<j<<" ";
cout<<'\n';
}
*/
for (int i=1;i<=m;i++){
if (!dfn[i]) tarjan(i);
}
if (nscc==m) cout<<"Yes\n";
else cout<<"No\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8928 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Correct |
4 ms |
8932 KB |
Output is correct |
4 |
Incorrect |
13 ms |
9328 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Incorrect |
7 ms |
9116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Incorrect |
7 ms |
9116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Incorrect |
7 ms |
9116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Incorrect |
7 ms |
9116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8924 KB |
Output is correct |
3 |
Correct |
4 ms |
8948 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Incorrect |
11 ms |
9044 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8928 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Correct |
4 ms |
8932 KB |
Output is correct |
4 |
Incorrect |
13 ms |
9328 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |