bzoj3242
如果是树,那么一定选择树的直径的中点
套了个环?裸的想法显然是断开环,然后求所有树的直径的最小值
环套树dp的一般思路,先把环放到根,把环上点下面的子树dp出来,然后再处理环上的情况
设f[i]表示以i为根的子树向下延伸的最长链长度
我们把环上的点扩展一倍,用前缀和维护环上的边权
依次枚举断点,则对直径影响就是max(s[j]-s[i]+f[i]+f[j])
分别维护max(s[j]+f[j])和max(f[i]-s[i])
注意i≠j,所以我们还要维护一个次小值
type node=record
po,next,num:longint;
end;
link=record
loc:longint;
mx:int64;
end; var tree:array[..*,..] of link;
e:array[..] of node;
cut:array[..] of boolean;
a,b,p,fa:array[..] of longint;
v:array[..] of boolean;
f,s:array[..] of int64;
i,len,n,t,x,y,z:longint;
ll,ans,lin:int64;
l1,l2,tp:link; function max(a,b:int64):int64;
begin
if a>b then exit(a) else exit(b);
end; function min(a,b:int64):int64;
begin
if a>b then exit(b) else exit(a);
end; procedure add(x,y,z:longint);
begin
inc(len);
e[len].po:=y;
e[len].next:=p[x];
e[len].num:=z;
p[x]:=len;
end; procedure find(x:longint);
var i,y,h:longint;
begin
i:=p[x];
while i<>- do
begin
y:=e[i].po;
if not cut[i] then
begin
if fa[y]<> then
begin
h:=x;
while h<>y do
begin
inc(t);
a[t]:=h;
b[t]:=f[h];
h:=fa[h];
end;
inc(t);
a[t]:=h;
b[t]:=e[i].num;
break;
end
else begin
cut[i]:=true;
cut[i xor ]:=true;
fa[y]:=x;
f[y]:=e[i].num;
find(y);
end;
end;
if t<> then break;
i:=e[i].next;
end;
end; procedure dfs(x:longint);
var i,y:longint;
begin
v[x]:=true;
f[x]:=;
i:=p[x];
while i<>- do
begin
y:=e[i].po;
if not v[y] then
begin
dfs(y);
lin:=max(lin,f[y]+f[x]+e[i].num);
f[x]:=max(f[x],f[y]+e[i].num);
end;
i:=e[i].next;
end;
end; procedure update(var c:link; a:link; b:link);
begin
c.mx:=a.mx;
c.loc:=a.loc;
if c.mx<b.mx then
begin
c.loc:=b.loc;
c.mx:=b.mx;
end;
end; procedure build(i,l,r:longint);
var m:longint;
begin
if l=r then
begin
tree[i,].mx:=f[a[l]]+s[l];
tree[i,].loc:=l;
tree[i,].mx:=f[a[l]]-s[l];
tree[i,].loc:=l;
end
else begin
m:=(l+r) shr ;
build(i*,l,m);
build(i*+,m+,r);
update(tree[i,],tree[i*,],tree[i*+,]);
update(tree[i,],tree[i*,],tree[i*+,]);
end;
end; function ask(i,l,r,w,x,y:longint):link;
var m:longint;
s,s1,s2:link;
begin
if (x<=l) and (y>=r) then exit(tree[i,w])
else begin
m:=(l+r) shr ;
if x>m then exit(ask(i*+,m+,r,w,x,y));
if y<=m then exit(ask(i*,l,m,w,x,y));
s1:=ask(i*,l,m,w,x,y);
s2:=ask(i*+,m+,r,w,x,y);
update(s,s1,s2);
exit(s);
end;
end; begin
len:=-;
fillchar(p,sizeof(p),);
readln(n);
for i:= to n do
begin
readln(x,y,z);
add(x,y,z);
add(y,x,z);
end;
fa[]:=-;
find();
for i:= to t do
begin
v[a[i]]:=true;
a[i+t]:=a[i];
b[i+t]:=b[i];
end;
for i:= to *t do
s[i]:=s[i-]+b[i-];
for i:= to t do
dfs(a[i]);
build(,,*t);
ans:=;
for i:= to t do
begin
l1:=ask(,,*t,,i+,i+t);
l2:=ask(,,*t,,i+,i+t);
if l1.loc=l2.loc then
begin
ll:=;
if l1.loc>i+ then
begin
tp:=ask(,,*t,,i+,l1.loc-);
ll:=max(ll,l1.mx+tp.mx);
end;
if l2.loc<i+t then
begin
tp:=ask(,,*t,,l2.loc+,i+t);
ll:=max(ll,l2.mx+tp.mx);
end;
ans:=min(ans,max(ll,lin));
end
else ans:=min(ans,max(l1.mx+l2.mx,lin));
end;
writeln(ans/::);
end.
bzoj3242的更多相关文章
- 【BZOJ3242】【NOI2013】快餐店(动态规划)
[BZOJ3242][NOI2013]快餐店(动态规划) 题面 BZOJ 题解 假设我们要做的是一棵树,那么答案显然是树的直径的一半. 证明? 假设树的直径是\(2d\),那么此时最远点的距离是\(d ...
- BZOJ3242 [Noi2013]快餐店 【环套树 + 单调队列dp】
题目链接 BZOJ3242 题解 题意很清楚,找一点使得最远点最近 如果是一棵树,就是直径中点 现在套上了一个环,我们把环单独拿出来 先求出环上每个点外向树直径更新答案,并同时求出环上每个点外向的最远 ...
- 【BZOJ3242】【UOJ#126】【NOI2013】快餐店
NOI都是这种难度的题怎么玩嘛QAQ 原题: 小T打算在城市C开设一家外送快餐店.送餐到某一个地点的时间与外卖店到该地点之间最短路径长度是成正比的,小T希望快餐店的地址选在离最远的顾客距离最近的地方. ...
- BZOJ3242/UOJ126 [Noi2013]快餐店
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- bzoj3242 [Noi2013]快餐店
Description 小T打算在城市C开设一家外送快餐店.送餐到某一个地点的时间与外卖店到该地点之间最短路径长度是成正比的,小T希望快餐店的地址选在离最远的顾客距离最近的地方. 快餐店的顾客分布在城 ...
- BZOJ3242 快餐店
原题传送门 题意 给定一个n条边n个点的连通图,求该图的某一点在该图距离最远的点距离它的距离的最小值. 题解 显然,答案是\(\frac {原图直径}{2}\). 本体的图有 \(n\) 个点 \(n ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
随机推荐
- LintCode-Majority Number
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- Linux内核分析作业一
一.实验 通过反汇编一个简单的c语言程序来分析计算机是如何工作的 1.进入实验楼,在实验楼环境下把c语言代码转换成汇编码 汇编代码如下图: 二.汇编代码的工作过程中堆栈的变化:(手绘步骤,顺序是从左到 ...
- sql replace
update dbo.EquipmentAttribute set AttributeName=replace(AttributeName,' ','') where EquipmentID=8 ...
- 在云服务器搭建WordPress博客(四)WordPress的基本设置
前面说了 如何安装WordPress,接下来我们需要快速熟悉WordPress,以及进行一些必要的基本设置. 开始设置之前,建议大家先点击一篇左边菜单栏的每一个选项,看看到底是做什么用的.下面开始说一 ...
- oracle中行转列函数
一.问题描述 有时在“相关子查询中”需要查询某个实体类对应的某个字段有多个值,如果不做行专列查询,会提示返回多个列的错误.例如: 如上图所示,一个组合包,可能对应多个产品,需要你将所对应的多个产品都放 ...
- 2014 Multi-University Training Contest 6
官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uz2v.html Apple Tree http://acm.hdu.edu.cn/showpro ...
- [C/CPP系列知识] Type difference of character literals 和 bool in C and C++
C/C+中的每一个常亮(every literal)都是有类型的,例如10 就是int型的,因此siziof(10)和sizeof(int)是相同的,但是字符型常亮(‘a’)在C和C++中有不同的变量 ...
- uva 1423 拓扑排序
刘书上例题 拓扑排序 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...
- JavaScript 函数参数是传值(byVal)还是传址(byRef)?
对于“JavaScript 函数参数是传值(byVal)还是传址(byRef)”这个问题,普遍存在一个误区:number,string等“简单类型”是传值,Number, String, Object ...
- 【C++基础】构造函数
说说你对构造函数的理解? 构造函数:对象创建时,利用特定的值构造对象(不是构造类),将对象初始化(保证数据成员有初始值),是类的一个public 函数 ① 与类同名 ② 无返回值 ③ 声明 ...