【Luogu】P2912牧场散步(TarjanLCA)
题目链接
老天……终于碰上一个除了模板之外的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)的更多相关文章
- 洛谷P2912 牧场散步Pasture Walking
题目描述 The \(N\) cows (\(2 \leq N \leq 1,000\)) conveniently numbered \(1..N\) are grazing among the N ...
- LCA【p2912】 牧场散步 (USACO08OCT)
顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述-->p2912 牧场散步 题意概括 给定一个树,给你Q个询问,每次询问输入一个二元组\((x,y)\),要求求出\((x,y)\) ...
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)
P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...
- 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...
- luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 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 ...
- 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
随机推荐
- C#实现MD5WITHRSA签名
这是很久以前的写的一篇博客了,今天把他重新找出来整理一下发布到博客园 当时对接银联的时候搞了很久都没搞出来,后来一个偶然的机会发现类似的一个代码参考了一下终于弄好了 这段代码主要是实现了C#服务端对接 ...
- MFC程序添加快捷键
[问题提出] 有的程序需要自定义组合键完成一定功能,如何实现? [解决方法] RegisterHotKey函数原型及说明: BOOL RegisterHotKey( H ...
- IDEA 启用/禁用 Run Dashboard
一.启用 方式一: 创建/打开一个SpringBoot项目[或者点击Run --> Edit Configurations 添加 Spring Boot 类型的项目配置:或者如图在红框处添加配置 ...
- HDU 1964 Pipes (插头DP,变形)
题意:给一个n*m的矩阵,每个格子都是必走的,且无障碍格子,每对格子之间都有一个花费,问哈密顿回路的最小花费. 思路: 这个和Formula1差不多,只是求得是最小花费,这只需要修改一下DP值为花费就 ...
- make与makefile的几个例子和(自己写一下,汗!忘记了!)总结
共用的几个源代码文件: main.c 2.c 3.c 代码依次为: #include<stdlib.h> #include "a.h" extern void func ...
- 快速WCF
初级原理:通得过地址调用接口服务,接口服务调用对应实现方法 援引文章地址:http://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049. ...
- UVA 12549 Sentry Robots (最小点覆盖)
这道题挺像hdu 5093 Battle ships的,不过那道题是要求最多放置的点数,而这道题是要求最小点覆盖. 顶点覆盖的定义是:在G中任意边至少有一个端点属于顶点集合S. 一个重要的位置有(x, ...
- Itunes共享机制实现
http://www.raywenderlich.com/1948/itunes-tutorial-for-ios-how-to-integrate-itunes-file-sharing-with- ...
- Codeforces Round #277.5 (Div. 2)-B. BerSU Ball
http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...
- No-11.变量进阶
变量进阶 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是保存在 内存 中的 在 Python 中 函数 的 参数传递 以及 返回值 都是靠 引用 传递 ...