lca板子
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,q,k;
LL ans,dis[M];
LL f[M][],vis[M],deep[M];
LL first[M],cnt;
struct node{LL to,next,w;}e[M];
void ins(LL a,LL b,LL w){e[++cnt]=(node){b,first[a],w}; first[a]=cnt;}
void insert(LL a,LL b,LL w){ins(a,b,w); ins(b,a,w);}
void dfs(LL x){
vis[x]=;
for(int i=;(<<i)<=deep[x];i++) f[x][i]=f[f[x][i-]][i-];
for(int i=first[x];i;i=e[i].next){
int now=e[i].to;
if(!vis[now]){
deep[now]=deep[x]+;
f[now][]=x;
dis[now]=dis[x]+e[i].w;
dfs(now);
}
}
}
int find(int x,int y){
if(deep[x]<deep[y]) swap(x,y);
int d=deep[x]-deep[y];
for(int i=;(<<i)<=d;i++) if((<<i)&d) x=f[x][i];
if(x==y) return x;
for(int i=;i>=;i--)
if((<<i)<=deep[x]&&f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][];
}
int main(){
int x,y,w;
n=read();
for(int i=;i<n;i++) x=read(),y=read(),w=read(),insert(x,y,w);
dfs();
q=read(); k=read();
for(int i=;i<=q;i++){
x=read(); y=read();
ans=;
int s1=find(x,k),s2=find(y,k);
LL sum1=dis[x]+dis[k]-dis[s1]*;
LL sum2=dis[y]+dis[k]-dis[s2]*;
ans=ans+sum1+sum2;
printf("%lld\n",ans);
}
return ;
}
lca板子的更多相关文章
- bzoj-1787-洛谷-4281(LCA板子题)
传送门(bzoj) 传送门(洛谷) 可以说这道也是一个板子题 由于题中是三个人需经过的路径最短 就会有一点点不太一样 那么 就两两求LCA 这样之后就会出现两种状况 一.所得到的三个LCA是相等的 那 ...
- hdu - 2586 (LCA板子题)
传送门 (这次的英文题面要比上一个容易看多了) (英语蒟蒻的卑微) 又是一个很裸的LCA题 (显然,这次不太容易打暴力咧) (但听说还是有大佬用dfs直接a掉了) 正好 趁这个机会复习一下LCA 这里 ...
- Nearest Common Ancestors(LCA板子)
题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 1000 ...
- 用tarjan求LCA板子(比倍增快)
懒!!直接转载!!!! https://solstice23.top/archives/62
- [板子]倍增LCA
倍增LCA板子,没有压行,可读性应该还可以.转载请随意. #include <cstdio> #include <cstring> #include <algorithm ...
- SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解
题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...
- 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增
倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...
- 【テンプレート】LCA
LCA目前比较流行的算法主要有tarjian,倍增和树链剖分 1)tarjian 是一种离线算法,需要提前知道所有询问对 算法如下 1.读入所有询问对(u,v),并建好树(建议邻接表) 2.初始化每个 ...
- 「刷题笔记」LCA问题相关
板子 ll lg[40]; ll dep[N],fa[N][40]; ll dis[N]; void dfs(ll u,ll f) { dep[u]=dep[f]+1; fa[u][0]=f; for ...
随机推荐
- nginx+php整合(是让nginx可以运行php,以及下载地址)
下载地址: nginx:http://nginx.org/en/download.html PHP: https://windows.php.net/download/ 都是官网的自己选择版本 安装文 ...
- ABAP自定义截取字符串长度函数
SAP 中strlen()只能计算字符串的个数,不能计算含有中文字符串的长度,如字符串“SAP大波霸”,strlen('SAP大波霸') = 6,其实真实长度为3+3*2 = 9.我们可以通过cl_a ...
- urllib使用三--urlretrieve下载文件
下载文件 urllib.urlretrieve() 参数: url:远程地址 filename:要保存到本地的文件 reporthook:下载状态报告 data:有就变成POST请求,有格式要求 返回 ...
- IE浏览器调用ActiveX获取U盘中的文件
<p> <script type="text/javascript" language="javaScript">// <![CD ...
- Android 打印方法调用堆栈
RuntimeException here = new RuntimeException("here"); here.fillInStackTrace(); Log.w(" ...
- Centos 7.X 安装JDK1.8
一.查看本机jdk版本并卸载原有openjdk 查看 # java -version openjdk version "1.8.0_144" ...
- 远程 RADIUS 服务器组
远程 RADIUS 服务器组 远程 RADIUS 服务器组是包含一个或多个 RADIUS 服务器的已命名的组.IAS 用作 RADIUS 请求消息的 RADIUS 代理时,必须指定远程 RADIUS ...
- VSX-5 VSXMusic 编码听音乐
给VS写个扩展来听音乐的说法,缘来已久,这两天做了个初版,使用豆瓣FM,先发出来. 插件现在只做了VS2013的,别的版本有待下一步支持. 现在暂时只有一个Adornment(就是那个有封面的),一个 ...
- 【Substring with Concatenation of All Words】cpp
题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...
- 孤荷凌寒自学python第五十四天使用python来删除Firebase数据库中的文档
孤荷凌寒自学python第五十四天使用python来删除Firebase数据库中的文档 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数据库 ...