题目链接 
老天……终于碰上一个除了模板之外的LCA题了 
这道题用Tarjan来LCA。树上两个点的路径是唯一的,所以钦定一个根,两点间的路径就是两点到根的路径减去双倍的公共祖先到根的路径。
大概很好理解。

#include<cstdio>
#include<cctype>
#include<cstring>
#include<iostream>
using namespace std; inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} struct Edge{
int next,to,dis;
};
struct Pic{
Edge edge[];
int head[],num;
bool vis[];
inline void add(int from,int to,int dis){
edge[++num]=(Edge){head[from],to,dis};
head[from]=num;
}
}pic,que;
int dst[];
int ans[][];
bool vis[];
int father[];
int find(int x){ return x==father[x]? x:father[x]=find(father[x]); }
int unionn(int x,int y){ father[find(y)]=find(x); } void dfs(int x){
vis[x]=;
for(int i=pic.head[x];i;i=pic.edge[i].next){
if(vis[pic.edge[i].to]) continue;
dst[pic.edge[i].to]=dst[x]+pic.edge[i].dis;
dfs(pic.edge[i].to);
unionn(x,pic.edge[i].to);
}
for(int i=que.head[x];i;i=que.edge[i].next)
if(vis[que.edge[i].to])
ans[x][que.edge[i].to]=ans[que.edge[i].to][x]=dst[x]+dst[que.edge[i].to]-(dst[find(que.edge[i].to)]<<);
} int a[],b[]; int main(){
int n=read(),q=read();
for(int i=;i<n;++i){
int from=read(),to=read(),dis=read();
pic.add(from,to,dis);pic.add(to,from,dis);
father[i]=i;
}
father[n]=n;
for(int i=;i<=q;++i){
int from=read(),to=read();
a[i]=from;b[i]=to;
que.add(from,to,);
que.add(to,from,);
}
dfs();
for(int i=;i<=q;++i) printf("%d\n",ans[a[i]][b[i]]);
return ;
}

【Luogu】P2912牧场散步(TarjanLCA)的更多相关文章

  1. 洛谷P2912 牧场散步Pasture Walking

    题目描述 The \(N\) cows (\(2 \leq N \leq 1,000\)) conveniently numbered \(1..N\) are grazing among the N ...

  2. LCA【p2912】 牧场散步 (USACO08OCT)

    顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述-->p2912 牧场散步 题意概括 给定一个树,给你Q个询问,每次询问输入一个二元组\((x,y)\),要求求出\((x,y)\) ...

  3. LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...

  4. bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)

    P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...

  5. 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]

    P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...

  6. luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  7. BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...

  8. 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  9. 洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

随机推荐

  1. java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/xxx/lib/arm/liblame.so: has text relocations

    最近在写本地录音转码过程中引入了liblame.so,我这边用了不同系统版本的手机测试本地录音都没有出现问题,但是有一天,同事在测试的时候,出现了以下错误: 09-13 17:32:29.140 26 ...

  2. windows 操作系统种类

    @hcy 敬请访问:http://blog.sina.com.cn/iihcy Microsoft公司从1983年开始研制Windows系统,最初的研制目标是在MS-DOS的基础上提供一个多任务的图形 ...

  3. codevs 1742 爬楼梯(水题日常)

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 小明家外面有一个长长的楼梯,共N阶.小明的腿很长,一次能跨过一或两阶.有一天,他 ...

  4. 如何在腾讯云上安装Cloud Foundry

    Cloud Foundry是VMware推出的业界第一个开源PaaS云平台,它支持多种框架.语言.运行时环境.云平台及应用服务,使开发人员能够在几秒钟内进行应用程序的部署和扩展,无需担心任何基础架构的 ...

  5. 使用ABAP编程实现对微软Office Word文档的操作

    SAP ABAP里提供了一个标准的类CL_DOCX_DOCUMENT,提供了本地以".docx"结尾的微软Office word文档的读和写操作. 本文介绍了ABAP类CL_DOC ...

  6. crontab 应用

    可以用crontab -e 添加要执行的命令. 命令执行的结果,无论是标准输出还是错误输出,都将以邮件形式发给用户.            添加的命令必须以如下格式:    * * * * * /co ...

  7. HTML5资源汇总(更新游戏引擎cocos2d-html5)

    我也是现学现用,想了解的可以看看效果,想知道实现的也有源码 http://cocos2d-html5.org Cocos2d-HTML5 API和Cocos2d-x一致,同样的代码可以支持cocos2 ...

  8. javase(8)_集合框架_List、Set、Map

    一.集合体系(不包括Queue体系) 二.ArrayList ArrayList的属性 private transient Object[] elementData; //存储元素 private i ...

  9. 初涉tarjan缩点

    tarjan缩点:口胡过好多题,不过从来没写过…… 什么是缩点 tarjan和Kosaraju.Gabow算法一样,是为了求有向图中的强连通分量.因为有向图中大多数情况下会有环存在,而有环是一个不甚好 ...

  10. 【php】【运算符】位移运算符

    位运算符 &,|,!,^,<<,>> ···<<···左移一位值乘以2 ···>>···右移一位值除以2 超过总位数都会变为0 正负值移位运算符 ...