1602: [Usaco2008 Oct]牧场行走

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1084  Solved: 556
[Submit][Status]

Description

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)对奶牛之间的距离。

Input

*第一行:两个被空格隔开的整数:N和Q

*第二行到第n行:第i+1行有两个被空格隔开的整数:AI,BI,LI

*第n+1行到n+Q行:每一行有两个空格隔开的整数:P1,P2,表示两头奶牛的编号。

Output

*第1行到第Q行:每行输出一个数,表示那两头奶牛之间的距离。

Sample Input

4 2
2 1 2
4 3 2
1 4 3
1 2
3 2

Sample Output

2
7

HINT

 

Source

题解:
不想写倍增了,把树链剖分的代码贴上改了改就A了。。。
每个节点保存的是到父亲的距离,所以对LCA要特判
代码:
 const maxn=+;
type node1=record
go,next,z:longint;
end;
node2=record
l,r,mid,sum:longint;
end; var e:array[..*maxn] of node1;
t:array[..*maxn] of node2;
p,a,v,fa,s,head,dep,son,top:array[..maxn] of longint;
i,n,m,x,y,z,sz,ans,tot:longint;
ch:char;
procedure swap(var x,y:longint);
var t:longint;
begin
t:=x;x:=y;y:=t;
end;
procedure insert(x,y,z:longint);
begin
inc(tot);
e[tot].go:=y;e[tot].z:=z;e[tot].next:=head[x];head[x]:=tot;
end;
function min(x,y:longint):longint;
begin
if x<y then exit(x) else exit(y);
end;
function max(x,y:longint):longint;
begin
if x>y then exit(x) else exit(y);
end; procedure dfs1(x:longint);
var i,j,y:longint;
begin
j:=;s[x]:=;
i:=head[x];
while i<> do
begin
y:=e[i].go;
if dep[y]= then
begin
dep[y]:=dep[x]+;
fa[y]:=x;v[y]:=e[i].z;
dfs1(y);
inc(s[x],s[y]);
if s[y]>s[j] then j:=y;
end;
i:=e[i].next;
end;
son[x]:=j;
end;
procedure dfs2(x,chain:longint);
var i,y:longint;
begin
inc(sz);p[x]:=sz;top[x]:=chain;
if son[x]<> then dfs2(son[x],chain);
i:=head[x];
while i<> do
begin
y:=e[i].go;
if (p[y]=) and (y<>son[x]) then dfs2(y,y);
i:=e[i].next;
end;
end;
procedure pushup(k:longint);
begin
t[k].sum:=t[k<<].sum+t[k<<+].sum;
end; procedure build(k,x,y:longint);
begin
with t[k] do
begin
l:=x;r:=y;mid:=(l+r)>>;
if l=r then begin sum:=a[l];exit;end;
build(k<<,l,mid);build(k<<+,mid+,r);
pushup(k);
end;
end;
function getsum(k,x,y:longint):longint;
begin
with t[k] do
begin
if (l=x) and (r=y) then exit(sum);
if y<=mid then exit(getsum(k<<,x,y))
else if x>mid then exit(getsum(k<<+,x,y))
else exit(getsum(k<<,x,mid)+getsum(k<<+,mid+,y));
end;
end;
procedure init;
begin
readln(n,m);
for i:= to n- do begin readln(x,y,z);insert(x,y,z);insert(y,x,z);end;
dep[]:=;
dfs1();
dfs2(,);
for i:= to n do a[p[i]]:=v[i];
build(,,n);
end;
procedure solvesum;
begin
ans:=;
readln(x,y);
while top[x]<>top[y] do
begin
if dep[top[x]]<dep[top[y]] then swap(x,y);
inc(ans,getsum(,p[top[x]],p[x]));
x:=fa[top[x]];
end;
if dep[x]>dep[y] then swap(x,y);
inc(ans,getsum(,p[x],p[y]));
if p[x]<> then dec(ans,v[x]);
writeln(ans);
end; procedure main;
begin
for i:= to m do solvesum;
end; begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
init;
main;
close(input);close(output);
end.
 

BZOJ1602: [Usaco2008 Oct]牧场行走的更多相关文章

  1. [BZOJ1602] [Usaco2008 Oct] 牧场行走 (LCA)

    Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...

  2. 【bzoj1602】[Usaco2008 Oct]牧场行走

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1793  Solved: 935[Submit][St ...

  3. bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 379  Solved: 216[Submit][Sta ...

  4. BZOJ 1602: [Usaco2008 Oct]牧场行走( 最短路 )

    一棵树..或许用LCA比较好吧...但是我懒...写了个dijkstra也过了.. ---------------------------------------------------------- ...

  5. 1602: [Usaco2008 Oct]牧场行走

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1211  Solved: 616 [Submit][ ...

  6. 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...

  7. BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题

    Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...

  8. 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 ...

  9. LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...

随机推荐

  1. html 之前学习响应式的笔记

    响应式的设计,根据用户设备的不同,用户屏幕大小不同,提供不同的网页设计http://mediaqueri.es/PhoneGap 使用2,如何模拟手机设备chome 浏览器 在32以上设备检测用 de ...

  2. exists查询中子表可以是

    exists查询中子表可以是’或则具体某一列 ,查询结果一致,因为exists只会返回 true或者false,一个boolean型的值

  3. PHP 内存的分布问题

    php运行,内存分为5个区域, 1.基本数据类型--->栈区 2.符合数据类型-->堆区 对象实例在堆区,对象名字在栈区(指向此对象实例的变量)

  4. VS2010打不开VS2012 .NET MVC 工程,及打开后部分模块加载不正确的解决办法

    转自http://www.xuebuyuan.com/2042634.html 首先,如果sln打开不正确,用(notepad++)打开sln 比如 VS2010的前两行为: Microsoft Vi ...

  5. IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET

    转载至码农SeraphWU IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET 进入CMD 输入如下命令即可 "%WINDIR%\Microsoft.NET\Framework\ ...

  6. setTimeout 和 setInterval区别

    setTimeout和setIntelval都有定时的功能!!!取消定时功能的时候,都有对应的clearTimeout以及clearInterval与之对应. 但是他们之间是有区别的! setTime ...

  7. Codevs 4768 跳石头 NOIP2015 DAY2 T1

    4768 跳石头 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 一年一度的"跳石头"比赛又要开始了! ...

  8. 让QMainWindow也表现出QDialog的exec函数的特征

    前几天在做毕业设计项目的时候,使用的PyQt4,想实现这么样一个功能: 场景描述:主窗口a(QMainWindow类型)和主窗口b(QMainWindow),b是通过a窗口中某一个按钮弹出来的. 功能 ...

  9. jQuery 标签淡入淡出 个人随笔

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. GoogleCode新手教程

    GoogleCode页面介绍 Project Home 首先显示的是project home,页面左边的是这个项目的介绍,右边的License是说明使用的是什么开源协议,Labels是标签的意思,就是 ...