bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)
P2912 [USACO08OCT]牧场散步Pasture Walking
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#define re register
using namespace std;
char gc(){
static char buf[],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
void read(int &x){
char c=getchar();x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=(x<<)+(x<<)+(c^),c=getchar();
}
void swap(int &a,int &b){a^=b;b^=a;a^=b;}
//-----优化------
#define N 1002
int n,Q,d[N],fa[][N],fv[][N];
int cnt,hd[N],nxt[N<<],ed[N],poi[N<<],val[N<<];
void adde(int x,int y,int v){
nxt[ed[x]]=++cnt; hd[x]=hd[x]?hd[x]:cnt;
ed[x]=cnt; poi[cnt]=y; val[cnt]=v;
}
void dfs(int x,int ffa){
fa[][x]=ffa; d[x]=d[ffa]+;
for(int i=;(<<i)<=d[x];++i){
fa[i][x]=fa[i-][fa[i-][x]];
fv[i][x]=fv[i-][x]+fv[i-][fa[i-][x]];//路径和预处理
}
for(int i=hd[x];i;i=nxt[i])
if(poi[i]!=ffa){
fv[][poi[i]]=val[i];
dfs(poi[i],x);
}
}
int ask(int x,int y){
if(d[x]<d[y]) swap(x,y);
int res=;
for(int i=;i>=;--i)
if(d[fa[i][x]]>=d[y])
res+=fv[i][x],x=fa[i][x];
if(x==y) return res;
for(int i=;i>=;--i)
if(fa[i][x]!=fa[i][y]){
res+=fv[i][x]+fv[i][y];
x=fa[i][x]; y=fa[i][y];
}
return res+fv[][x]+fv[][y];//记得最后还要加一次
}
int main(){
// freopen("chino.in","r",stdin);
read(n);read(Q); int q1,q2,q3;
for(re int i=;i<n;++i){
read(q1);read(q2);read(q3);
adde(q1,q2,q3); adde(q2,q1,q3);
}dfs(,);
for(re int i=;i<=Q;++i){
read(q1); read(q2);
printf("%d\n",ask(q1,q2));
}return ;
}
bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)的更多相关文章
- 洛谷——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 [2017年7月计划 树上问题 01]
P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 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 ...
- 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 ...
- [luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)
传送门 水题. 直接倍增求lca. x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)] ——代码 #include <cstdio> #include ...
- [USACO08OCT]牧场散步Pasture Walking BZOJ1602 LCA
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- Luogu 2912 [USACO08OCT]牧场散步Pasture Walking
快乐树剖 #include<cstdio> #include<cstring> #include<algorithm> #define rd read() #def ...
随机推荐
- Xcode 利用VVDocumenter 生成注释 通过设置 再生成注释文档
在写代码的时候,如果按照一定的规范在头文件里写上注释的话, 就可以利用Xcode的文档自动输出功能生成一份完整的HTML项目文档. 生成的格式和Apple Developer网站上的API文档几乎是一 ...
- Excel 2010 对号叉号怎么打出来
按小键盘数字键:Alt+41420 对号 按小键盘数字键:Alt+41642 叉号 http://jingyan.baidu.com/article/fdbd4277c228cdb89e3f482 ...
- TweenLite简单运用
参考:TweenLite说明 1 ease TweenLite.to(nextLevelTip, 1.5, { x:0, ease:Bounce.easeOut } ); 2 delayCall 语法 ...
- Mac环境下搭建PhoneGap环境
首先,官方文档中,要求,编译需要使用Intel架构的 Mac OS X系统的Mac设备,最低要使用XCode4.5以上版本和Mac OS X 10.7以上的系统以及IOS6.0 以上的SDK,至于发布 ...
- 不同linux下两网卡绑定方法
记得原来在做性能测试时,为了提高网络吞吐率.必须将两个网卡绑定一起工作.绑定方法如下: 一.CentOS 配置 1.编辑虚拟网络接口配置文件,指定网卡IP: # vi /etc/sysconfig ...
- Appium中长按按钮操作
在一次项目中,appium要对某个按钮进行长按操作(大于2s),类似拍微信小视频,参考网上长按视频会报错 action1 = TouchActions(self.driver) el = self.d ...
- Swift - 获取状态栏一些信息
// 获取状态栏的各种信息 :网络类型,运营商,电池电量,显示的系统时间等信息 import UIKit enum NetWorkType { case NetworkStatesNone // 没有 ...
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- lambda 缩写推演
- Oracle性能优化之Oracle里的执行计划
一.执行计划 执行计划是目标SQL在oracle数据库中具体的执行步骤,oracle用来执行目标SQL语句的具体执行步骤的组合被称为执行计划. 二.如何查看oracle数据库的执行计划 oracle数 ...