【BZOJ】1602:[Usaco2008 Oct]牧场行走
【算法】最近公共祖先(LCA)
【题解】
点x,y到最近公共祖先z的距离之和相当于x,y到根的距离减去两倍z到根的距离,
即ans=dis[x]+dis[y]-2*dis[z]
记得边数组要开两倍!!!T_T
#include<cstdio>
#include<algorithm>
using namespace std;
const double eps=1e-;
const int maxn=;
struct cyc{int from,to,w;}e[maxn*];
int dis[maxn],deep[maxn],f[maxn][],n,q,cnt,head[maxn];
bool vis[maxn];
void insert(int u,int v,int w)
{cnt++;e[cnt].from=head[u];e[cnt].to=v;e[cnt].w=w;head[u]=cnt;}
void dfs(int x)
{
vis[x]=;
for(int i=;(<<i)<=deep[x];i++)
f[x][i]=f[f[x][i-]][i-];
for(int i=head[x];i;i=e[i].from)
if(!vis[e[i].to])
{
int now=e[i].to;
deep[now]=deep[x]+;
dis[now]=dis[x]+e[i].w;
f[now][]=x;
dfs(now);
}
}
int lca(int x,int y)
{
if(deep[x]<deep[y])swap(x,y);
int d=deep[x]-deep[y];
for(int i=;(<<i)<=d;i++)
if((<<i)&d)x=f[x][i];
if(x==y)return x;
for(int i=;i>=;i--)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][];
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
insert(u,v,w);
insert(v,u,w);
}
dfs();
for(int i=;i<=q;i++)
{
int p1,p2;
scanf("%d%d",&p1,&p2);
printf("%d\n",dis[p1]+dis[p2]-*dis[lca(p1,p2)]);
}
return ;
}
也可以记录g[x][i],则不必减去多余的。
事实证明在一些简单的树上路径问题上,倍增比链剖更有优势。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
struct edge{int v,from,w;}e[maxn*];
int n,first[maxn],deep[maxn],f[maxn][],g[maxn][],v[maxn],tot,lca,q;
void insert(int u,int v,int w)
{tot++;e[tot].v=v;e[tot].w=w;e[tot].from=first[u];first[u]=tot;}
void dfs(int x,int fa)
{
for(int i=;(<<i)<=deep[x];i++)
{
f[x][i]=f[f[x][i-]][i-];
g[x][i]=g[x][i-]+g[f[x][i-]][i-];
}
for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa)
{
int y=e[i].v;
f[y][]=x;
deep[y]=deep[x]+;
g[y][]=e[i].w;
dfs(y,x);
}
}
int find(int x,int y)
{
int ans=;
if(deep[x]<deep[y])swap(x,y);
int d=deep[x]-deep[y];
for(int i=;(<<i)<=deep[x];i++)
if((<<i)&d){ans+=g[x][i];x=f[x][i];}
if(x==y)return ans;
for(int i=;i>=;i--)
if((<<i)<=deep[x]&&f[x][i]!=f[y][i])
{
ans+=g[x][i]+g[y][i];
x=f[x][i];y=f[y][i];
}
lca=f[x][];
return ans+g[x][]+g[y][];
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
insert(u,v,w);
insert(v,u,w);
}
dfs(,-);
for(int i=;i<=q;i++)
{
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",find(u,v));
}
return ;
}
【BZOJ】1602:[Usaco2008 Oct]牧场行走的更多相关文章
- bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 379 Solved: 216[Submit][Sta ...
- BZOJ 1602: [Usaco2008 Oct]牧场行走( 最短路 )
一棵树..或许用LCA比较好吧...但是我懒...写了个dijkstra也过了.. ---------------------------------------------------------- ...
- 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 ...
- BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题
Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- BZOJ 1602 [Usaco2008 Oct]牧场行走 dfs
题意:id=1602">链接 方法:深搜暴力 解析: 这题刚看完还有点意思,没看范围前想了想树形DP,只是随便画个图看出来是没法DP的,所以去看范围. woc我没看错范围?果断n^2暴 ...
- BZOJ 1602 USACO2008 Oct 牧场行走
翻翻吴大神的刷题记录翻到的... 乍一看是一个树链剖分吓瓜我...难不成吴大神14-10-28就会了树剖?orz... 再一看SB暴力都可过... 然后一看直接树上倍增码个就好了... 人生真是充满着 ...
- bzoj 1602: [Usaco2008 Oct]牧场行走【瞎搞】
本来想爆手速写个树剖,然而快下课了就手残写了了个n方的短小-- 暴力把查询的两个点中深的一个跳上来,加上边权,然后一起跳加边权就行了 #include<iostream> #include ...
- 1602: [Usaco2008 Oct]牧场行走
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1211 Solved: 616 [Submit][ ...
- 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...
随机推荐
- iOS 出现错误reason: image not found的解决方案
在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...
- Windows Sever 2008隐藏和系统属性
由于有些目录为隐藏和系统属性,首先要把 显示系统文件和显示所有文件 功能开启,把隐藏文件和目录显出来. 1.C:\Windows\Web\Wall*** 自带墙纸,不需要的可以删除掉. 2.C:\Wi ...
- SpringBoot2.0(三) 文件上传
SpringBoot中发起文件上传示例: /** * 文件上传 * @param multipartFile * @param path * @return */ @RequestMapping(va ...
- bootstrap 有些控件需要调用锚点,会与angular 路由 冲突
最简单的方法 就是 在 #号前加/, 但有人说 在服务器上回失效,也不知道是什么原理.慎用 最靠谱的方法 就 是 使用bootstrap中的js控制控件, 比如轮播图的上一页 下一页,就可以在 ang ...
- RT-thread内核之事件
一.事件控制块:在include/rtdef.h中 #ifdef RT_USING_EVENT /** * flag defintions in event */ #define RT_EVENT_F ...
- 廖雪峰老师Python教程读后笔记
廖老师网站:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 花几天时间看了廖老师的 ...
- P1297 [国家集训队]单选错位
题目背景 原 <网线切割>请前往P1577 题目描述 gx和lc去参加noip初赛,其中有一种题型叫单项选择题,顾名思义,只有一个选项是正确答案.试卷上共有n道单选题,第i道单选题有ai个 ...
- CentOS 设置环境变量
1. 查看环境变量,echo 命令用于在终端输出字符串或变量提取后的值,格式为“echo [字符串 | $变量]” echo $PATH /usr/local/bin:/usr/bin:/usr/lo ...
- [luoguT30208]太极剑
题面在这里 description 在一个圆环上给出\(n\)条端点在圆环上的绳子, 每次在圆环上切割的轨迹是一条直线,可以将可以将所有与这条直线相交的绳子切断. 求切割次数的最小值. data ra ...
- CF498D:Traffic Jams in the Land——题解
https://vjudge.net/problem/CodeForces-498D http://codeforces.com/problemset/problem/498/D 题面描述: 一些国家 ...