Code:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=40000+3;
typedef long long ll;
ll dis[maxn];
int son[maxn],siz[maxn],rank1[maxn],p[maxn],top[maxn];
struct Edge {
int from, to, dis;
Edge(int from, int to, int dis) :from(from), to(to), dis(dis){}
};
struct LCA{
vector<Edge>edges;
vector<int>G[maxn];
void init(int n){
for(int i=1;i<=n;++i)G[i].clear();
edges.clear();
memset(son,-1,sizeof(son));memset(dis,0,sizeof(dis));
memset(rank1,0,sizeof(rank1));memset(p,0,sizeof(p));
memset(siz,0,sizeof(siz));memset(top,0,sizeof(top));
}
void add_edge(int from,int to,int dis){
edges.push_back(Edge(from,to,dis));
int m=edges.size()-1;
G[from].push_back(m);
}
void dfs1(int u,int fa,int cur,ll dep)
{
siz[u]=1,p[u]=fa,rank1[u]=cur;
dis[u]=dep;
for(int i=0;i<G[u].size();++i)
{
int m=G[u][i];
Edge E=edges[m];
if(E.to!=fa)
{
dfs1(E.to,u,cur+1,dep+E.dis);
siz[u]+=siz[E.to];
if(son[u]==-1||siz[son[u]]<siz[E.to])son[u]=E.to;
}
}
}
void dfs2(int u,int tp){
top[u]=tp;
if(son[u]!=-1)dfs2(son[u],tp);
for(int i=0;i<G[u].size();++i)
{
int m=G[u][i];
Edge E=edges[m];
if(E.to!=p[u]&&E.to!=son[u])dfs2(E.to,E.to);
}
}
int query(int x,int y)
{
while(top[x]!=top[y])
{
if(rank1[top[x]]<=rank1[top[y]])y=p[top[y]];
else x=p[top[x]];
}
if(rank1[x]<=rank1[y])return x;
return y;
}
ll ans(int x,int y)
{
ll m1=dis[x],m2=dis[y];
int lca=query(x,y);
return m1+m2-2*dis[lca];
}
};
int main(){
int T;scanf("%d",&T);
while(T--)
{
int n,m;scanf("%d%d",&n,&m);
LCA _lca;
_lca.init(n);
for(int i=1;i<n;++i)
{
int from,to,dis;
scanf("%d%d%d",&from,&to,&dis);
_lca.add_edge(from,to,dis);
_lca.add_edge(to,from,dis);
}
_lca.dfs1(1,-1,1,0);
_lca.dfs2(1,1);
for(int i=1;i<=m;++i)
{
int a,b;scanf("%d%d",&a,&b);
printf("%lld\n",_lca.ans(a,b));
}
}
return 0;
}

  

Hdu 2586 树链剖分求LCA的更多相关文章

  1. 树链剖分求LCA

    树链剖分中各种数组的作用: siz[]数组,用来保存以x为根的子树节点个数 top[]数组,用来保存当前节点的所在链的顶端节点 son[]数组,用来保存重儿子 dep[]数组,用来保存当前节点的深度 ...

  2. cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!

    2450. 距离 ★★   输入文件:distance.in   输出文件:distance.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 在一个村子里有N个房子,一 ...

  3. cogs 2109. [NOIP 2015] 运输计划 提高组Day2T3 树链剖分求LCA 二分答案 差分

    2109. [NOIP 2015] 运输计划 ★★★☆   输入文件:transport.in   输出文件:transport.out   简单对比时间限制:3 s   内存限制:256 MB [题 ...

  4. HDU2586 How far away ? (树链剖分求LCA)

    用树链剖分求LCA的模板: 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 const ...

  5. 【树链剖分】洛谷P3379 树链剖分求LCA

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  6. 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  7. 【模板】树链剖分求LCA

    洛谷3379 #include<cstdio> #include<algorithm> using namespace std; ,inf=1e9; int n,m,x,y,r ...

  8. hdu 5274 树链剖分

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  9. hdu 5893 (树链剖分+合并)

    List wants to travel Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

随机推荐

  1. APUE学习笔记5——信号、信号集和进程信号屏蔽字

    1 信号传递过程 当引发信号的事件发生时(如软硬件异常.软件定时.终端产生信号或调用kill函数等等),会产生信号,内核会发送给目标进程. 在信号产生到信号传递给目标进程之间的时间间隔内,称该信号为未 ...

  2. dfs___刷题记录

    poj 1564 给出一个s,n个数,输出所有的能够得到s的方案 #include<cstdio> #include<cstring> #include<iostream ...

  3. luogu P4245 【模板】任意模数NTT MTT

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...

  4. Pyhton二级操作题练习

    # 1.编写一个python程序,输入两个数,比较它们的大小并输出其中较大者. num1 = input('请输入数字X:') num2 = input('请输入数字Y:') if num1.isde ...

  5. 多进程Socket_Client

    from socket import * #导入套接字模块的所有命令import struct #导入struck模块,用于封装数据流长度# from functools import partial ...

  6. Vue.mixin Vue.extend(Vue.component)的原理与区别

    1.本文将讲述 方法 Vue.extend Vue.mixin 与 new Vue({mixins:[], extend:{}})的区别与原理 先回顾一下 Vue.mixin 官网如下描述: Vue. ...

  7. BZOJ 3203 [SDOI2013]保护出题人 (凸包+三分)

    洛谷传送门 题目大意:太长略 每新加入一个僵尸,容易得到方程$ans[i]=max{\frac{sum_{i}-sum_{j-1}}{s_{i}+d(i-j)}}$ 即从头开始每一段僵尸都需要在规定距 ...

  8. 使用PoolingHttpClientConnectionManager解决httpclient的多线程请求问题

    直接上代码 1.主程序 public class TestMain { public static void main(String[] args) throws NSQException, Time ...

  9. C/C++ Swap without using extra variable

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255379 对于可以线性运算的变量, ...

  10. HDU 4307 Contest 1

    http://www.cnblogs.com/staginner/archive/2012/08/13/2636826.html 自己看过后两周吧,重新写了一遍.很受启发的.对于0.1,可以使用最小割 ...