Hdu 2586 树链剖分求LCA
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的更多相关文章
- 树链剖分求LCA
树链剖分中各种数组的作用: siz[]数组,用来保存以x为根的子树节点个数 top[]数组,用来保存当前节点的所在链的顶端节点 son[]数组,用来保存重儿子 dep[]数组,用来保存当前节点的深度 ...
- cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!
2450. 距离 ★★ 输入文件:distance.in 输出文件:distance.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 在一个村子里有N个房子,一 ...
- cogs 2109. [NOIP 2015] 运输计划 提高组Day2T3 树链剖分求LCA 二分答案 差分
2109. [NOIP 2015] 运输计划 ★★★☆ 输入文件:transport.in 输出文件:transport.out 简单对比时间限制:3 s 内存限制:256 MB [题 ...
- HDU2586 How far away ? (树链剖分求LCA)
用树链剖分求LCA的模板: 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 const ...
- 【树链剖分】洛谷P3379 树链剖分求LCA
题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...
- 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- 【模板】树链剖分求LCA
洛谷3379 #include<cstdio> #include<algorithm> using namespace std; ,inf=1e9; int n,m,x,y,r ...
- hdu 5274 树链剖分
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- hdu 5893 (树链剖分+合并)
List wants to travel Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
随机推荐
- 关于优化for循环的注意的事项
for循环注意事项: 1.for循环内部尽量少做数据库查询之类的IO代价大的操作 2.尽量控制for循环的次数,不多做无用功 3.能一次加载在内存中的,就不要通过循环来多次查询数据库,除非数据量过大. ...
- swift使用查阅资料备份4
Swift - RxSwift的使用详解6(观察者2: 自定义可绑定属性) http://www.hangge.com/blog/cache/detail_1946.html extension UI ...
- Spring、Spring MVC、MyBatis 整合文件配置详解
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
- div+css布局自适应小结
一.两栏布局(左定宽,右自动)1. float + margin即固定宽度元素设置float属性为left,自适应元素设置margin属性,margin-left应>=定宽元素宽度.举例: &l ...
- 值得尝试的十款 GNOME Shell 扩展
值得尝试的十款 GNOME Shell 扩展 作者: JACK WALLEN 译者: 核子可乐 | 2016-09-22 17:10 评论: 6 收藏: 1 当 GNOME Shell(即 GNO ...
- pythone 学习笔记(粗略)
文档目录 概述 安装 基本语法 数据结构 4.1 数字和字符串类型 4.2 元祖 4.3 列表 4.4 字典 流程语句 5.1 分支结构 5.2 逻辑运算符(if) 5.3 循环 5.3.1 for ...
- javaScript(其他引用类型对象)
javascript其他引用类型对象 Global对象(全局)这个对象不存在,无形的对象,无法new一个 其内部定义了一些方法和属性:如下 encodeURI str = www.baidu.com ...
- JS中的异步
Hello,日常更新的我“浪”回来了!!! JS中有三座高山:异步和单线程.作用域和闭包.原型原型链 今天“浪”的主题是JS中的异步和单线程的问题. 主要从这三个方面入手 一.什么是异步(与同步作比较 ...
- 硬核官宣:台积电官宣6nm及7nm加强版工艺!
台积电正式宣布了6nm(N6)工艺,在已有7nm(N7)工艺的基础上大幅度增强,号称可提供极具竞争力的高性价比,而且能加速产品研发.量产.上市速度. 这几年,曾经执行业牛耳的Intel在新工艺方面进展 ...
- myeclipse导入工程 Some projects cannot be imported because they already exist in the workspace
问题描述: 1 第一次从外部导入工程或者新建工程,成功: 2 删除该工程,但是没有选择delete project contents on disk 3 再次需要该工程,导入该工程时出现警告:Some ...