HDU 2586 How far away ?(LCA裸题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586

#include<bits/stdc++.h>
#define lson rt << 1, l, m
#define rson rt << 1 | 1, m + 1, r
using namespace std;
typedef long long ll;
static const ll inf = (1 << 32);
static const int INF = 0x3f3f3f3f;
static const int MAX_N = 4e4 + 5;
static const int N = 5005;
struct Edge{
    int to, dis;
    Edge(int _to, int _dis){to = _to; dis = _dis;}
};
int n;
vector<Edge>edge[MAX_N];
int fa[MAX_N], dep[MAX_N], dis[MAX_N];
int par[MAX_N][30];
void dfs(int u, int pre, int depth){
    fa[u] = pre;
    dep[u] = depth;
    int le = edge[u].size();
    for(int i = 0; i < le; ++i){
        int v = edge[u][i].to;
        if(v == pre) continue;
        dis[v] = dis[u] + edge[u][i].dis;
        dfs(v, u, depth + 1);
    }
}
void init_LCA(){
    for(int j = 0; (1 << j) <= n; ++j){
        for(int i = 1; i <= n; ++i){
            par[i][j] = -1;
        }
    }
    for(int i = 1; i <= n; ++i) par[i][0] = fa[i];
    for(int j = 1; (1 << j) <= n; ++j){
        for(int i = 1; i <= n; ++i){
            if(par[i][j - 1] != -1) par[i][j] = par[par[i][j - 1]][j - 1];
        }
    }
}
int LCA(int x, int y){
    if(dep[x] < dep[y]) swap(x, y);
    int lg;
    for(lg = 0; (1 << lg) <= dep[x]; ++lg);
    --lg;
    for(int i = lg; i >= 0; --i){
        if(dep[x] - (1 << i) >= dep[y]){
            x = par[x][i];
        }
    }
    if(x == y) return x;
    for(int i = lg; i >= 0; --i){
        if(par[x][i] != -1 && par[x][i] != par[y][i]){
            x = par[x][i], y = par[y][i];
        }
    }
    return fa[x];
}
void addEdge(int u, int v, int w){
    edge[u].push_back(Edge(v, w));
}
int main(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int t, m;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; ++i) edge[i].clear();
        for(int i = 1; i < n; ++i){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            addEdge(u, v, w);
            addEdge(v, u, w);
        }
        dis[1] = 0;
        dfs(1, -1, 0);
        init_LCA();
        while(m--){
            int u, v;
            scanf("%d%d", &u, &v);
            printf("%d\n", dis[u] + dis[v] - 2 * dis[LCA(u, v)]);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/xorxor/p/11230840.html

时间: 2024-10-07 17:46:45

HDU 2586 How far away ?(LCA裸题)的相关文章

SZOJ 167 Lca裸题

一道.......一道我改了一周的裸题 无根树建双向边 无根树建双向边 无根树建双向边 重要的事情说三遍(微笑) 还有要开longlong 还有双向边不是双倍边(微笑) 我真是,能把自己气吐血10次就不把自己气吐血9次 [问题描述] 已知一棵nn个点的树,点从1开始标号,树上每条边都有一个正整数边权. 有qq个询问,每个询问由type,u,vtype,u,v三个正整数构成. 当type=1type=1时,询问uu到vv路径上所有边权的二进制异或和. 当type=2type=2时,询问uu到vv路

HDU 2602 Bone Collector(01背包裸题)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 60469    Accepted Submission(s): 25209 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bo

HDU 2546 饭卡(01背包裸题)

饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28562    Accepted Submission(s): 9876 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无

POJ 1330 LCA裸题~

POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an anc

HDU 2586 How far away LCA的离线算法 Tarjan

链接: How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11204    Accepted Submission(s): 4079 Problem Description There are n houses in the village and some bidirectional roads connec

HDU 3480 Division(斜率DP裸题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 题目大意:将n个数字分成m段,每段价值为(该段最大值-该段最小值)^2,求最小的总价值. 解题思路:很单纯的斜率优化DP,得出状态转移方程:dp[i][j]=min{dp[k][j-1]+(a[i]-a[k+1])^2}(j-1<=k<i),然后斜率优化降到O(n^2)就好了. 注意:数据类型建议用int,不要用long long,后者乘法计算时间是前者的四倍,否则C++可能会超时. 代码:

HDU 2665 Kth number 主席树裸题

题目链接 主席树详解 每次插入logn个点 这样就不需要重新建树了. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <fstream> #include <string> #include <time.h> #include <vector> #include <map> #include &

HDU - 2586 How far away ?(LCA)

题目大意:给出一张连通图,问两个点之间的距离 解题思路:LCA裸题 #include <cstdio> #include <cstring> #define N 40010 #define M 80010 struct Edge{ int to, next, dis; }E[M]; struct Question { int x, y; }Q[N]; int n, m ,tot; int head[N], dist[N], f[N], LCA[N]; bool vis[N]; vo

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description 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&quo