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
题目描述
N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草。 这n块土地被n-1条边连接。 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=Li<=10000)。 这些边被安排成任意两头奶牛都可以通过这些边到达的情况,所以说这是一棵树。 这些奶牛是非常喜欢交际的,经常会去互相访问,他们想让你去帮助他们计算Q(1<=q<=1000)对奶牛之间的距离。
输入
*第一行:两个被空格隔开的整数:N和Q
*第二行到第n行:第i+1行有两个被空格隔开的整数:AI,BI,LI
*第n+1行到n+Q行:每一行有两个空格隔开的整数:P1,P2,表示两头奶牛的编号。
输出
*第1行到第Q行:每行输出一个数,表示那两头奶牛之间的距离。
样例输入
2 1 2
4 3 2
1 4 3
1 2
3 2
样例输出
7
提示
来源
练习模板
倍增:
#include <algorithm>
#include <cstdio> using namespace std; const int N(+);
int n,q,x,y,z;
int dad[N<<][],deep[N<<];
int dis[N<<];
int sumedge,head[N<<];
struct Edge
{
int from,to,next,dis;
Edge(int from=,int to=,int next=,int dis=) :
from(from),to(to),next(next),dis(dis) {}
}edge[N<<]; int add(int from,int to,int dis)
{
edge[++sumedge]=Edge(from,to,head[from],dis);
return head[from]=sumedge;
} void DFS(int x)
{
deep[x]=deep[dad[x][]]+;
for(int i=;dad[x][i];i++)
dad[x][i+]=dad[dad[x][i]][i];
for(int i=head[x];i;i=edge[i].next)
if(!deep[edge[i].to])
{
dad[edge[i].to][]=x;
dis[edge[i].to]=dis[x]+edge[i].dis;
DFS(edge[i].to);
}
} int LCA(int x,int y)
{
if(deep[x]>deep[y]) swap(x,y);
for(int i=;i>=;i--)
if(deep[x]<=deep[dad[y][i]]) y=dad[y][i];
if(x==y) return x;
for(int i=;i>=;i--)
if(dad[x][i]!=dad[y][i])
x=dad[x][i],y=dad[y][i];
return dad[x][];
} int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z); add(y,x,z);
}
DFS();
for(;q;q--)
{
scanf("%d%d",&x,&y);
printf("%d\n",dis[x]+dis[y]-(dis[LCA(x,y)]<<));
}
return ;
}
然后是树剖的~
#include <algorithm>
#include <cstdio>
#include <vector> using namespace std; const int N(+);
vector< pair<int,int> >vec[N];
int n,q,x,y,z,dis[N];
int dad[N],deep[N],size[N],top[N]; void DFS(int x)
{
size[x]=; deep[x]=deep[dad[x]]+;
for(int i=;i<vec[x].size();i++)
if(dad[x]!=vec[x][i].first)
{
dad[vec[x][i].first]=x;
dis[vec[x][i].first]=dis[x]+vec[x][i].second;
DFS(vec[x][i].first);
size[x]+=size[vec[x][i].first];
}
} void DFS_(int x)
{
int t=; if(!top[x]) top[x]=x;
for(int i=;i<vec[x].size();i++)
if(dad[x]!=vec[x][i].first&&size[t]<size[vec[x][i].first]) t=vec[x][i].first;
if(t) top[t]=top[x],DFS_(t);
for(int i=;i<vec[x].size();i++)
if(dad[x]!=vec[x][i].first&&t!=vec[x][i].first) DFS_(vec[x][i].first);
} int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=dad[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
} int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
vec[x].push_back(make_pair(y,z));
vec[y].push_back(make_pair(x,z));
}
DFS(); DFS_();
for(;q;q--)
{
scanf("%d%d",&x,&y);
printf("%d\n",dis[x]+dis[y]-dis[LCA(x,y)]*);
}
return ;
}
BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking的更多相关文章
- 洛谷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 ...
- 洛谷 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]牧场行走(LCA模板)
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 379 Solved: 216[Submit][Sta ...
- BZOJ 1602: [Usaco2008 Oct]牧场行走( 最短路 )
一棵树..或许用LCA比较好吧...但是我懒...写了个dijkstra也过了.. ---------------------------------------------------------- ...
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题
Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...
- BZOJ 1602 [Usaco2008 Oct]牧场行走 dfs
题意:id=1602">链接 方法:深搜暴力 解析: 这题刚看完还有点意思,没看范围前想了想树形DP,只是随便画个图看出来是没法DP的,所以去看范围. woc我没看错范围?果断n^2暴 ...
- BZOJ 1602 USACO2008 Oct 牧场行走
翻翻吴大神的刷题记录翻到的... 乍一看是一个树链剖分吓瓜我...难不成吴大神14-10-28就会了树剖?orz... 再一看SB暴力都可过... 然后一看直接树上倍增码个就好了... 人生真是充满着 ...
随机推荐
- java web项目发生异常依然能运行
由于JavaWeb应用业务逻辑的复杂性,容易发生一些意想不到的错误和异常,给系统的调试带来不必要的麻烦,不友好的提示信息使编程者对错误和异常无从下手.特别是当发生异常时,Java异常栈输出的信息只能给 ...
- BestCoder Round #75 King's Order dp:数位dp
King's Order Accepts: 381 Submissions: 1361 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
- Android 推断程序在手机中是否是活动状态或者正在执行状态
沈阳斌子在今天项目需求上碰到个这种问题,在Service中须要推断当前的程序是否是活动状态,换句话说也就是说后台跑的服务中有业务需求检測当前程序是否是该服务的程序 这样好让点击推送通知时跳转到不同的页 ...
- hdu2688 Rotate(树状数组)
题目链接:pid=2688">点击打开链接 题意描写叙述:对一个长度为2<=n<=3000000的数组,求数组中有序对(i<j而且F[i]<F[j])的数量?其 ...
- poj1286 Necklace of Beads【裸polya】
非常裸的polya,只是我看polya看了非常久 吉大ACM模板里面也有 #include <cstdio> #include <cmath> #include <ios ...
- TNS-12555 / TNS-12560 / TNS-00525 Error listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPR
TNS-12555 / TNS-12560 / TNS-00525 Error listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPR ...
- 2015.05.11,外语,读书笔记-《Word Power Made Easy》 15 “如何谈论事情进展” SESSION 44
1. not the real McCoy simulate(['simjuleit] v. 假装,冒充,模仿,模拟)来自拉丁simulo,copy的意思.simulo本身派生自拉丁形容词simili ...
- scrollView中内部控件的悬停
以下图为例,图片,红色view和蓝色view是添加在scrollView上的,向上拖动,红色view停留在屏幕顶端不动,其它的继续滚动,向下拖动后,红色view跟着下来 代码如下:(注意的是scrol ...
- Caffe_Example之训练mnist
0.参考文献 [1]caffe官网<Training LeNet on MNIST with Caffe>; [2]薛开宇<读书笔记4学习搭建自己的网络MNIST在caffe上进行训 ...
- linux的chmod,chown命令 详解
指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...