import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } enum LIM_LOGN = 17; int N; int[] A; int[] X, Y; int Q; int[] U, V; int R; int[][] G; int[][] par; int[] dep; int[][] dp; void dfs(int u, int p) { par[0][u] = p; dep[u] = (u == R) ? 0 : (dep[p] + 1); dp[u][0] = (u == R) ? 0 : (A[u] < A[p]) ? ((p == R) ? 1 : ((A[u] != A[par[0][p]]) ? (1 + dp[p][1]) : 1)) : 0; dp[u][1] = (u == R) ? 0 : (A[u] > A[p]) ? ((p == R) ? 1 : ((A[u] != A[par[0][p]]) ? (1 + dp[p][0]) : 1)) : 0; foreach (v; G[u]) { if (v != p) { dfs(v, u); } } } int LCA(int u, int v) { foreach_reverse (e; 0 .. LIM_LOGN) { if (dep[u] - (1 << e) >= dep[v]) { u = par[e][u]; } if (dep[v] - (1 << e) >= dep[u]) { v = par[e][v]; } } foreach_reverse (e; 0 .. LIM_LOGN) { if (par[e][u] != par[e][v]) { u = par[e][u]; v = par[e][v]; } } if (u != v) { u = par[0][u]; v = par[0][v]; } return u; } int up(int u, int k) { foreach (e; 0 .. LIM_LOGN) { if ((k >> e) & 1) { u = par[e][u]; } } return u; } void main() { try { for (; ; ) { N = readInt(); A = new int[N]; foreach (u; 0 .. N) { A[u] = readInt(); } X = new int[N - 1]; Y = new int[N - 1]; foreach (i; 0 .. N - 1) { X[i] = readInt() - 1; Y[i] = readInt() - 1; } Q = readInt(); U = new int[Q]; V = new int[Q]; foreach (q; 0 .. Q) { U[q] = readInt() - 1; V[q] = readInt() - 1; } R = 0; G = new int[][N]; foreach (i; 0 .. N - 1) { G[X[i]] ~= Y[i]; G[Y[i]] ~= X[i]; } par = new int[][](LIM_LOGN, N); dep = new int[N]; dp = new int[][](N, 2); dfs(R, R); foreach (e; 1 .. LIM_LOGN) { foreach (u; 0 .. N) { par[e][u] = par[e - 1][par[e - 1][u]]; } } debug { writeln("dp = ", dp); } foreach (q; 0 .. Q) { const u = U[q]; const v = V[q]; const l = LCA(u, v); const dU = dep[u] - dep[l]; const dV = dep[v] - dep[l]; debug { writeln(u, " ", v, ": ", l, " ", dU, " ", dV); } bool ans; if (dU + dV + 1 >= 3 && (dU + dV + 1) % 2 == 0) { ans = ans || (A[u] < A[v] && dp[u][0] >= dU && dp[v][1] >= dV); ans = ans || (A[u] > A[v] && dp[u][1] >= dU && dp[v][0] >= dV); if (dU == 0) { // u <- <- v const vv = up(v, dV - 1); ans = ans && (A[v] != A[vv]); ans = ans && (A[u] != A[par[0][v]]); } else if (dV == 0) { // u -> -> v const uu = up(u, dU - 1); ans = ans && (A[u] != A[uu]); ans = ans && (A[v] != A[par[0][u]]); } else { // u -> -> l <- <- <- v const uu = up(u, dU - 1); const vv = up(v, dV - 1); ans = ans && (A[uu] != A[vv]); ans = ans && (A[u] != A[par[0][v]]); ans = ans && (A[v] != A[par[0][u]]); } } writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }