带权值的LCA

例题:http://poj.org/problem?id=1986

 POJ1986 Distance Queries

Language:
Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 18225   Accepted: 6265
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare" 

* Line 2+M: A single integer, K. 1 <= K <= 10,000 

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms. 

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart. 
 

带权值的LCA处理方法和原处理方法唯一一点区别就是加了一个数组step[x]用来记录到根节点的距离,最后step[x]+step[y]-2*step[point]其中point为x和y的最近公共祖先点

AC代码:

#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+7;
ll bits[30];
ll depth[N];
int fa[N][30];
struct stu {
    int a,b;
};
vector<stu>ve[N];
int n,m;
int mark[N];
ll step[N];
void inint(){
    bits[0]=1;
    for(int i=1;i<=29;i++)
        bits[i]=bits[i-1]<<1;
}

void dfs(int x,int y){
    depth[x]=depth[y]+1;
    fa[x][0]=y;
    for(int i=0;i<ve[x].size();i++){
        if(ve[x][i].a==y){
            step[x]=step[y]+ve[x][i].b;
        }
    }
    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 x1=ve[x][i].a;    
        if(x1!=y){
            dfs(x1,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-=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(){
    inint();
    scanf("%d%d",&n,&m);
    int x,y,z;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);
        getchar();getchar();
            ve[x].push_back({y,z});
            ve[y].push_back({x,z});
    }
    dfs(1,0);
    int d;
    scanf("%d",&d);
    while(d--){
        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/11322183.html