题目链接: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. 从零开始实现Vue简单的Toast插件

    在前端项目中,有时会需要通知.提示一些信息给用户,尤其是在后台系统中,操作的正确与否,都需要给与用户一些信息. 1. 实例 在Vue组件的methods内,调用如下代码 `this``.$toast( ...

  2. 安徽师大附中%你赛day7 T2 乘积 解题报告

    乘积 题目背景 \(\mathrm{Smart}\) 最近在潜心研究数学, 他发现了一类很有趣的数字, 叫做无平方因子数. 也就是这一类数字不能够被任意一个质数的平方整除, 比如\(6\).\(7\) ...

  3. Jsp上传组件Smartupload介绍

    <form action="UploadServlet" enctype="multipart/form-data" method="post& ...

  4. Phaser的timer用法

    1. 延迟timer,相当于setTimeout game.time.events.add(Phaser.Timer.SECOND*5,this.delayOver,this); 2. 循环timer ...

  5. Document base D:\devTools\apache-tomcat-6.0.51\webapps\AppService does not exist or is not a readable directory

    tomcat通过eclipse发布项目到webapp后  手动删除在webapp目录下的文件,启动tomcat时,会报出异常找不到那个删除的项目. 解决方法是(1)重新发布项目到webapp (2)在 ...

  6. jw player学习笔记三---发布到其它网站

    一.通过官网发布向导 登陆 http://www.longtailvideo.com,注册一个账号,进入你的用户管理中心,如下图 然后按提示,一步步操作,就会得到js代码了. 二.自己服务器发布 &l ...

  7. 程序员的那些问题---转载自veryCD

    展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告   走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过 ...

  8. MySQL 8.0.11(zip)安装及配置

    (1)下载MySQL8.0.11: (2)解压zip文件: 我解压到了D:/MySQL/mysql-8.0.11-winx64 (3)配置环境变量:   右键此电脑->属性 高级系统设置 环境变 ...

  9. checkbox和后面文字无法居中对齐的解决方案

    制作前端页面时,表单的页面中都存在表单元素与提示文字无法对齐的问题.下面是针对这一问题的解决方案: 先上结果图看效果,吼吼~ 最上面两个是经过css处理后的效果,已经居中对齐了哦~,最后一个是没有处理 ...

  10. 【Foreign】旅行路线 [倍增]

    旅行路线 Time Limit: 20 Sec  Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 3 2 ...