#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 120005;
int q;
vector<int> adj[MAXN];
int S[MAXN], T[MAXN];
int dep[MAXN];
int twok[MAXN][20];
int pre[MAXN], post[MAXN];
int cnt;
int lift(int x, int k) {
FOR(i,0,19){
if((1<<i)&k)x=twok[x][i];
if(x==-1)return -1;
}
return x;
}
int lca(int x, int y) {
if(dep[x]>dep[y]) {
int dd=dep[x]-dep[y];
x=lift(x,dd);
} else {
int dd=dep[y]-dep[x];
y=lift(y,dd);
}
if(x==y)return x;
DEC(i,19,0){
if(twok[x][i]!=twok[y][i]){
x=twok[x][i];
y=twok[y][i];
}
}
return twok[x][0];
}
void dfs(int x, int p, int lvl) {
pre[x]=cnt++;
dep[x]=lvl;
twok[x][0]=p;
FOR(i,1,19){
if(twok[x][i-1] == -1)continue;
twok[x][i]=twok[twok[x][i-1]][i-1];
}
for(auto v:adj[x])if(v!=p) {
dfs(v,x,lvl+1);
}
post[x]=cnt-1;
}
set<int> V[MAXN];
bool isanc(int par, int child) {
if(pre[par]<pre[child] && post[par]>=post[child]) return 1;
else return 0;
}
bool ispar(int par, int child) {
if(pre[par]<=pre[child] && post[par]>=post[child]) return 1;
else return 0;
}
bool onpath(int x, int y, int node) {
db3(x,y,node);
// check if node is on path from x to y
if(ispar(y,x)) {
return (ispar(y,node) && !isanc(x,node));
}
if(ispar(x,y)) {
return (ispar(x,node) && !isanc(y,node));
}
int par=lca(x,y);
if(isanc(x,node)||isanc(y,node))return 0;
if(ispar(par,node) && (ispar(node, x) || ispar(node, y))) return 1;
else return 0;
}
int vis[MAXN];
bool dfs(int x) {
bool res=0;
if(vis[x] == 2) {
return 1;
}
vis[x]=2; // active
for(auto v:V[x]){
if(vis[v] == 2) return 1;
if(vis[v] == 1) continue;
else chmax(res, dfs(v));
}
vis[x]=1;
return res;
}
void solve(){
int n; cin >> n;
FOR(i,1,n-1) {
int a,b; cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1,-1,0);
int m; cin >> m;
FOR(i,1,m) cin >> S[i] >> T[i];
vector<int> vs1, vt1;
vector<int> vs2, vt2;
int L1=lca(S[1],T[1]);
int L2=lca(S[2],T[2]);
while(S[1] != L1) {
vs1.pb(S[1]);
S[1]=twok[S[1]][0];
}
vs1.pb(L1);
while(T[1] != L1) {
vt1.pb(T[1]);
T[1]=twok[T[1]][0];
}
reverse(all(vt1)); for(auto x:vt1)vs1.pb(x);
while(S[2] != L2) {
vs2.pb(S[2]);
S[2]=twok[S[2]][0];
}
vs2.pb(L2);
while(T[2] != L2) {
vt2.pb(T[2]);
T[2]=twok[T[2]][0];
}
reverse(all(vt2)); for(auto x:vt2)vs2.pb(x);
bool can = 0;
bool havest=0,haveen=0;
for(auto x:vs2) {
if(x==vs1[0])havest=1;
if(x==vs1.back())haveen=1;
}
if(havest && haveen)can=0;
else can=1;
done:;
if(!can)cout<<"NO\n";
else cout<<"YES\n";
cnt=0;
FOR(i,1,n)FOR(j,0,19)twok[i][j]=-1;
FOR(i,1,n)pre[i]=post[i]=0;
FOR(i,1,n)dep[i]=0;
FOR(i,1,m)vis[i]=0;
FOR(i,1,m)V[i].clear();
FOR(i,1,n)S[i]=T[i]=0;
FOR(i,1,n)adj[i].clear();
}
int32_t main()
{
IAMSPEED
cin >> q;
while(q--)solve();
}
Compilation message
jail.cpp: In function 'void solve()':
jail.cpp:170:2: warning: label 'done' defined but not used [-Wunused-label]
170 | done:;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
8788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
8788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
8788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
8788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
8800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |