## zyh

move on~(799050408@qq.com)

# hdu 2586 How far away ？ lca 在线和离线算法

给出在线和离线两种方法

tarjan离线算法:

#include <cstdio>
#include <cstring>
#include <algorithm>

#pragma warning (disable: 4996)

using namespace std;

const int maxn = 40005;
const int maxm = 205;

struct edge
{
int u, v, w, next;
}e[maxn];

struct quiry
{
int u, v, num, next;
}q[maxm << 1];

int n, m;
int p[maxn];
int dis[maxn];
bool in[maxn];
bool vis[maxn];
int root;
int lca[maxm];

int Find(int x)
{
return p[x] == x ? p[x] : p[x] = Find(p[x]);
}

void Union(int x, int y)
{
int dx = Find(x);
int dy = Find(y);
if (dx != dy)
p[dx] = dy;
}

void init()
{
cnt = 0;
cnt1 = 0;
memset(dis, 0, sizeof(dis));
memset(in, false, sizeof(in));
memset(vis, false, sizeof(vis));
for (int i = 1; i <= n; i++)
p[i] = i;
}

void addedge(int u, int v, int w)
{
e[cnt].u = u, e[cnt].v = v, e[cnt].w = w, e[cnt].next = head[u], head[u] = cnt++;
}

void addquery(int u, int v, int num)
{
q[cnt1].u = u, q[cnt1].v = v, q[cnt1].num = num, q[cnt1].next = head1[u], head1[u] = cnt1++;
}

void tarjan(int u)
{
vis[u] = true;
int v;
for (int i = head1[u]; i != -1; i = q[i].next)
{
v = q[i].v;
if (vis[v])
{
lca[q[i].num] = Find(v);
}
}
for (int i = head[u]; i != -1; i = e[i].next)
{
v = e[i].v;
if (!vis[v])
{
dis[v] = dis[u] + e[i].w;
tarjan(v);
p[v] = u;
}
}
}

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
init();
int u, v, w, c;
for (int i = 0; i < n - 1; i++)
{
scanf("%d%d%d", &u, &v, &w);
in[v] = true;
}
for (int i = 0; i < m; i++)
{
scanf("%d%d", &u, &v);
}
for (int i = 1; i <= n; i++)
{
if (!in[i])
{
root = i;
break;
}
}
tarjan(root);
for (int i = 0; i < m * 2; i += 2)
{
u = q[i].u, v = q[i].v, c = q[i].num;
printf("%d\n", dis[u] + dis[v] - 2 * dis[lca[c]]);
}
}
return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>

#pragma warning (disable: 4996)

using namespace std;

const int maxn = 40005;
const int maxm = 205;

struct edge
{
int u, v, w, next;
}e[maxn];
int n, m;
bool in[maxn];
int root;
int vs[maxn << 1];
int dep[maxn << 1];
int id[maxn];
int dp[maxn << 1][20]; //注意这里要乘2
int dis[maxn];

void init()
{
cnt = 0;
memset(dis, 0, sizeof(dis));
memset(in, false, sizeof(in));
}

void addedge(int u, int v, int w)
{
e[cnt].u = u, e[cnt].v = v, e[cnt].w = w, e[cnt].next = head[u], head[u] = cnt++;
}

void dfs(int u, int fa, int d, int &k)
{
id[u] = k;
vs[k] = u;
dep[k++] = d;
int v;
for (int i = head[u]; i != -1; i = e[i].next)
{
v = e[i].v;
if (v != fa)
{
dis[v] = dis[u] + e[i].w;
dfs(v, u, d + 1, k);
vs[k] = u;
dep[k++] = d;
}
}
}

int Min(int x, int y)
{
return dep[x] <= dep[y] ? x : y;
}

int rmq(int l, int r)
{
int k = 0;
while ((1 << (k + 1)) <= r - l + 1) k++;
return Min(dp[l][k], dp[r - (1 << k) + 1][k]);
}

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 << j) - 1 < n; i++)
dp[i][j] = Min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
}

void build()
{
for (int i = 1; i <= n; i++)
{
if (!in[i])
{
root = i;
break;
}
}
int k = 0;
dfs(root, -1, 0, k);
rmq_init(n * 2);
}

int lca(int u, int v)
{
int l, r;
l = min(id[u], id[v]);
r = max(id[u], id[v]);
int res = rmq(l, r);
return vs[res];
}

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
init();
scanf("%d%d", &n, &m);
for (int i = 0; i < n - 1; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
in[v] = true;
}
build();
for (int i = 0; i < m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
int a = lca(u, v);
printf("%d\n", dis[u] + dis[v] - 2 * dis[a]);
}
}
return 0;
}

#### hdu2586How far away ？【LCA tarjan求最短距离】

2015-11-25 21:39:27

#### HDU 2586 How far away ？

2015-01-24 00:38:35

#### HDU2586How far away ？(LCA－在线ST+ tarjan离线)

2017-04-12 12:04:39

#### 【HDU】2586 How far away ？ LCA求树上点对最近距离

2014-07-02 22:13:37

#### 【杭电oj2586】How far away ？

2016-09-18 20:53:34

#### hdu 2586 LCA模板题（离线和在线两种解法）

2015-03-01 10:51:36

#### HDU-2586-How far away ？（倍增求LCA模板）

2017-03-28 18:33:07

#### hdu 2586 How far away ？ LCA离线算法

2016-04-28 20:14:02

#### LCA三种算法学习（离线算法tarjan+在线算法转rmq+在线倍增）例题poj1330、1470;hdu4547、2874

2016-07-24 17:14:36