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 ...
随机推荐
- windows下配置VisualSVN Server服务器
下载安装文件: 服务端安装文件:VisualSVN-Server-1.6.2 客户端安装文件:TortoiseSVN-1.5.5.14361-win32-svn-1.5.4 上面是我使用的版本. 在V ...
- C++11新特性之二——std::bind std::function 高级用法
/* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ #include <iostream> #includ ...
- bootstrap里面的popover组件如何使鼠标移入可以对弹出框进行一系列的操作
在bootstrap里面,有一个组件很可爱,它就是popover,它是对标签title属性的优化,奉上连接一枚:http://docs.demo.mschool.cn/components/popov ...
- JS Date parse
因为JS中的Date转换格式没有“-”这种间隔符,Date.parse会生成NAN,所以只能进行转换. <script type="text/javascript"> ...
- 树形DP+背包(poj1155泛化分组背包)
TELE Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3675 Accepted: 1936 Description ...
- python下几种打开文件的方式
昨天看完了这本python进阶,感觉这本书对我启发很大,做了三张纸的笔记,方便我在遇到问题的时候翻阅,然后寻找可能的解决方案.作为一个使用Python一年的小白,虽然说不是小白,但是这一年来基本上是用 ...
- centos6.8 环境一键安装包 nginx配置thinkphp5
---恢复内容开始--- lnmp1.4 一键安装包 nginx配置thinkphp5 环境:Nginx1.12.1 PHP5.6 Coentos6.8 修改网站配置文件 ser ...
- poj2376 Cleaning Shifts【线段树】【DP】
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32561 Accepted: 7972 ...
- 快速定位oracle故障-恩墨
首先我们要明白一点,所谓的故障,意味着相对来讲比较严重.也就是可能比不同的问题要严重一些,比如锁等待. 要能够快速的定位和解决问题,恢复业务正常:首先我们需要了解Oracle的一些常见的故障有哪些. ...
- rest_framework知识总汇
RESTful规范 rest_framework基础 rest_framework基本组件(权限.认证.频率) rest_framework渲染器 rest_framework版本控制 解析器.路由控 ...