P2912 [USACO08OCT]牧场散步Pasture Walking

求树上两点间路径--->lca
使用倍增处理lca(树剖多长鸭)
 #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)的更多相关文章

  1. 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  2. 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]

    P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...

  3. LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...

  4. luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  5. 洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  6. 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 ...

  7. [luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)

    传送门 水题. 直接倍增求lca. x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)] ——代码 #include <cstdio> #include ...

  8. [USACO08OCT]牧场散步Pasture Walking BZOJ1602 LCA

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  9. Luogu 2912 [USACO08OCT]牧场散步Pasture Walking

    快乐树剖 #include<cstdio> #include<cstring> #include<algorithm> #define rd read() #def ...

随机推荐

  1. JS面向对象编程学习

    学习目标:1.掌握JS中的类(原型对象)和对象.2.什么是成员变量和成员方法.3.掌握构造方法的使用.补充:关于双等号(==):1.如果等号两边都是字符串时,则比较内容是否相等2.如果等号两边是数字时 ...

  2. JavaIO再回顾

    File类 JavaIO访问文件名和文件检测相关操作 分隔符最好是使用File类提供的File.separator,使程序更加的健壮. File类提供的方法基本上是见名知意,例如getName()就是 ...

  3. Android Framewrork资源类型有哪些?

    1. Google Framework res frameworks/base/core/res/res/values <public type="attr" name=&q ...

  4. sencha touch 问题汇总

    做sencha touch有一段时间了,目前而言,sencha touch在android上问题比较严重,在此对android中sencha touch的问题做一些汇总: 1.内存问题: 打包成安装程 ...

  5. 微信小程序 --- if/else条件渲染

    if 条件渲染:当为真的时候显示,当为假的时候隐藏: else 条件渲染:当为真的时候隐藏,当为假的时候显示: <view wx:if="{{true}}">{{tex ...

  6. Visual Studio实用小技巧

    有一个有关微软Office的笑话,说的是它的特性太多: 当你觉得自己发现了一个Office的新特性时,它已经存在很多年了. 本文将介绍一些在Visual Studio(免费下载)中很实用却被忽略的小技 ...

  7. 160225、解决纯js文件国际化的问题

    1.js中国际化 function test(){     alert("<s:text name='jsp.loading'/>"); }   2.最近在做strut ...

  8. jquery 实现点击按钮后出现倒计时效果(用于实现发送手机验证码、邮箱验证码)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Wireshark分析之TCP协议(二)

    (1)TCP首部格式 源端口:   用来传输数据报的端口 目标端口: 数据包将要发送到的端口 序号: 用来表示一个TCP片段.这个值用来表示数据流中的部分数据没有丢失 确认号:  表示通信中希望从另一 ...

  10. vue - nodejs

    一.知识 打开Nodejs英文网:https://nodejs.org/en/ 中文网:http://nodejs.cn/ 我们会发现这样一句话: 翻译成中文如下: Node.js 是一个基于 Chr ...