链接:戳这里
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
Input
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.
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
Sample Output
10
25
100
100
题意:
n个点的树,边带权值,m组询问u,v。输出u到v之间的路径权值总和
思路:
LCA裸的题
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double lb;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int T;
int n,m;
ll f[40010][22];
int fa[40010][22],deep[40010];
int head[40010],tot;
struct edge{
int v,next,w;
}e[80010];
void Add(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
void DFS(int u,int Fa,int de){
deep[u]=de;
fa[u][0]=Fa;
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(v==Fa) continue;
f[v][0]=(ll)e[i].w;
DFS(v,u,de+1);
}
}
void build(){
for(int i=1;i<=20;i++){
for(int j=1;j<=n;j++){
if(fa[j][i-1]!=-1){
fa[j][i]=fa[fa[j][i-1]][i-1];
f[j][i]=f[j][i-1]+f[fa[j][i-1]][i-1];
}
}
}
}
int LCA(int u,int v){
if(deep[u]<deep[v]) swap(u,v);
int len=deep[u]-deep[v];
for(int i=0;i<=20;i++){
if(len&(1<<i)) u=fa[u][i];
}
for(int i=20;i>=0;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];
v=fa[v][i];
}
}
if(u==v) return u;
return fa[u][0];
}
int main(){
scanf("%d",&T);
while(T--){
mst(head,-1);tot=0;mst(deep,0);mst(fa,-1);mst(f,0);
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
Add(u,v,w);
Add(v,u,w);
}
DFS(1,0,0);
build();
while(m--){
int u,v;
scanf("%d%d",&u,&v);
int lca=LCA(u,v);
///printf("lca=%d\n",lca);
ll ans=0;
int lenu=deep[u]-deep[lca];
for(int i=20;i>=0;i--){
if(lenu&(1<<i)) {
ans+=f[u][i];
u=fa[u][i];
}
}
int lenv=deep[v]-deep[lca];
for(int i=20;i>=0;i--){
if(lenv&(1<<i)) {
ans+=f[v][i];
v=fa[v][i];
}
}
printf("%I64d\n",ans);
}
}
return 0;
}