LCA【p2912】 牧场散步 (USACO08OCT)
顾z
你没有发现两个字里的blog都不一样嘛 qwq
题目描述-->p2912 牧场散步
题意概括
给定一个树,给你Q个询问,每次询问输入一个二元组\((x,y)\),要求求出\((x,y)\)的距离.
明显带权lca.
这里写一下递推式
\]
\]
定义:
\(f[u][i]\)代表\(u\)向上跳\(2^i\)步到达的节点.
\(gw[u][i]\)代表\(u\)向上跳\(2^i\)步到达的节点的边权和.
为什么\(f[u][i]=f[f[u][i-1]][i-1]\)?
我们从\(u\)到达\(f[u][i]\)需要\(2^i\)步,而到达\(f[u][i-1]\)需要\(2^{i-1}\)步,再从这个位置跳\(2^{i-1}\)步,的话就到达了\(f[u][i]\)。
\]
又因为我们处理\(f[u][i-1]\)一定比处理\(f[u][i]\)要早,所以这样转移即可.
初始化
f[u][0]=fa;
gw[u][0]=edge[i].w;//即连向u的边权.
然后这样这个题就变成了裸的带权lca问题 qwq.
---------------------代码---------------------
#include<bits/stdc++.h>
#define R register
#define N 1008
using namespace std;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
int n,head[N],tot,m;
int depth[N],f[N][18],gw[N][18];
struct cod{int u,v,w;}edge[N<<1+8];
inline void add(int x,int y,int z)
{
edge[++tot].u=head[x];
edge[tot].v=y;
edge[tot].w=z;
head[x]=tot;
}
void dfs(int u,int fa,int dis)
{
depth[u]=depth[fa]+1;
f[u][0]=fa;gw[u][0]=dis;
for(R int i=1;(1<<i)<=depth[u];i++)
f[u][i]=f[f[u][i-1]][i-1],
gw[u][i]=gw[f[u][i-1]][i-1]+gw[u][i-1];
for(R int i=head[u];i;i=edge[i].u)
{
if(edge[i].v==fa)continue;
dfs(edge[i].v,u,edge[i].w);
}
}
inline int lca(int x,int y)
{
if(depth[x]>depth[y])swap(x,y);
int ans=0;
for(R int i=17;i>=0;i--)
if(depth[y]-(1<<i)>=depth[x])
ans+=gw[y][i],y=f[y][i];
if(x==y)return ans;
for(R int i=17;i>=0;i--)
{
if(f[x][i]==f[y][i])continue;
ans+=gw[x][i]+gw[y][i];
y=f[y][i],x=f[x][i];
}
return (ans+gw[x][0]+gw[y][0]);
}
int main()
{
in(n),in(m);
for(R int i=1,x,y,z;i<n;i++)
{
in(x),in(y),in(z);
add(x,y,z);add(y,x,z);
}
dfs(1,0,0);
for(R int x,y;m;m--)
{
in(x),in(y);
printf("%d\n",lca(x,y));
}
}
LCA【p2912】 牧场散步 (USACO08OCT)的更多相关文章
- 洛谷P2912 牧场散步Pasture Walking
题目描述 The \(N\) cows (\(2 \leq N \leq 1,000\)) conveniently numbered \(1..N\) are grazing among the N ...
- 【Luogu】P2912牧场散步(TarjanLCA)
题目链接 老天……终于碰上一个除了模板之外的LCA题了 这道题用Tarjan来LCA.树上两个点的路径是唯一的,所以钦定一个根,两点间的路径就是两点到根的路径减去双倍的公共祖先到根的路径.大概很好理解 ...
- bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)
P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...
- 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- [USACO08OCT]牧场散步Pasture Walking BZOJ1602 LCA
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 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 ...
随机推荐
- 利用Xtrabackup搭建GTID主从复制(一主一从)
Preface I've been demonstrated how to implement a master-slave structure using mysqldump in ...
- Python全栈(一)编程语言介绍
一.编程语言介绍 程序是计算机能读懂的语言,是人和计算机沟通的方式. 计算机无法理解符号,只能理解0,1的二进制. 计算机内的运行状态就像灯泡的开关一样来表示各庄状态,两个灯泡能表示4种状态,无数的灯 ...
- obj = object(),所创建的obj实例到底是个啥?
In[1]: dir(obj) Out[1]:['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '_ ...
- 使用dib element proliant-tools制作deploy image
element proliant-tools会在ipa ramdisk中安装一个rpm包hpssacli(HP的RAID管理工具),和一个python module proliantutils(里面P ...
- CentOS 6.3安装配置LAMP服务器(Linux+Apache+MySQL+PHP5)
服务器系统环境:CentOS 6.3 客户端系统环境:Windows 7 ultimate(x86)sp1 简体中文旗舰版 ※ 本文档描述了如何在Linux服务器配置Apache.Mysql.PHP ...
- Android记事本11
昨天: Activity的启动模式. 今天: 分析了一些网上的例子的源码. 遇到问题: 无.
- hdu1712 分组背包 ACboy needs your help
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- nio的reactor模式
转自:http://blog.csdn.net/it_man/article/details/38417761 线程状态转换图 就是非阻塞IO 采用多路分发方式举个例子吧,你服务器做一个聊天室,按照以 ...
- hdu 1172 猜数字
猜数字 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- JZOJ 5280 膜法师
好啰嗦......还好作者给了一句话题意,不然光看题就很耗费时间. 样例输入: 1 6 3 1 78 69 55 102 233 666 样例输出: 1 2 3 4 5 6 11 数据范围: 思路: ...