杭电 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.

InputFirst 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.OutputFor 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
典型的带权LCA
#include<bits/stdc++.h>
using namespace std;
const int N=1E5+7;
typedef long long ll;
struct stu{
    int a,b;
};
vector<stu>ve[N];
ll bits[30];
int step[N];
int depth[N];
int fa[N][30];

void inint(){
    bits[0]=1;
    for(int i=1;i<30;i++) bits[i]=bits[i-1]<<1;
}

void dfs(int x,int y){
    depth[x]=depth[y]+1;    
    for(int i=0;i<ve[x].size();i++){
        if(ve[x][i].a==y){
            step[x]=step[y]+ve[x][i].b;
        }
    }    
    fa[x][0]=y;
    for(int i=1;i<30;i++) fa[x][i]=fa[fa[x][i-1]][i-1];
    for(int i=0;i<ve[x].size();i++){
        int dx=ve[x][i].a;
        if(dx!=y){
            dfs(dx,x);
        }
    } 
}
int lca(int x,int y){
    if(depth[x]<depth[y]) swap(x,y);
    int dif=depth[x]-depth[y];
    for(int i=29;i>=0;i--){
        if(dif>=bits[i]){
            x=fa[x][i];
            dif=dif-bits[i];
        }
    }
    if(x==y) return x;
    for(int i=29;i>=0;i--){
        if(depth[x]>=bits[i]&&fa[x][i]!=fa[y][i]){
            x=fa[x][i];
            y=fa[y][i];
        }
    }
    return fa[x][0];
} 
int main(){
    int t;
    inint();    
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        int x,y,z;
        for(int i=1;i<=n;i++){
            ve[i].clear();
        }
        memset(depth,0,sizeof(depth));
        memset(fa,0,sizeof(fa));
        memset(step,0,sizeof(step));
        for(int i=1;i<=n-1;i++){
            scanf("%d%d%d",&x,&y,&z);
            ve[x].push_back({y,z});
            ve[y].push_back({x,z});
        }
        dfs(1,0);
        while(m--){
            int x1,y1;
            scanf("%d%d",&x1,&y1);
            int point=lca(x1,y1);
            printf("%d
",step[x1]+step[y1]-2*step[point]);
        }
    } 
    return 0;
}


原文地址:https://www.cnblogs.com/Accepting/p/11323258.html