题目链接 
老天……终于碰上一个除了模板之外的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. 自定义可伸缩的imageView

    直接上代码 /** * 自定义可伸缩的ImageView */ public class ZoomImageView extends ImageView { /** 画笔类 **/ private P ...

  2. bufferedinputStream操作

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  3. 安装Pywin32后无法正常引用pyd文件

    1. 首先在官方下载pywin32 2.下载完成后,无法正常引用pyd文件 3.解决方案: python安装目录\Lib\site-packages\pywin32_system32\* 至 C:\W ...

  4. codevs 2728 整数帝国问题(水题日常)

    时间限制: 1 s  空间限制: 16000 KB  题目等级 : 白银 Silver 题目描述 Description 在很久以前,在遥远的东方,有一个整数帝国,它里面里居住着大量的正整数,了缓解都 ...

  5. Unity3d中MonoBehavior默认函数的执行顺序和生命周期

    Awake()在MonoBehavior创建后就立刻调用,在脚本实例的整个生命周期中,Awake函数仅执行一次:如果游戏对象(即gameObject)的初始状态为关闭状态,那么运行程序,Awake函数 ...

  6. VM虚拟机下的Linux不能上网

    虚拟机linux上网配置 图解教程 首先查看window7主机下的网络配置VMNet1或VMNet8是否开启,其实linux系统的网络连接跟linux系统一致 在虚拟机界面将桥接改为NAT连接 点虚拟 ...

  7. ios之UIProgressView

    UIProgressView和UIActivityIndicator有些类似   但是不同之处在于, UIProgressView能够更加精确的反应进度 UIActivityIndicator则只能表 ...

  8. HTML 显示和隐藏浏览器滚动条

    滚动条和overflow有关 显示: overflow-x:auto; overflow-y:auto; overflow-x:scroll; overflow-y:scroll; 隐藏: overf ...

  9. 【贪心】「poj1328」Radar Installation

    建模:二维转一维:贪心 Description Assume the coasting is an infinite straight line. Land is in one side of coa ...

  10. 【数论】贝壳找房计数比赛&&祭facinv

    震惊!阶乘逆元处理背后竟有如此玄机…… 题目描述 贝壳找房举办了一场计数比赛,比赛题目如下. 给一个字符串 s 和字符串 t,求出 s 的所有去重全排列中 t 出现的次数.比如aab的去重全排列为aa ...