#include #define _overload(_1,_2,_3,name,...) name #define _rep(i,n) _range(i,0,n) #define _range(i,a,b) for(int i=int(a);i=int(b);--i) #define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__) #define _all(arg) begin(arg),end(arg) #define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg)) #define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary) #define clr(a,b) memset((a),(b),sizeof(a)) #define bit(n) (1LL<<(n)) #define popcount(n) (__builtin_popcountll(n)) templatebool chmax(T &a, const T &b) { return (abool chmin(T &a, const T &b) { return (b graph[vmax]; int value[vmax]; int ok[vmax]; int depth[vmax],par[vmax][20]; void tree_dfs(int v,int p,int d){ par[v][0]=p,depth[v]=d; for(auto &v2:graph[v]) if(v2!=p) tree_dfs(v2,v,d+1); } void init(int n){ tree_dfs(0,-1,0); rep(j,19)rep(i,n){ if(par[i][j]==-1) par[i][j+1]=-1; else par[i][j+1]=par[par[i][j]][j]; } } tuple lca(int u,int v){ if(depth[u] < depth[v]) swap(u,v); rrep(i,20) if((depth[u]-depth[v])>>i&1) u=par[u][i]; if(u==v) return make_tuple(u,-1); rrep(i,20) if(par[u][i]!=par[v][i])u=par[u][i],v=par[v][i]; return make_tuple(u,v); } void dfs(int v,int p,int p2){ ok[v]=0; if(p!=-1) ok[v]+=ok[p]; if(p2!=-1) ok[v]+=check(value[p2],value[p],value[v]); for(auto &v2:graph[v]) if(v2!=p) dfs(v2,v,p); } int trace(int v,int d){ rrep(i,20) if(d>>i&1) v=par[v][i]; return v; } int main(void){ int n; scanf("%d",&n); rep(i,n) scanf("%d",value+i); rep(i,n-1){ int x,y; scanf("%d%d",&x,&y); x--,y--; graph[x].push_back(y); graph[y].push_back(x); } dfs(0,-1,-1); init(n); int q; cin >> q; rep(i,q){ int u,v; scanf("%d%d",&u,&v); u--,v--; if(u>v) swap(u,v); bool ans=true; int a,b,c; tie(a,c)=lca(u,v); if(c==-1){ if(a==u) swap(u,v); const int ud=depth[u]-depth[a]-1; const int vchi=trace(u,ud); const int ucnt=ok[u]-ok[vchi]; if(ucnt!=ud) ans=false; if(par[u][0]!=-1) ans &= check(value[par[u][0]],value[u],value[v]); ans &= check(value[u],value[v],value[vchi]); }else{ b=par[a][0]; ans &= check(value[a],value[b],value[c]); if(par[u][0]!=-1) ans &= check(value[par[u][0]],value[u],value[v]); if(par[v][0]!=-1) ans &= check(value[u],value[v],value[par[v][0]]); const int ud=depth[u]-depth[b]-1; const int ucnt=ok[u]-ok[trace(u,ud)]; if(ucnt!=ud) ans=false; const int vd=depth[v]-depth[b]-1; const int vcnt=ok[v]-ok[trace(v,vd)]; if(vcnt!=vd) ans=false; } if(ans) puts("YES"); else puts("NO"); } return 0; }