1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #include <cstdio> #include <vector> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const int N = 2e5 + 10, C = 5e4 + 10, K = 20; int n, m, c, p[N], d[N], w[N]; int st[N], pre[N], up[N][K], down[N][K]; vector<int> g[N]; struct Query { int s, t, l, r, v; } q[N]; struct Check { int v, id; }; vector<Check> h[N]; namespace LCA { int stmp, id[N], st[N << 1][K], lg[N << 1]; void dfs(int x) { st[++stmp][0] = x; id[x] = stmp; for (int i : g[x]) if (!d[i]) { d[i] = d[x] + 1; dfs(i); st[++stmp][0] = x; } } int dmin(int &x, int &y) { return d[x] < d[y] ? x : y; } void init() { d[1] = 1; dfs(1); for (int i = 2; i <= stmp; i++) lg[i] = lg[i >> 1] + 1; for (int k = 1; (1 << k) <= stmp; k++) for (int i = 1; i + (1 << k) - 1 <= stmp; i++) st[i][k] = dmin(st[i][k - 1], st[i + (1 << k - 1)][k - 1]); } int lca(int x, int y) { x = id[x], y = id[y]; if (x > y) swap(x, y); int k = lg[y - x + 1]; return dmin(st[x][k], st[y - (1 << k) + 1][k]); } } void init(int x = 1, int fa = 1) { up[x][0] = pre[w[x] + 1]; down[x][0] = pre[w[x] - 1]; for (int i = 1; i < K; i++) up[x][i] = up[up[x][i - 1]][i - 1]; for (int i = 1; i < K; i++) down[x][i] = down[down[x][i - 1]][i - 1]; int tmp = pre[w[x]]; pre[w[x]] = x; st[x] = pre[1]; for (int i : g[x]) (i != fa) && (init(i, x), 0); pre[w[x]] = tmp; } void dfs(int x = 1, int fa = 0) { int tmp = pre[w[x]]; pre[w[x]] = x; for (Check i : h[x]) st[i.id] = pre[i.v]; for (int i : g[x]) (i != fa) && (dfs(i, x), 0); pre[w[x]] = tmp; } int main() { read(n), read(m), read(c); for (int i = 1, a; i <= c; i++) read(a), p[a] = i; for (int i = 1, a; i <= n; i++) read(a), w[i] = p[a]; for (int i = 1, a, b; i < n; i++) { read(a), read(b); g[a].push_back(b); g[b].push_back(a); } LCA::init(); init(); read(m); for (int i = 1, s, t, v; i <= m; i++) { read(s), read(t); int lca = LCA::lca(s, t); s = st[s], v = d[s] >= d[lca]; for (int k = K - 1; ~k; k--) d[up[s][k]] >= d[lca] && (s = up[s][k], v += 1 << k); q[i].s = lca, q[i].t = t, q[i].l = q[i].v = v, q[i].r = c; } for (int times = 20; times; times--) { for (int i = 1; i <= n; i++) h[i].clear(); for (int i = 1; i <= m; i++) { int mid = q[i].l + q[i].r + 1 >> 1; h[q[i].t].push_back((Check){mid, i}); } for (int i = 0; i <= c + 1; i++) pre[i] = 0; dfs(); for (int i = 1; i <= m; i++) { int s = st[i], t = q[i].s, mid = q[i].l + q[i].r + 1 >> 1; int ans = mid - (d[s] > d[t]); for (int k = K - 1; ~k; k--) d[down[s][k]] > d[t] && (s = down[s][k], ans -= 1 << k); ans <= q[i].v ? q[i].l = mid : q[i].r = mid - 1; } } for (int i = 1; i <= m; i++) write(q[i].l), puts(""); return 0; }
|