1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int read(){ int res=0,sign=1; char ch=getchar(); for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-'){sign=-sign;} for(;ch>='0'&&ch<='9';ch=getchar()){res=(res<<3)+(res<<1)+(ch^'0');} return res*sign; }
int n,q;
vector<int> G[N]; int ver[N]; int tin[N],tout[N],timer;
void dfs(int u,int fno){ tin[u]=++timer; for(auto&& v:G[u])if(v!=fno){ dfs(v,u); } tout[u]=timer; }
int ls[N<<5],rs[N<<5],rt[N<<5],sum[N<<5],tot;
#define MID int m=s+((t-s)>>1)
int ins(int s,int t,int x,int _p){ int p=++tot; ls[p]=ls[_p],rs[p]=rs[_p],sum[p]=sum[_p]+1; if(s==t)return p; MID; if(x<=m)ls[p]=ins(s,m,x,ls[p]); else rs[p]=ins(m+1,t,x,rs[p]); return p; }
int query(int s,int t,int u,int v,int l,int r){ if(l<=s&&t<=r)return sum[v]-sum[u]; MID; int res=0; if(l<=m)res+=query(s,m,ls[u],ls[v],l,r); if(r>m)res+=query(m+1,t,rs[u],rs[v],l,r); return res; }
void solve(){ n=read(),q=read(); for(int i=1;i<=n;++i)G[i].clear(); tot=0,timer=0; for(int i=1,u,v;i<=n-1;++i){ u=read(),v=read(); G[u].push_back(v); G[v].push_back(u); } dfs(1,0); for(int i=1;i<=n;++i)ver[i]=read(),rt[i]=ins(1,n,tin[ver[i]],rt[i-1]); for(int _=1;_<=q;++_){ int l=read(),r=read(),x=read(); int L=tin[x],R=tout[x]; int res=query(1,n,rt[l-1],rt[r],L,R); if(res>=1)puts("YES"); else puts("NO"); } puts(""); }
int main(){ int t=read(); while(t--)solve(); return 0; }
|