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]) ? (1 + dp[p][1]) : 0; dp[u][1] = (u == R) ? 0 : (A[u] > A[p]) ? (1 + dp[p][0]) : 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; } 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]; bool ans; const l = LCA(u, v); const dU = dep[u] - dep[l]; const dV = dep[v] - dep[l]; debug { writeln(u, " ", v, ": ", l, " ", dU, " ", dV); } 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); } writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }