题目链接 
老天……终于碰上一个除了模板之外的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多态的应用

    //多态的应用 class Animal{     public void eat(){     } } class Dog extends Animal{     public void eat() ...

  2. [文章泛读] The varying faces of a program transformation systems (ACM Inroads, 2012)

    Beevi S. Nadera, D. Chitraprasad, and Vinod S. S. Chandra. 2012. The varying faces of a program tran ...

  3. js生成txt文件

    HTML CODE: <div class="modal-footer"> <a onfocus="this.blur();" id=&quo ...

  4. Kubernetes里的ConfigMap的用途

    顾名思义,ConfigMap用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件. ConfigMap同Kubernetes的另一个概念secret类似,区别是ConfigMap主要 ...

  5. 使用JDK自带的工具jstack找出造成运行程序死锁的原因

    Java多线程编程也是Java面试中经常考察的内容.刚接触Java多线程编程的朋友们,可能会不慎写出一些会导致死锁(deadlock)的应用出来.如何分析造成Java多线程的原因呢?很多时候我们在怀疑 ...

  6. poj2312Battle City BFS

    题意: M行N列矩阵, 'Y'表示开始位置, 'T'表示目标位置, 从开始位置到目标位置至少需要走多少步,其中, 'S', 'R'表示不能走, 'B' 花费为2, 'E'花费为1. 思路:纯 BFS. ...

  7. selenium-元素的定位

    前戏 元素的定位是自动化测试的核心,要想操作一个元素,首先应该识别这个元素.Webdriver 提供了一系列的元素定位方法,常用的有 id,name,class name,link text,part ...

  8. springmvc导出excel(POI)

    /** * 导出excel表格 */ @RequestMapping(value = "/doExportData", method = {RequestMethod.POST, ...

  9. Bzoj 1257 [CQOI2007]余数之和 (整除分块)

    Bzoj 1257 [CQOI2007]余数之和 (整除分块) 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1257 一道简单题. 题目 ...

  10. NOIP 模拟题

    目录 T1 : grid T2 : ling T3 : threebody 数据可私信我. T1 : grid 题目:在一个\(n*n\)的方格中,你只能斜着走.为了让问题更简单,你还有一次上下左右走 ...