#include #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define FORR(i,a,b) for (int i=(a);i>=(b);i--) #define pb push_back #define pcnt __builtin_popcount #define show(x) cout<<#x<<" = "< pii; typedef vector vi; typedef vector vvi; typedef vector vpii; typedef set si; typedef pair pll; typedef vector vl; typedef vector vvl; typedef vector vpll; typedef set sl; typedef __int128_t lll; typedef pair plll; typedef vector vlll; templatestring join(vector&v) {stringstream s;FOR(i,0,sz(v))s<<' '<b)swap(a,b);for(;a>0;b%=a,swap(a,b));return b;} int modpow(ll a,ll n,int m){if(a==0)return a;ll p=1;for(;n>0;n/=2,a=a*a%m)if(n&1)p=p*a%m;return(int)p;} void dout(double d){printf("%.12f\n",d);} const int iinf = 1e9; const ll linf = 1e18; const int mod = 1e9+7; const double pi = acos(-1); const double eps = 1e-10; const int N = 1e5+1; int n, q, A[N], D[N], P[N][18]; bool K[N][18], Y[N]; vi V[N]; bool kdmt(int a, int b, int c){ return (a!=c) && (b < a && b < c || a < b && c < b); } int back(int a, int b){ FOR(i, 0, 18) if(b&(1< D[b]) swap(a, b); b = back(b, D[b]-D[a]); if(a == b) return a; return _parent(a, back(b, D[b]-D[a])); } bool kdmtl(int a, int b){ if(b < 1) return true; FOR(i, 0, 18) if(b&(1< D[b]) swap(a, b); int p = parent(a, b), _a, _b; if(!kdmtl(a, D[a] - D[p] - 1)) return false; if(!kdmtl(b, D[b] - D[p] - 1)) return false; if(a == p){ _a = back(b, D[b] - D[p] - 1); if(!kdmt(A[_a], A[a], A[b])) return false; if(!kdmt(A[a], A[b], A[P[b][0]])) return false; }else{ _a = back(a, D[a] - D[p] - 1); _b = back(b, D[b] - D[p] - 1); if(!kdmt(A[_a], A[p], A[_b])) return false; if(!kdmt(A[P[a][0]], A[a], A[b])) return false; if(!kdmt(A[a], A[b], A[P[b][0]])) return false; } return true; } main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n; FOR(i, 1, n+1) cin >> A[i]; { int x, y; FOR(i, 0, n-1){ cin >> x >> y; V[x].pb(y); V[y].pb(x); } } { queue q[2]; q[0].push(1); Y[1] = true; P[1][0] = 0; int t, d = 1, i = 0; while(!q[i].empty()){ while(!q[i].empty()){ t = q[i].front(); q[i].pop(); D[t] = d; each(itr, V[t]) if(!Y[*itr]){ Y[*itr] = true; P[*itr][0] = t; q[1-i].push(*itr); } } i = 1 - i; d++; } FOR(i, 1, 18)FOR(j, 0, n+1) P[j][i] = P[P[j][i-1]][i-1]; } FOR(i, 0, n+1) if(D[i] > 2) K[i][0] = kdmt(A[i], A[P[i][0]], A[P[i][1]]); FOR(j, 1, 18) FOR(i, 0, n+1) K[i][j] = K[i][j-1] && K[P[i][j-1]][j-1]; cin >> q; { int u, v; FOR(i, 0, q){ cin >> u >> v; cout << (kdmtc(u, v) ? "YES\n" : "NO\n"); } } return 0; }