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
| #include<bits/stdc++.h> using namespace std; #define ll long long const int N = 1000010, M = N << 1; int n, m; int h[N], e[M], ne[M], idx; void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; } int dep[N], fa[N], sz[N], son[N]; int id[N], nw[N], top[N], cnt; void dfs1(int p, int father, int depth) { dep[p] = depth, fa[p] = father, sz[p] = 1; for (int i = h[p]; ~i; i = ne[i]) { int j = e[i]; if (j == father)continue; dfs1(j, p, depth + 1); sz[p] += sz[j]; if (sz[j] > sz[son[p]])son[p] = j; } } void dfs2(int p, int t) { id[p] = ++cnt, nw[cnt] = p, top[p] = t; if (!son[p])return; dfs2(son[p], t); for (int i = h[p]; ~i; i = ne[i]) { int j = e[i]; if (j == fa[p] || j == son[p])continue; dfs2(j, j); } } int lca(int p, int q) { while (top[p] != top[q]) { if (dep[top[p]] < dep[top[q]])swap(p, q); p = fa[top[p]]; } if (dep[p] < dep[q])swap(p, q); return q; } int jump(int p, int k) { while (dep[p] - dep[top[p]] + 1 <= k) { k -= dep[p] - dep[top[p]] + 1; p = fa[top[p]]; } return nw[id[p] - k]; } int main() { memset(h, -1, sizeof(h)); int u; scanf("%d%d%d", &n, &u, &m); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); add(u, v), add(v, u); } dfs1(1, 0, 1); dfs2(1, 1); while (m--) { int v, step; scanf("%d%d", &v, &step); int x = lca(u, v); int dis1 = dep[u] - dep[x], dis2 = dep[v] - dep[x]; if (step >= dis1 + dis2) { u = v; printf("%d ", u); } else if (step == dis1) { u = x; printf("%d ", u); } else if (step < dis1) { u = jump(u, step); printf("%d ", u); } else { u = jump(v, dis2 - (step - dis1)); printf("%d ", u); } } return 0; }
|