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. jsonObject的一些方法

    1.从前端传过来的数字,默认是Integer类型不能直接用Long接收 错误写法: 报错:Exception in thread "main" java.lang.ClassCas ...

  2. BNU4207:台风

    东方非想天则(TH12.3)是一款优秀的格斗游戏,其以华丽的弹幕,连贯的技能衔接及优美的音乐吸引了众多玩家(宅男更多-_-),而且各平台上也为其提供了联机的机会. 好了,言归正传,天气系统是本游戏的一 ...

  3. 配置Python实战开发环境

    一.安装Python和easy_install 和pip 新版本的linux下面应该带有这些环境,没有自带的话可以查找google配置. 二.配置python运行的虚拟化环境: 好处:Python的库 ...

  4. 转载-解决使用httpClient 4.3.x登陆 https时的证书报错问题

    今天在使用httpClient4.3.6模拟登陆https网站的时候出现了证书报错的问题,这是在开源中国社区里找到的可行的答案(原文链接:http://www.oschina.net/question ...

  5. ts和js中let和var定义变量的区别

    javascript 严格模式 第一次接触let关键字,有一个要非常非常要注意的概念就是”JavaScript 严格模式”,比如下述的代码运行就会报错: let hello = 'hello worl ...

  6. Service简介 demos

    extends:http://blog.csdn.net/ithomer/article/details/7364024 一. Service简介 Service是android 系统中的四大组件之一 ...

  7. C++ list容器系列功能函数详解

    C++ list函数详解 首先说下eclipse工具下怎样debug:方法:你先要设置好断点,然后以Debug方式启动你的应用程序,不要用run的方式,当程序运行到你的断点位置时就会停住,也会提示你进 ...

  8. 防止独立IP被其它恶意域名恶意解析

    一:什么是恶意域名解析 一般情况下,要使域名能访问到网站需要两步,第一步,将域名解析到网站所在的主机,第二步,在web服务器中将域名与相应的网站绑定.但是,如果通过主机IP能直接访问某网站,那么把域名 ...

  9. Git之删除仓库

    Github删除已有仓库步骤 在仓库页面点击设置 在新打开网页删除 输入仓库名点击删除即可

  10. python 的 ord()、 chr()、 unichr() 函数

    一. ord() 函数描述ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返 ...