//#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;
bool ins[N];
stack<int>s;
pii pos[N];
vector<int>g[N],ng[N],scc[N];
void dfs(int p,int lp){
fa[p]=lp;
dep[p]=dep[lp]+1;
for (auto i:g[p]){
if (i!=lp) dfs(i,p);
}
}
int LCA(int a,int b){
while (a!=b){
if (dep[a]>dep[b]) a=fa[a];
else b=fa[b];
}
return a;
}
int find_first_start(int x,int lca){
if (dep[x]<dep[lca]) return 0;
while (x!=lca&&!st[x]) x=fa[x];
if (st[x]) return st[x];
else return 0;
}
int find_first_end(int x,int lca){
if (dep[x]<dep[lca]) return 0;
while (x!=lca&&!ed[x]) x=fa[x];
if (ed[x]) return ed[x];
else return 0;
}
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);
}
}
}
void reset(){
nscc=0; dft=0;
while (!s.empty()) s.pop();
for (int i=0;i<=m+5;i++){
fa[i]=0; dfn[i]=0; low[i]=0; st[i]=0;
dep[i]=0; ins[i]=0; ed[i]=0;
ng[i].clear();
scc[i].clear();
}
for (int i=0;i<=n+5;i++){
g[i].clear();
}
}
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);
}
dfs(1,0);
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;
}
for (int i=1;i<=m;i++){
int lca=LCA(pos[i].x,pos[i].y);
int f=find_first_start(fa[pos[i].x],lca);
if (f){
ng[f].push_back(i);
}
f=find_first_start(pos[i].y,lca);
if (f){
ng[f].push_back(i);
}
int d=find_first_end(pos[i].x,lca);
if (d){
ng[i].push_back(d);
}
d=find_first_end(fa[pos[i].y],lca);
if (d){
ng[i].push_back(d);
}
}
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 |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Correct |
4 ms |
8788 KB |
Output is correct |
4 |
Incorrect |
14 ms |
9172 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8792 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Incorrect |
6 ms |
8892 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8792 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Incorrect |
6 ms |
8892 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8792 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Incorrect |
6 ms |
8892 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8792 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Incorrect |
6 ms |
8892 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8800 KB |
Output is correct |
2 |
Correct |
4 ms |
8788 KB |
Output is correct |
3 |
Correct |
4 ms |
8800 KB |
Output is correct |
4 |
Correct |
5 ms |
8800 KB |
Output is correct |
5 |
Incorrect |
9 ms |
8880 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
5 ms |
8788 KB |
Output is correct |
3 |
Correct |
4 ms |
8788 KB |
Output is correct |
4 |
Incorrect |
14 ms |
9172 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |