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
| #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <utility> #ifdef XLor #define dbg(args...) do {cout << #args << " -> "; err(args);} while (0) #else #define dbg(...) #endif void err() {std::cout << std::endl;} template<typename T, typename...Args> void err(T a, Args...args){std::cout << a << ' '; err(args...);} #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 300000 + 5;
int n, m; vector<int> edge[maxn];
int dep[maxn], son[maxn], top[maxn], len[maxn], maxd[maxn], fa[maxn][20]; void dfs(int u, int f) { maxd[u] = dep[u] = dep[f] + 1; fa[u][0] = f; son[u] = 0; for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (int v: edge[u]) { if (v == f) continue; dfs(v, u); if (maxd[v] > maxd[son[u]]) son[u] = v, maxd[u] = maxd[v]; } } void dfs(int u, int f, int tp, int l) { top[u] = tp; len[u] = l; if (son[u]) { dfs(son[u], u, tp, l + 1); len[u] = len[son[u]]; } for (int& v: edge[u]) { if (v == f || v == son[u]) continue; dfs(v, u, v, 1); } } bool vis[maxn]; int highbit[maxn]; vector<int> up[maxn], dwn[maxn]; void init() { dfs(1, 0); dfs(1, 0, 1, 1); for (int i = 1; i <= n; i++) { int tp = top[i]; if (!vis[tp]) { vis[tp] = 1; int l = 0, now = tp; while (l < len[tp] && now) { now = fa[now][0]; l++; up[tp].push_back(now); } l = 0, now = tp; while (l < len[tp]) { now = son[now]; l++; dwn[tp].push_back(now); } } } int mx = 1; for (int i = 1; i <= n; i++) { if (i >> mx & 1) mx++; highbit[i] = mx - 1; } } int qkth(int u, int k) { if (k > dep[u]) return 0; if (k == 0) return u; u = fa[u][highbit[k]]; k -= 1 << highbit[k]; if (k == 0) return u; if (dep[u] - dep[top[u]] == k) return top[u]; if (dep[u] - dep[top[u]] > k) return dwn[top[u]][dep[u] - dep[top[u]] - k - 1]; return up[top[u]][k - (dep[u] - dep[top[u]]) - 1]; }
int main() { scanf("%d", &n); for (int i = 2, u, v; i <= n; i++) { scanf("%d%d", &u, &v); edge[u].push_back(v); edge[v].push_back(u); } init(); scanf("%d", &m); int u, k, lastans = 0; while (m--) { scanf("%d%d", &u, &k); u ^= lastans; k ^= lastans; printf("%d\n", lastans = qkth(u, k)); } return 0; }
|