鬼知道我为什么要来写这篇博客
诺, 下边的是代码,
具体讲解的话
这里(侵删)
墙裂推荐!
讲的太好了
我才不会说是因为自己懒才推荐别人的,qwq
虽然我的也是看的他的
#include <iostream> #include <cstdio> #include <algorithm> #define N 5000001 using namespace std; struct exo { int t, next; }e[N]; int dt[N], fa[N][30], lg[N], head[N], cnt, n, m ,s; void add (int x, int y) { e[++cnt].next = head[x]; e[cnt].t = y; head[x] = cnt; } void dfs (int f, int fath) { dt[f] = dt[fath] + 1; fa[f][0] = fath; for (int i = 1; (1 << i) <= dt[f]; i++) fa[f][i] = fa[fa[f][i - 1]][i - 1]; for (int i = head[f];i;i = e[i].next) if (e[i].t != fath) dfs (e[i].t, f); } int lca (int x, int y) { if (dt[x] < dt[y]) swap (x, y); while (dt[x] > dt[y]) x = fa[x][lg[dt[x] - dt[y]] - 1]; if (x == y) return x; for (int k = lg[dt[x]] - 1; k >= 0;k--) if (fa[x][k] != fa[y][k]) x = fa[x][k], y = fa[y][k]; return fa[x][0]; } int main () { scanf ("%d%d%d", &n, &m, &s); for (int i = 1; i < n; i++) { int x, y; scanf ("%d%d", &x, &y); add (x, y); add (y, x); } dfs (s, 0); for (int i = 1; i <= n; i++) lg[i] = lg[i - 1] + (1 << lg[i- 1] == i) ; for (int i = 1; i <= m; i++) { int x, y; scanf ("%d%d", &x, &y); printf ("%d ", lca (x, y)); } return 0; }