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
| struct LCA { int root; vector<vector<int>> adj; vector<int> dep, father, sz, heavy, top;
LcA () {} LCA (int root, vector<vector<int>> adj) { init(root, adj); }
void init(int root, vector<vector<int>> adj) { this->root = root; this->adj = adj; int n = adj.size(); dep.resize(n), father.resize(n), sz.resize(n), heavy.resize(n), top.resize(n); dfs1(root, root), dfs2(root, root); }
void dfs1(int cur, int fa) { father[cur] = fa; sz[cur] = 1; dep[cur] = dep[fa] + 1; for (auto son : adj[cur]) { if (son == fa) continue; dfs1(son, cur); sz[cur] += sz[son]; if (sz[heavy[cur]] < sz[son]) heavy[cur] = son; } }
void dfs2(int cur, int head) { top[cur] = head; if (!heavy[cur]) return; dfs2(heavy[cur], head); for (auto son : adj[cur]) { if (son == father[cur] || son == heavy[cur]) continue; dfs2(son, son); } }
int query(int x, int y) { while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); x = father[top[x]]; } return dep[x] < dep[y] ? x : y; } };
|