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
| #include<iostream> #include<cstdio> using namespace std; const int N = 500005; int fa[N][21],dep[N]; int n,m,s; struct edge{ int v,next; }e[N * 2]; int front[N],cnt = 0; void AddEdge(int u,int v) { e[++cnt].v = v; e[cnt].next = front[u]; front[u] = cnt; } void dfs(int x,int fx) { dep[x] = dep[fx] + 1; fa[x][0] = fx; for (int i = 1;(1<<i) <= dep[x]; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; for(int i = front[x]; i; i = e[i].next) { int v = e[i].v; if(v != fx) dfs(v,x); } } int lca(int x, int y) { if(dep[x] > dep[y]) swap(x, y); for (int i = 20; i >= 0; i--) { if (dep[x] <= dep[y] - (1<<i)) y = fa[y][i]; } if (x == y) return x; for (int i = 20; i >= 0; i--) { if(fa[x][i] == fa[y][i]) continue; else x = fa[x][i],y = fa[y][i]; } return fa[x][0]; } int main() { int a,b; scanf("%d%d%d",&n,&m,&s); for (int i = 1; i < n; i++) { scanf("%d%d", &a, &b); AddEdge(a,b); AddEdge(b,a); } dfs(s, 0); for (int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); printf("%d\n", lca(a,b)); } return 0; }
|