#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <set> #include <vector> #include <map> #include <queue> #include <set> #include <algorithm> using namespace std; #define MAXN 50010 struct Q { int u,v,id,dir; Q(){} Q(int _u,int _v,int _id,int _dir):u(_u),v(_v),id(_id),dir(_dir){} }; int path[MAXN]; vector <int> e[MAXN]; vector <Q> h[MAXN],q[MAXN]; int maxx[MAXN],minx[MAXN],up[MAXN],down[MAXN],n,ans[MAXN]; int vis[MAXN]; inline void init(){ for(int i=0;i<=n+3;i++){ path[i]=i; vis[i]=up[i]=down[i]=0; e[i].clear(); h[i].clear(); q[i].clear(); } } int Find(int x){ if(x==path[x])return x; int px=path[x]; path[x]=Find(path[x]); up[x]=max(maxx[px]-minx[x],max(up[x],up[px])); down[x]=max(maxx[x]-minx[px],max(down[x],down[px])); maxx[x]=max(maxx[x],maxx[px]); minx[x]=min(minx[x],minx[px]); return path[x]; } void LCA(int u){ vis[u]=1; for(int i=0;i<(int)q[u].size();i++){ Q &next=q[u][i]; if(vis[next.v]){ int lca=Find(next.v); h[lca].push_back(next); } } for(int i=0;i<(int)e[u].size();i++){ int v=e[u][i]; if(!vis[v]){ LCA(v); path[v]=u; } } for(int i=0;i<(int)h[u].size();i++){ Q now=h[u][i]; Find(now.u); Find(now.v); if(!now.dir) swap(now.u,now.v); ans[now.id]=max(up[now.u],down[now.v]); ans[now.id]=max(ans[now.id],maxx[now.v]-minx[now.u]); } } int main() { while(~scanf("%d",&n)){ init(); for(int i=1,x;i<=n;i++){ scanf("%d",&x); maxx[i]=minx[i]=x; } for(int i=1,x,y;i<n;i++){ scanf("%d%d",&x,&y); e[x].push_back(y); e[y].push_back(x); } int qq; scanf("%d",&qq); for(int i=0,x,y;i<qq;i++){ scanf("%d%d",&x,&y); q[x].push_back(Q(x,y,i,1)); q[y].push_back(Q(y,x,i,0)); } LCA(1); for(int i=0;i<qq;i++) printf("%d\n",ans[i]); } return 0; }
|