#include using namespace std; #define rep(i,a,b) for(int i=a;i> g; // vid : id -> vid // head, heavy, parent : unknown // depth : id -> depth // inv : vid -> id vector vid, head, heavy, parent, depth, inv; HLDecomposition(int n) : g(n), vid(n, -1), head(n), heavy(n, -1), parent(n), depth(n), inv(n) {} void add(int u, int v) { g[u].push_back(v); g[v].push_back(u); } void build() { dfs(0, -1); bfs(); } int dfs(int curr, int prev) { parent[curr] = prev; int sub = 1, max_sub = 0; for (int next : g[curr]) if (next != prev) { depth[next] = depth[curr] + 1; int sub_next = dfs(next, curr); sub += sub_next; if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next; } return sub; } void bfs() { int k = 0; queue q({ 0 }); while (!q.empty()) { int h = q.front(); q.pop(); for (int i = h; i != -1; i = heavy[i]) { vid[i] = k++; inv[vid[i]] = i; head[i] = h; for (int j : g[i]) if (j != parent[i] && j != heavy[i]) q.push(j); } } } void foreach(int u, int v, function f) { if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]); if (head[u] != head[v]) foreach(u, parent[head[v]], f); } int ancestor(int u, int d) { // uのd個上の頂点を返す(無いなら0) while (true) { if (depth[head[u]] > depth[u] - d) { d -= depth[u] - depth[head[u]] + 1; if (head[u] == 0) return 0; u = parent[head[u]]; } else return inv[vid[u] - d]; } } int lca(int u, int v) { if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u; return lca(u, parent[head[v]]); } // 頂点 u と頂点 v の距離を求める int distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; } }; //----------------------------------------------------------------------------------- int N, A[101010]; vector E[101010]; int chk(int a, int b, int c) { if (max(A[a], A[c]) < A[b] && A[a] != A[c]) return 1; if (A[b] < min(A[a], A[c]) && A[a] != A[c]) return 1; return 0; } //----------------------------------------------------------------------------------- int cnt[101010]; void dfs(int cu, int pa, HLDecomposition &hld) { if (2 <= hld.depth[cu]) { int a = hld.ancestor(cu, 2); int b = hld.ancestor(cu, 1); int c = cu; cnt[cu] = chk(a, b, c); } for (int to : E[cu]) if (to != pa) dfs(to, cu, hld); } void dfs2(int cu, int pa = -1) { for (int to : E[cu]) if (to != pa) { cnt[to] += cnt[cu]; dfs2(to, cu); } } //----------------------------------------------------------------------------------- int main() { cin >> N; rep(i, 0, N) scanf("%d", &A[i]); HLDecomposition hld(N); rep(i, 0, N - 1) { int a, b; scanf("%d%d", &a, &b); a--; b--; E[a].push_back(b); E[b].push_back(a); hld.add(a, b); } hld.build(); dfs(0, -1, hld); dfs2(0); int Q; cin >> Q; rep(q, 0, Q) { int a, b; scanf("%d%d", &a, &b); a--; b--; int p = hld.lca(a, b); bool ans = true; if (a == p) { int pb = hld.ancestor(b, hld.depth[b] - hld.depth[p] - 1); if (!chk(b, a, pb)) ans = false; int d = hld.depth[b] - hld.depth[p] - 1; if (0 < d) { int c = cnt[b] - cnt[hld.ancestor(b, d)]; if (c != d) ans = false; } if (!chk(a, b, hld.ancestor(b, 1))) ans = false; } else if (b == p) { int pa = hld.ancestor(a, hld.depth[a] - hld.depth[p] - 1); if (!chk(a, b, pa)) ans = false; int d = hld.depth[a] - hld.depth[p] - 1; if (0 < d) { int c = cnt[a] - cnt[hld.ancestor(a, d)]; if (c != d) ans = false; } if (!chk(hld.ancestor(a, 1), a, b)) ans = false; } else { int pa = hld.ancestor(a, hld.depth[a] - hld.depth[p] - 1); int pb = hld.ancestor(b, hld.depth[b] - hld.depth[p] - 1); if (!chk(pa, p, pb)) ans = false; int d = hld.depth[a] - hld.depth[p] - 1; if (0 < d) { int c = cnt[a] - cnt[hld.ancestor(a, d)]; if (c != d) ans = false; } d = hld.depth[b] - hld.depth[p] - 1; if (0 < d) { int c = cnt[b] - cnt[hld.ancestor(b, d)]; if (c != d) ans = false; } if (!chk(hld.ancestor(a, 1), a, b)) ans = false; if (!chk(a, b, hld.ancestor(b, 1))) ans = false; } if (ans) printf("YES\n"); else printf("NO\n"); } }