题目链接 
老天……终于碰上一个除了模板之外的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. log日志应用 自定义的log

    package com.kxd.utils; import java.util.Hashtable; import android.util.Log; /** * The class for prin ...

  2. IOS typedef 函数指针的用法

    代码简化, 促进跨平台开发的目的. typedef 行为有点像 #define 宏,用其实际类型替代同义字. 不同点:typedef 在编译时被解释,因此让编译器来应付超越预处理器能力的文本替换. 用 ...

  3. SVN几个重要的问题

    本文不是系统地讲解SVN,只是对SVN中一些重要的或者笔者一直混淆的问题做简要归纳. SVN的安装可以参考笔者的另一篇技术随笔<SVN安装使用小结>. 1.既然能够通过SVN得到“每一个版 ...

  4. CodeForces 149D Coloring Brackets (区间DP)

    题意: 给一个合法的括号序列,仅含()这两种.现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红.要求不能有两个同颜色的括号相邻,问有多少种染色的方法? 思路: 这题的模拟成分比较多吧?两种颜 ...

  5. 目后佐道IT教育的品牌故事

    关于目后佐道 目后佐道IT教育作为中国IT职业教育领导品牌,致力于HTML5.UI.PHP.Java+大数据.Python+人工智能.Linux.产品经理.测试.运维等课程培训.100%全程面授,平均 ...

  6. 使用python模拟cookie登陆wooyun

    import urllib2 class SimpleCookieHandler(urllib2.BaseHandler): def http_request(self, req): simple_c ...

  7. fluent_python2

    字典和集合 泛映射类型, 继承自collections.abc, Mapping和MutableMapping 标准库里的所有映射类型都是利用 dict 来实现的,因此它们有个共同的限制,即只有可散列 ...

  8. UVa 12219 Common Subexpression Elimination (stl,模拟,实现)

    一般来说,把一颗子树离散成一个int,把一个结点的字符离散成一个int会方便处理 直接map离散.当然一个结点最多只有4个小写字母,也可以直接编码成一个27进制的整数,舍掉0,为了区分0和0000. ...

  9. ios 使用NSRegularExpression解析正则表达式

    初始化一个   NSRegularExpression 对象 注:_str是要匹配的字符串 NSRegularExpression *regex = [NSRegularExpression regu ...

  10. 开源项目: circular-progress-button

    带进度条显示的按钮, 其效果如下所示: 其由三部分动画组成: 初始状态->圆环状态->完成状态. 0. 实现从初始到圆环的简单实现: 继承自button 类, 设置其背景 public c ...