2014
02-10

# How far away ？

There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can’t visit a place twice) between every two houses. Yout task is to answer all these curious people.

First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.

First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.

2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

10
25
100
100

LCA模板题

题意：给一个无根树，有q个询问，每个询问两个点，问两点的距离。求出  lca = LCA(X,Y) , 然后  dir[x] + dir[y] – 2 * dir[lca]

dir[u]表示点u到树根的距离

Tarjan算法跑得更快些，C++ 15ms，  G++ 50ms  左右， RMQ大概60ms

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 40010;
const int M = 25;

int _pow[M];
int dp[2*N][M];  //这个数组记得开到2*N，因为遍历后序列长度为2*n-1
bool vis[N];
struct edge
{
int u,v,w,next;
}e[2*N];

inline void add(int u ,int v ,int w ,int &k)
{
e[k].u = u; e[k].v = v; e[k].w = w;
u = u^v; v = u^v; u = u^v;
e[k].u = u; e[k].v = v; e[k].w = w;
}

void dfs(int u ,int dep)
{
vis[u] = true; ver[++tot] = u; first[u] = tot; R[tot] = dep;
if( !vis[e[k].v] )
{
int v = e[k].v , w = e[k].w;
dir[v] = dir[u] + w;
dfs(v,dep+1);
ver[++tot] = u; R[tot] = dep;
}
}

void ST(int len)
{
int K = (int)(log((double)len) / log(2.0));
for(int i=1; i<=len; i++) dp[i][0] = i;
for(int j=1; j<=K; j++)
for(int i=1; i+_pow[j]-1<=len; i++)
{
int a = dp[i][j-1] , b = dp[i+_pow[j-1]][j-1];
if(R[a] < R[b]) dp[i][j] = a;
else            dp[i][j] = b;
}
}

int RMQ(int x ,int y)
{
int K = (int)(log((double)(y-x+1)) / log(2.0));
int a = dp[x][K] , b = dp[y-_pow[K]+1][K];
if(R[a] < R[b]) return a;
else            return b;
}

int LCA(int u ,int v)
{
int x = first[u] , y = first[v];
if(x > y) swap(x,y);
int res = RMQ(x,y);
return ver[res];
}

int main()
{
for(int i=0; i<M; i++) _pow[i] = (1<<i);
int cas;
scanf("%d",&cas);
while(cas--)
{
int n,q,num = 0;
scanf("%d%d",&n,&q);
memset(vis,false,sizeof(vis));
for(int i=1; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
}
tot = 0; dir[1] = 0;
dfs(1,1);
/*
printf("节点 "); for(int i=1; i<=2*n-1; i++) printf("%d ",ver[i]); cout << endl;
printf("深度 "); for(int i=1; i<=2*n-1; i++) printf("%d ",R[i]);   cout << endl;
printf("首位 "); for(int i=0; i<n; i++) printf("%d ",first[i]);    cout << endl;
printf("距离 "); for(int i=0; i<n; i++) printf("%d ",dir[i]);      cout << endl;
*/
ST(2*n-1);
while(q--)
{
int u,v;
scanf("%d%d",&u,&v);
int lca = LCA(u,v);
//            printf("lca = %d\n",lca);
printf("%d\n",dir[u] + dir[v] - 2*dir[lca]);
}
}
return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 40010;
const int M = 410;

struct edge{
int u,v,w,next;
}e[2*N];
int u,v,lca,next;
}ea[M];
int dir[N],fa[N],ance[N];
bool vis[N];

inline void add_edge(int u,int v,int w,int &k)
{
e[k].u = u; e[k].v = v; e[k].w = w;
u = u^v; v = u^v; u = u^v;
e[k].u = u; e[k].v = v; e[k].w = w;
}

{
ea[k].u = u; ea[k].v = v; ea[k].lca = -1;
u = u^v; v = u^v; u = u^v;
ea[k].u = u; ea[k].v = v; ea[k].lca = -1;
}

int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
void Union(int u ,int v)
{
fa[v] = fa[u];  //可写为  fa[Find(v)] = fa[u];
}

void Tarjan(int u)
{
vis[u] = true;
ance[u] = fa[u] = u; //课写为 ance[Find(u)] = fa[u] = u;
if( !vis[e[k].v] )
{
int v = e[k].v , w = e[k].w;
dir[v] = dir[u] + w;
Tarjan(v);
Union(u,v);
//ance[Find(u)] = u;  //可写为ance[u] = u;  //甚至不要这个语句都行
}
if( vis[ea[k].v] )
{
int v = ea[k].v;
ea[k].lca = ea[k^1].lca = ance[Find(v)];
}
}

int main()
{
int cas,n,q,tot;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&q);
tot = 0;
for(int i=1; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
}
tot = 0;
for(int i=0; i<q; i++)
{
int u,v;
scanf("%d%d",&u,&v);
}
memset(vis,0,sizeof(vis));
dir[1] = 0;
Tarjan(1);
for(int i=0; i<q; i++)
{
int s = i * 2 , u = ea[s].u , v = ea[s].v , lca = ea[s].lca;
printf("%d\n",dir[u] + dir[v] - 2*dir[lca]);
}
}
return 0;
}