#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; const int INF = 1e9; const ll LINF = 1e18; inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; } template ostream& operator << (ostream& out,const pair& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template ostream& operator << (ostream& out,const vector V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template ostream& operator << (ostream& out,const vector > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template ostream& operator << (ostream& out,const map mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* 問題文============================================================ ================================================================= 解説============================================================= ref : https://drive.google.com/file/d/0B8GpBQ5LDTd6QjZIY1VUckxWZ0U/view ================================================================ */ map A; bool isKadomatsu(ll a,ll b,ll c){ if(a == 0 || b == 0 || c == 0) return false; if(a == b || b == c || c == a) return false; if(a > b && c > b) return true; if(a < b && c < b) return true; return false; } struct edge{ ll u,v,c; edge():u(0),v(0),c(0){} edge(ll u,ll v,ll c):u(u),v(v),c(c){} }; typedef vector> Graph; Graph G; class LCA{ public: const ll n = 0; const ll log2_n = 0; vector> parent; vector depth; LCA(); LCA(const Graph &g,ll root):n((ll)g.size()),log2_n(log2(n)+1),parent(log2_n,vector(n)),depth(n){ dfs(g,root,-1,0); for(int k = 0; k+1 < log2_n; k++){ for(int v = 0; v < (int)g.size();v++){ if(parent[k][v] < 0) parent[k+1][v] = -1; else parent[k+1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g,ll v,ll p,ll d){ parent[0][v] = p; depth[v] = d; for(auto &e :g[v]){ if(e.v != p) dfs(g, e.v, v, d+1); } } ll get(ll u,ll v){ if(depth[u] > depth[v]) swap(u,v); for(int k = 0; k < log2_n; k++){ if(((depth[v]-depth[u])>>k) & 1) v = parent[k][v]; } if(u == v) return u; for(int k = (int)log2_n - 1; k >= 0; k--){ if(parent[k][u] != parent[k][v]){ u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } ll get_parent(ll v,ll p){ ll d = depth[v] - depth[p] - 1; for(int k = 0; k < log2_n; k++){ if((d>>k) & 1) v = parent[k][v]; } return v; } }; vector> tG; vector checked; void make_tree(int n,vector>& tG,vector& checked){ checked[n] = true; for(auto next:tG[n]){ if(checked[next]) continue; G[n].push_back(edge(n,next,0)); make_tree(next,tG,checked); } } void solve(){ int N; cin >> N; for(int i = 0; i < N;i++){ ll _A; cin >> _A; A[i] = _A; } //vector A(N); for(auto& in:A) cin >> in; tG.resize(N); checked.resize(N); for(int i = 0; i < N-1;i++){ int x,y; cin >> x >> y; x--;y--; tG[x].push_back(y); tG[y].push_back(x); } G.resize(N); int root = 0; make_tree(root,tG,checked); LCA lca(G,root); vector> kadomatsu(lca.log2_n,vector(lca.n)); for(ll n = 0; n < N;n++){ ll pn = lca.parent[0][n], ppn = lca.parent[0][pn]; if(isKadomatsu(A[n],A[pn],A[ppn])){ kadomatsu[0][n] = 1; } } for(int k = 0; k+1 < lca.log2_n; k++){ for(int v = 0; v < N;v++){ kadomatsu[k+1][v] = kadomatsu[k][v] & kadomatsu[k][lca.parent[k][v]]; } } //cout << kadomatsu << endl; int Q; cin >> Q; while(Q--){ ll u,v; cin >> u >> v; u--; v--; ll p = lca.get(u,v); ll pu = lca.get_parent(u, p); ll pv = lca.get_parent(v, p); ll qu = lca.parent[0][u]; ll qv = lca.parent[0][v]; bool ok = true; if(qu != -1) ok &= isKadomatsu(A[qu], A[u], A[v]); if(pv != -1) ok &= isKadomatsu(A[u], A[v], A[qv]); if(pu != -1 && pv != -1) ok &= isKadomatsu(A[pu], A[p], A[pv]); ll dv = max(lca.depth[v] - lca.depth[p] -2,0LL); for(int k = 0; k < lca.log2_n; k++){ if((dv>>k) & 1){ ok &= kadomatsu[k][v]; v = lca.parent[k][v]; } } ll du = max(lca.depth[u] - lca.depth[p] - 2,0LL); for(int k = 0; k < lca.log2_n; k++){ if((du>>k) & 1){ ok &= kadomatsu[k][u]; u = lca.parent[k][u]; } } if(ok){ cout << "YES" << endl; }else{ cout << "NO" << endl; } } // cout << tG << endl; // for(int i = 0; i < N;i++){ // cout << i << " : "; for(auto v:G[i]) cout << v.v << " "; cout << endl; // } } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); solve(); return 0; }