版本比较
LCA模板总结 & 测试
正文
增加 9 行删除 9 行
16 行未改动1718192021222324252 行未改动28293031323334353612 行未改动4950515253545556576 行未改动6465666768697071727 行未改动808182838485868788899091929394954 行未改动10010110210310410510610710810 行未改动11912012112212312412512612733 行未改动16116216316416516616716816928 行未改动16 行未改动```cppnamespace LCA {int dep[N], f[__lg(N) + 1][N];void dfs(int u, int fa) {dep[u] = dep[fa] + 1, f[0][u] = fa;for (int i = 1; i <= __lg(dep[u]); i++) {f[i][u] = f[i - 1][f[i - 1][u]];2 行未改动if (edge[j].to != fa) dfs(edge[j].to, u);}}void init(int rt) {dfs(rt, 0);}int lca(int x, int y) {if (dep[x] < dep[y]) swap(x, y);while (dep[x] > dep[y]) x = f[__lg(dep[x] - dep[y])][x];if (x == y) return x;12 行未改动class SphareTable {private:using func_t = function<T(const T&, const T&)>;int n; T st[__lg(N) + 1][N]; func_t func;void init(const T* a, bool get_init) {if (!get_init) func = [](const T& x, const T& y) -> T {return x > y ? x : y;};memset(st, 0, sizeof(st));memcpy(st[0] + 1, a + 1, sizeof(T) * n);6 行未改动public:SphareTable() {}SphareTable(const T* a, int _n) : n(_n) {init(a, 0);}SphareTable(const T* a, int _n, const func_t& f) : n(_n), func(f) {init(a, 1);}T query(int l, int r) const {int k = __lg(r - l + 1);return func(st[k][l], st[k][r - (1 << k) + 1]);}7 行未改动for (int j = head[u]; j != 0; j = edge[j].next) {if (edge[j].to != fa) dfs(edge[j].to, u);}}void init(int rt) {dn = 0, dfs(rt, 0);st = SphareTable<int>(dfa, n, [&](const int& u, const int& v) -> int {return dfn[u] < dfn[v] ? u : v;});}int lca(int x, int y) {if (x == y) return x;return st.query(min(dfn[u], dfn[v]) + 1, max(dfn[u], dfn[v]));}4 行未改动```cppnamespace LCA {int dep[N], sz[N], fa[N], son[N], top[N];void dfs1(int u) {sz[u] = 1;for (int j = head[u]; j != 0; j = edge[j].next) {int v = edge[j].to;10 行未改动if (v != fa[u] && v != son[u]) dfs2(v, v);}}void init(int rt) {dfs1(rt), dfs2(rt, rt);}int lca(int u, int v) {while (top[u] != top[v]) {if (dep[top[u]] < dep[top[v]]) swap(u, v);u = fa[top[u]];33 行未改动for (int f = fa[x]; f = fa[x], !is_root(x); rotate(x)) {if (!is_root(f)) rotate(get(f) == get(x) ? f : x);}}bool is_root(int x) {return ch[0][fa[x]] != x && ch[1][fa[x]] != x;}void access(int x) {28 行未改动16 行未改动1718192021222324252 行未改动28293031323334353612 行未改动4950515253545556576 行未改动6465666768697071727 行未改动808182838485868788899091929394954 行未改动10010110210310410510610710810 行未改动11912012112212312412512612733 行未改动16116216316416516616716816928 行未改动16 行未改动```cppnamespace LCA {int dep[N], f[__lg(N) + 1][N];void dfs(int u, int fa) {dep[u] = dep[fa] + 1, f[0][u] = fa;for (int i = 1; i <= __lg(dep[u]); i++) {f[i][u] = f[i - 1][f[i - 1][u]];2 行未改动if (edge[j].to != fa) dfs(edge[j].to, u);}}void init(int rt) {dfs(rt, 0);}int lca(int x, int y) {if (dep[x] < dep[y]) swap(x, y);while (dep[x] > dep[y]) x = f[__lg(dep[x] - dep[y])][x];if (x == y) return x;12 行未改动class SphareTable {private:using func_t = function<T(const T&, const T&)>;int n; T st[__lg(N) + 1][N]; func_t func;void init(const T* a, bool get_init) {if (!get_init) func = [](const T& x, const T& y) -> T {return x > y ? x : y;};memset(st, 0, sizeof(st));memcpy(st[0] + 1, a + 1, sizeof(T) * n);6 行未改动public:SphareTable() {}SphareTable(const T* a, int _n) : n(_n) {init(a, 0);}SphareTable(const T* a, int _n, const func_t& f) : n(_n), func(f) {init(a, 1);}T query(int l, int r) const {int k = __lg(r - l + 1);return func(st[k][l], st[k][r - (1 << k) + 1]);}7 行未改动for (int j = head[u]; j != 0; j = edge[j].next) {if (edge[j].to != fa) dfs(edge[j].to, u);}}void init(int rt) {dn = 0, dfs(rt, 0);st = SphareTable<int>(dfa, n, [&](const int& u, const int& v) -> int {return dfn[u] < dfn[v] ? u : v;});}int lca(int x, int y) {if (x == y) return x;return st.query(min(dfn[u], dfn[v]) + 1, max(dfn[u], dfn[v]));}4 行未改动```cppnamespace LCA {int dep[N], sz[N], fa[N], son[N], top[N];void dfs1(int u) {sz[u] = 1;for (int j = head[u]; j != 0; j = edge[j].next) {int v = edge[j].to;10 行未改动if (v != fa[u] && v != son[u]) dfs2(v, v);}}void init(int rt) {dfs1(rt), dfs2(rt, rt);}int lca(int u, int v) {while (top[u] != top[v]) {if (dep[top[u]] < dep[top[v]]) swap(u, v);u = fa[top[u]];33 行未改动for (int f = fa[x]; f = fa[x], !is_root(x); rotate(x)) {if (!is_root(f)) rotate(get(f) == get(x) ? f : x);}}bool is_root(int x) {return ch[0][fa[x]] != x && ch[1][fa[x]] != x;}void access(int x) {28 行未改动