题目链接:https://ac.nowcoder.com/acm/problem/23482
题目大意:
图片说明

思路:我们直接得到或者u-x y-v, u-y x-v。这些都可以通过LCA搞定。

#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define rint register int

#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char buf[1<<20],*p1=buf,*p2=buf;
inline int read() {
    int f=0,fu=1;
    char c=getchar();
    while(c<'0'||c>'9') {
        if(c=='-')
            fu=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') {
        f=(f<<3)+(f<<1)+c-48;
        c=getchar();
    }
    return f*fu;
}

inline void print(LL x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x >= 10)
        print(x / 10);
    putchar(x % 10 + '0');
}

const int N = 300015;

struct Edge {
    int to, nxt, val;
} edge[N << 1];


struct RMQ_lca {

    LL w[N];
    int head[N], etot;
    int lg[N << 1], a[N << 1], dfn[N], dep[N], tot;
    int f[N << 1][20];

    void init(int n) {
        etot=tot=0;
        memset(head, 0, sizeof(head[0])*(n+5));
        memset(w, 0, sizeof(w[0])*(n+5));
    }

    void add(int u, int v, int w) {
        edge[++etot] = {v, head[u], w};
        head[u] = etot;
    }

    void dfs(int u, int fa) {
        f[++tot][0] = u, dfn[u] = tot, dep[u] = dep[fa] + 1;
        for(rint i = head[u]; i; i = edge[i].nxt) {
            int v = edge[i].to;
            if (v == fa)
                continue;
            w[v] = w[u] + edge[i].val;
            dfs(v, u);
            f[++tot][0] = u;
        }
    }
    void pre() {
        lg[1] = 0;
        for (rint i = 2; i <= tot; i++)
            lg[i] = lg[i >> 1] + 1;
        for (rint j = 1; j <= 19; j++) {
            for (rint i = 1; i + (1 << j) - 1 <= tot; i++) {
                if (dep[f[i][j - 1]] < dep[f[i + (1 << j - 1)][j - 1]]) {
                    f[i][j] = f[i][j - 1];
                } else {
                    f[i][j] = f[i + (1 << j - 1)][j - 1];
                }
            }
        }
    }
    int LCA(int u, int v) {
        u = dfn[u], v = dfn[v];
        if (u > v)
            swap(u, v);
        int len = lg[v - u + 1];
        if (dep[f[u][len]] < dep[f[v - (1 << len) + 1][len]]) {
            return f[u][len];
        } else {
            return f[v - (1 << len) + 1][len];
        }
    }
    LL dist(int u, int v) {
        return w[u] + w[v] - 2ll * w[LCA(u, v)];
    }

} lca;

int main() {

    int n=read();
    lca.init(n);
    for(int i=1; i<=n-1; i++) {
        int u = read(), v = read(), w = 1;
        lca.add(u, v, w), lca.add(v, u, w);
    }
    int x=read(), y=read();
    lca.dfs(1, 0);
    lca.pre();
    int q=read();
    while (q--) {
        int l = read(), r = read();
        int d=lca.dist(l, r);
        int d1=lca.dist(l, x)+lca.dist(y, r);
        int d2=lca.dist(l, y)+lca.dist(x, r);
        d=min(d, min(d1, d2));
        print(d);
        printf("\n");
    }

    return 0;
}