lca最短公共祖先模板(hdu2586)
题目链接: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)的更多相关文章
- LCA(最近公共祖先)模板
Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...
- LCA最近公共祖先模板(求树上任意两个节点的最短距离 || 求两个点的路进(有且只有唯一的一条))
原理可以参考大神 LCA_Tarjan (离线) TarjanTarjan 算法求 LCA 的时间复杂度为 O(n+q) ,是一种离线算法,要用到并查集.(注:这里的复杂度其实应该不是 O(n+q) ...
- LCA最近公共祖先模板代码
vector模拟邻接表: #include<iostream> #include<cstdio> #include<cstring> #include<cma ...
- LCA(最近公共祖先)之倍增算法
概述 对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. 如图,3和5的最近公共祖先是1,5和2的最近公共祖先是4 在本篇中我们先介 ...
- CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )
CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...
- lca 最近公共祖先
http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...
- Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)
Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...
- LCA近期公共祖先
LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...
- LCA 近期公共祖先 小结
LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...
随机推荐
- 【题解】[USACO12JAN]视频游戏的连击Video Game Combos
好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...
- NOIP2012 洛谷P1084 疫情控制
Description: H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控制疫情 ...
- TypeConverter使用
如下代码, <Window.Resources> <local:Human x:Key="human" Name="Tester1" Chil ...
- Java并发(11)- 有关线程池的10个问题
引言 在日常开发中,线程池是使用非常频繁的一种技术,无论是服务端多线程接收用户请求,还是客户端多线程处理数据,都会用到线程池技术,那么全面的了解线程池的使用.背后的实现原理以及合理的优化线程池的大小等 ...
- js实现2048小游戏
这是学完javascript基础,编写的入门级web小游戏 游戏规则:在玩法规则也非常的简单,一开始方格内会出现2或者4等这两个小数字,玩家只需要上下左右其中一个方向来移动出现的数字,所有的数字就会想 ...
- 为什么VS没有提供平win64程序编写项
最近在学习C++和MFC编程,突然有个疑问,为什么每次新建项目时,都只有win32 console application,从来没见过win64的选项,于是去网上查了查,下面是我找到的几个答案: 作者 ...
- js没有重载
javascript与其他语言(如java)不同,它没有传统意义上的重载(即为函数编写两个定义,只要这两个函数的参数类型或数量不同即可),在js中,后定义的函数会覆盖先前的函数.js中的参数在内部是用 ...
- Linux 下面adb命令的使用
平板或者android手机使用adb是非常方便的.接下来我就介绍下adb使用以及一些常用的命令. 1,连接 用adb连接线,一端接PC的USB中,一端接平板或手机的adb口,当然得确保线没有问题,而且 ...
- DWM.EXE进程(Desktop Window Manager)不能删除
英文全拼:Desktop Window Manager(DWM) 进程描述:桌面窗口管理器文件位置:C:\Windows\System32进程简介:桌面窗口管理器是windows Vista及wind ...
- Linux 内核链表的使用及深入分析【转】
转自:http://blog.csdn.net/BoArmy/article/details/8652776 1.内核链表和普通链表的区别 内核链表是一个双向链表,但是与普通的双向链表又有所区别.内核 ...