Common ancestor of template nearest

Source: Internet
Author: User

LCA Tarjan off-line algorithm


On-line algorithm for LCA transfer RMQ

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 200010; struct Edge{int u, V, W, next;} EDGES[MAXN*2], E[maxn];int e[maxn*2], h[maxn*2], i[maxn*2], L[MAXN], r[maxn];int dp[maxn*2][40];int cnt, clock, Dfn;int F Irst[maxn];int a[maxn<<2];int b[maxn];int add[maxn<<2];int degree[maxn];int vis[maxn];void AddEdge (int u, int V, int w) {edges[cnt].u = U;EDGES[CNT].V = V;EDGES[CNT].W = W;edges[cnt].next = First[u];first[u] = cnt++;edges[cnt].u = V;EDGES[CNT].V = U;EDGES[CNT].W = W;edges[cnt].next = First[v];first[v] = cnt++;} void Dfs (int u, int fa, int dep) {E[++clock] = u; H[clock] = dep;i[u] = clock; L[u] = ++DFN;B[DFN] = u;for (int i = first[u]; I! = 1; i = edges[i].next) {int v = edges[i].v;if (v = = FA) continue;if (Vis[v] ) Continue;vis[v] = True;dfs (V, U, dep+1); E[++clock] = u; H[clock] = dep;} R[u] = DFN;} void Rmq_init (int n) {for (int i = 1; I <= n; i++) dp[i][0] = i;for (int j = 1; (1<<j) <= N; J + +) for (int i = 1; i+ (1&LT;&LT;J)-1 <= N; i++) {if (H[dp[i][j-1]] < h[dp[i+ (1<< (j-1))][j-1]]) dp[i][j] = dp[i][j-1];elsedp[i][j] = dp[i+ (1<< (j-1)) ][J-1];}} int RMQ (int l, int r) {L = I[l], r = i[r];if (L > R) Swap (L, r); int len = r-l+1, k = 0;while ((1<<k) <= len) k++;k- -;if (H[dp[l][k]] < h[dp[r-(1<<k) +1][k]] return E[dp[l][k]];elsereturn e[dp[r-(1<<k) +1][k]];}


LCA Multiplication method

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 20010; const int INF = 999999999;int anc[maxn][16], Maxcost[maxn][16];int FA[MAXN], L[MAXN], COST[MAXN], Vis[maxn];int N, M;int F IRST[MAXN], cnt;struct edge{int u, V, next;} e[maxn*2];void Addedge (int u, int v) {e[cnt].v = V;e[cnt].next = First[u];first[u] = CNT++;E[CNT].V = U;e[cnt].next = First [V];first[v] = cnt++;} void Pre () {for (int i = 1; I <= n; i++) {anc[i][0] = Fa[i]; maxcost[i][0] = cost[i];for (int j = 1; (1&LT;&LT;J) < n; J + +) anc[i][j] = 1;} for (int j = 1; (1&LT;&LT;J) < n; J + +) for (int i = 1; I <= n; i++) if (anc[i][j-1]! =-1) {int a = Anc[i][j-1];anc[i][j] = anc[a][j-1];maxcost[i][j] = max (ma Xcost[i][j-1], maxcost[a][j-1]);}} int query (int p, int q) {int tmp, log, i;if (L[p] < l[q]) swap (p, q); for (log = 1; (1<<log) <= l[p]; log++); Log--;int ans =-inf;for (int i = log; I >= 0; i--) if (L[p]-(1<<i) >= l[q]) {ans = max (ans, maxcost[p][i ]);p = ANC[P] [i];}  if (p = = q) return ans;for (int i = log; I >= 0; i--) {if (anc[p][i]! = 1 && anc[p][i]! = Anc[q][i]) {ans = max (ans, Maxcost[p][i]); ans = max (ans, maxcost[q][i]);p = Anc[p][i];q = Anc[q][i];}} ans = max (ans, cost[p]); ans = max (ans, cost[q); return ans;} void Dfs (int u) {for (int i = first[u]; I! = 1; i = e2[i].next) {int v = e[i].v;if (Vis[v]) continue;vis[v] = 1;fa[v] = u; L[V] = L[u]+1;dfs (v);}}


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Common ancestor of template nearest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.