树上三点间最短距离(dfs+ST lca)

https://zoj.pintia.cn/problem-sets/91827364500/problems/91827367819

题意:给出n个城市,n-1条边,无环图,m次询问使u、v、w间联通的最短距离。

解法:lca求两两点间最短距离除以二。

//#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
typedef long long ll ;
#define mod 1000000007
#define gcd __gcd
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//ll lcm(ll a , ll b){return a*b/gcd(a,b);}
//ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define int ll
#define cin() scanf("%lld" , &x);
using namespace std;
const int maxn = 5e5+9;
int head[maxn] , tol , oula[maxn<<1] , pos[maxn] , len , de[maxn<<1] , dis[maxn] ;
int dp[maxn<<1][30];
int n , flag;
struct node{
    int to , next , w ;
}g[maxn<<1];

void init(){
    ME(head , 0);
    tol = 0;
    len = 0;
}

void add(int u , int v , int w){
    g[++tol] = {v , head[u] , w};
    head[u] = tol;
}

void dfs(int u , int pre , int d , int di){
    oula[++len] = u ;
    de[len] = d ;
    pos[u] = len ;
    dis[u] = di ;
    for(int i = head[u] ; i ; i = g[i].next){
        int v = g[i].to;
        if(v == pre) continue;
        dfs(v , u , d+1 , di+g[i].w);
        oula[++len] = u;
        de[len] = d;
    }
}

int Min(int x , int y){
    return de[x] > de[y] ? y : x;
}

void ST(){
    rep(i , 1 , len){
        dp[i][0] = i ;
    }
    for(int j = 1 ; (1<<j) <= len ; j++){
        for(int i = 1 ; i+(1<<j)-1 <= len ; i++){
            dp[i][j] = Min(dp[i][j-1] , dp[i+(1<<j-1)][j-1]);
        }
    }
}

int lca(int l , int r){
    l = pos[l] , r = pos[r];
    if(l > r) swap(l , r);
    int k = log2(r-l+1);
    return Min(dp[l][k] , dp[r-(1<<k)+1][k]);
}

int dis_lca(int x , int y){
    return dis[x] + dis[y] - 2*dis[oula[lca(x , y)]];
}

void solve(){
    if(flag) cout << endl;
    flag = 1 ;
    init();
    rep(i , 1 , n-1){
        int u , v , w;
        cin >> u >> v >> w;
        add(u , v , w);
        add(v , u , w);
    }
    dfs(0 , 0 , 1 , 0);
    ST();
    int m ;
    cin >> m ;
    rep(i , 1 , m){
        int u , v , w ;
        cin >> u >> v >> w ;
        cout << (dis_lca(u,v)+dis_lca(u,w)+dis_lca(v,w))/2<<endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //int t ;
    //cin >> t ;
    //while(t--){
    while(cin >> n)
        solve();
    //}
}

 

posted @ 2020-02-20 17:58  无名菜鸟1  阅读(547)  评论(0编辑  收藏  举报