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

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<climits>
#include<bitset>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define scd(a) scanf("%d",&a)
#define scf(a) scanf("%lf",&a)
#define scl(a) scanf("%lld",&a)
#define sci(a) scanf("%I64d",&a)
#define scs(a) scanf("%s",a)
#define left asfdasdasdfasdfsdfasfsdfasfdas1
#define tan asfdasdasdfasdfasfdfasfsdfasfdas
typedef long long ll;
typedef unsigned int un;
const int desll[][]={{,},{,-},{,},{-,}};
const ll mod=1e9+;
const int maxn=4e4+;
const int maxm=3e8+;
const double eps=1e-;
int m,n; struct node
{
int b,nex,c;
}no[maxn*];
int head[maxn],sz=;
int add(int a,int b,int c){
no[sz].b=b;
no[sz].c=c;
no[sz].nex=head[a];
head[a]=sz++;
}
int father[maxn],tan[maxn*][],dep[maxn];
int ar[maxn*],le,in[maxn];
ll dis[maxn];
void dfs1(int u,int pre){
for(int i=head[u];i!=-;i=no[i].nex){
int v=no[i].b;
if(v==pre)continue;
dis[v]=dis[u]+no[i].c;
father[v]=u;
dep[v]=dep[u]+;
ar[le++]=u;
dfs1(v,u);
}
in[u]=le;
ar[le++]=u;
}
void init()
{
memset(tan,,sizeof(tan));
for(int i=;i<le;i++){
tan[i][]=ar[i];
}
for(int j=;j<;j++){
for(int i=le-;i>=;i--){
if(i+(<<j)->=le)continue;
int x=tan[i][j-],y=tan[i+(<<(j-))][j-];
if(dep[x]<dep[y])tan[i][j]=x;
else tan[i][j]=y;
//cout<<i<<" "<<j<<" "<<tan[i][j]<<endl;
}
}
}
int sameFather(int a,int b)
{
a=in[a];
b=in[b];
if(a>b)swap(a,b);
int res=floor(log(b-a+)/log());
int x=tan[a][res],y=tan[b-(<<res)+][res];
if(dep[x]<dep[y])return x;
else return y;
} int main()
{
int t;scd(t);
while(t--){
scd(n);scd(m);
memset(head,-,sizeof(head));sz=;
for(int i=;i<n;i++){
int a,b,c;
scd(a);scd(b);scd(c);
add(a,b,c);
add(b,a,c);
}
memset(dis,,sizeof(dis));
memset(dep,,sizeof(dep));
dep[]=;
dfs1(,);
father[]=;
init();
for(int i=;i<m;i++){
int a,b;
scd(a);scd(b);
int x=sameFather(a,b);
printf("%d\n",dis[a]+dis[b]-*dis[x]);
}
}
return ;
}

lca最短公共祖先模板(hdu2586)的更多相关文章

  1. LCA(最近公共祖先)模板

    Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...

  2. LCA最近公共祖先模板(求树上任意两个节点的最短距离 || 求两个点的路进(有且只有唯一的一条))

    原理可以参考大神 LCA_Tarjan (离线) TarjanTarjan 算法求 LCA 的时间复杂度为 O(n+q) ,是一种离线算法,要用到并查集.(注:这里的复杂度其实应该不是 O(n+q)  ...

  3. LCA最近公共祖先模板代码

    vector模拟邻接表: #include<iostream> #include<cstdio> #include<cstring> #include<cma ...

  4. LCA(最近公共祖先)之倍增算法

    概述 对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. 如图,3和5的最近公共祖先是1,5和2的最近公共祖先是4 在本篇中我们先介 ...

  5. CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )

    CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...

  6. lca 最近公共祖先

    http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...

  7. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  8. LCA近期公共祖先

    LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...

  9. LCA 近期公共祖先 小结

    LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...

随机推荐

  1. Flink 任务打包、提交

    一.Flink版本 flink-1.6.1-bin-hadoop26-scala_2.11 二.Flink任务打包 笔者将写好的flink计算任务代码发到服务器(ubuntu16.04),在服务器端进 ...

  2. 用JQuery的$.getJSON发起跨域Ajax请求

    jQuery中常用getJSON来调用并获取远程的JSON字符串,将其转换为JSON对象,如果成功,则执行回调函数.原型如下: jQuery.getJSON( url, [data], [callba ...

  3. oracle数据库导入导出方法

    Oracle Database 10g以后引入了最新的数据泵(Data Dump)技术,使DBA或开发人员可以将数据库元数据(对象定义)和数据快速移动到另一个oracle数据库中. 数据泵导出导入(E ...

  4. bzoj5091 [Lydsy1711月赛]摘苹果 概率题

    [Lydsy1711月赛]摘苹果 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 174  Solved: 135[Submit][Status][Dis ...

  5. HDFS集中化缓存管理

    概述 HDFS中的集中化缓存管理是一个明确的缓存机制,它允许用户指定要缓存的HDFS路径.NameNode会和保存着所需快数据的所有DataNode通信,并指导他们把块数据缓存在off-heap缓存中 ...

  6. ansible 批量修改root密码

    [root@sz_fy_virt_encrypt_33_239 fetch]# cat /opt/passwd.yml - hosts: web vars: path: /home/opsadmin ...

  7. jspersonft有关Table数据绑定(一)

    一:前言 在公司来就学着做报表,觉得这个报表学着还是很有意义的.jspersonft我在网上搜了一些有关的资料但是不是很多,现在就是学一点就记载一点.好记性不如烂笔头嘛! 二:在jspersonft定 ...

  8. 如何让 linux unzip 命令 不输出结果

    unzip xx.zip > /dev/null 2>&1 unzip xx.zip > /dev/null前半部分是将标准输出重定向到空设备, 后面的2>&1 ...

  9. request.getParameterValues与request.getParameter的区别

    一. 简单的对比 request.getParameter用的比较多,相对熟悉 request.getParameterValues(String   name)是获得如checkbox类(名字相同, ...

  10. 【uva11732-"strcmp()" Anyone?】Trie

    http://acm.hust.edu.cn/vjudge/problem/28438 题意:给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. 题解: 插入一个‘#’作为字符串的结束 ...