#include using namespace std; bool isKadomatsu(long long x, long long y, long long z) { if (x == y || y == z || x == z) return false; if (x > y && y < z) return true; if (x < y && y > z) return true; return false; } int n; int a[101010]; vector g[101010]; int parent[24][101010]; int depth[101010]; bool kado[24][101010]; void dfs(int curr, int prev) { parent[0][curr] = prev; kado[0][curr] = true; if (prev != -1 && parent[0][prev] != -1) { int p = prev; int pp = parent[0][prev]; kado[0][curr] = isKadomatsu(a[curr], a[p], a[pp]); } for (int next : g[curr]) if (next != prev) { depth[next] = depth[curr] + 1; dfs(next, curr); } } void build() { dfs(0, -1); for (int i = 0; i < 23; i++) { for (int j = 0; j < n; j++) { if (parent[i][j] != -1) { parent[i + 1][j] = parent[i][parent[i][j]]; kado[i + 1][j] = kado[i][j] && kado[i][parent[i][j]]; } else { parent[i + 1][j] = -1; kado[i + 1][j] = kado[i][j]; } } } } int ancestor(int u, int k) { for (int i = 0; i < 24; i++) if (k & 1 << i) u = parent[i][u]; return u; } int lca(int u, int v) { if (depth[u] < depth[v]) swap(u, v); for (int i = 23; i >= 0; i--) { if (depth[u] - depth[v] >= 1 << i) { u = parent[i][u]; } } if (u == v) return u; for (int i = 23; i >= 0; i--) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } int query(int u, int d) { if (d < 0) return true; bool result = true; for (int i = 0; i < 24; i++) if (d & 1 << i) { result &= kado[i][u]; u = parent[i][u]; result &= kado[0][u]; } return result; } int main() { cin >> n; for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n - 1; i++) { int x, y; scanf("%d %d", &x, &y); x--; y--; g[x].push_back(y); g[y].push_back(x); } build(); int q; cin >> q; for (int i = 0; i < q; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; int l = lca(u, v); bool ans = true; if (parent[0][u] == v || parent[0][v] == u) { ans = false; } else if (l != u && l != v) { int p = ancestor(u, depth[u] - depth[l] - 1); int q = ancestor(v, depth[v] - depth[l] - 1); ans &= isKadomatsu(a[p], a[l], a[q]); ans &= isKadomatsu(a[parent[0][u]], a[u], a[v]); ans &= isKadomatsu(a[parent[0][v]], a[v], a[u]); ans &= query(u, depth[u] - depth[l] - 2); ans &= query(v, depth[v] - depth[l] - 2); } else { if (l == v) swap(u, v); int q = ancestor(v, depth[v] - depth[l] - 1); ans &= query(v, depth[v] - depth[u] - 2); ans &= isKadomatsu(a[q], a[u], a[v]); ans &= isKadomatsu(a[parent[0][v]], a[v], a[u]); } printf("%s\n", ans ? "YES" : "NO"); } }