bzoj1758

时间:2023-03-09 06:19:08
bzoj1758

好题
显然是分数规划,二分答案之后我们要找是否存在一条边数在[l,u]长度和为正的路径
可以用树的分治来解决这个问题
我们假设当前处理的是过点root的路径
显然我们不好像之前男人八题里先算出所有答案,然后再剔除不合法的
这里我们的统计方法是依次处理每个子树,算这个子树中的从根到某个节点的路径和之前处理的子树的路径能产生的最大值
这样就能保证路径过root且不重不漏
然后不难想到维护w[s]表示处理到当前子树前边数为s的路径长度和的最大值,处理完当前子树更新w[]
考虑能产生的最大合法路径我们bfs当前子树,由于从子树根到节点的边数是逐渐增加的
不难想到维护一个单调队列来解决,具体维护见程序
由于这道题时限比较紧,如果我们先二分然后用树分治判定是否合法,非常耗时
思考一下我们的流程,我们找到树的重心后要处理过重心的路径
我们完全可以在这时内二分得出过重心的最优值然后向下分治时,这个最优值就成了以后的下界
这样不断提高下界可以快不少
还有一些优化细节见程序

 const eps=0.0001;
type node=record
po,next,len:longint;
end; var e:array[..] of node;
fa,dep,p,f,s,q,qq:array[..] of longint;
d,w,g:array[..] of double;
v:array[..] of boolean;
md,tot,n,root,i,l,u,len,x,y,z:longint;
h,r,m,ans,lim:double; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; function maxx(a,b:double):double;
begin
if a>b then exit(a) else exit(b);
end; procedure add(x,y,z:longint);
begin
inc(len);
e[len].po:=y;
e[len].len:=z;
e[len].next:=p[x];
p[x]:=len;
end; procedure getroot(x,fa:longint); //找重心
var i,y:longint;
begin
i:=p[x];
f[x]:=;
s[x]:=;
while i<> do
begin
y:=e[i].po;
if not v[y] and (y<>fa) then
begin
getroot(y,x);
s[x]:=s[x]+s[y];
f[x]:=max(f[x],s[y]);
end;
i:=e[i].next;
end;
f[x]:=max(f[x],tot-s[x]);
if f[x]<f[root] then root:=x;
end; function calc(x:longint):boolean;
var f,r,i,y,j,h,t:longint;
begin
f:=; r:=; q[]:=x;
dep[x]:=;
fa[x]:=;
h:=; t:=;
j:=md; //这个优化使数据某个点快了十倍,md表示之前处理的子树最深的链的深度
while f<=r do
begin
x:=q[f];
g[dep[x]]:=maxx(g[dep[x]],d[x]);
while (j>=) and (j+dep[x]>=l) do //倒序入队,保证满足路径边数下界,维护单调降队列
begin
while (h<=t) and (w[j]>w[qq[t]]) do dec(t);
inc(t);
qq[t]:=j;
dec(j);
end;
while (h<=t) and (qq[h]+dep[x]>u) do inc(h); //端头出队满足上界
if (h<=t) and (w[qq[h]]+d[x]>=) then
begin
md:=max(md,dep[x]);
exit(true);
end;
if dep[x]<u then
begin
i:=p[x];
while i<> do
begin
y:=e[i].po;
if (fa[x]<>y) and not v[y] then
begin
fa[y]:=x;
dep[y]:=dep[x]+;
d[y]:=d[x]+e[i].len-m;
inc(r);
q[r]:=y;
end;
i:=e[i].next;
end;
end;
inc(f);
end;
md:=max(md,dep[x]);
for i:= to dep[x] do
w[i]:=maxx(w[i],g[i]);
exit(false);
end; function check(x:longint):boolean;
var i,y:longint;
begin
md:=;
check:=false;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if not v[y] then
begin
d[y]:=e[i].len-m;
if calc(y) then
begin
check:=true;
break;
end;
end;
i:=e[i].next;
end;
for i:= to md do
begin
w[i]:=-;
g[i]:=-;
end;
end; procedure work(x:longint);
var i,y:longint;
begin
v[x]:=true;
h:=ans;
r:=lim;
while r-h>eps do
begin
m:=(h+r)/;
if check(x) then
begin
ans:=m;
h:=m;
end
else r:=m;
end;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if not v[y] then
begin
tot:=s[y];
root:=;
getroot(y,);
if s[y]>l then work(root);
end;
i:=e[i].next;
end;
end; begin
f[]:=;
readln(n);
readln(l,u);
for i:= to n- do
begin
readln(x,y,z);
add(x,y,z);
add(y,x,z);
if lim<z then lim:=z;
end;
for i:= to u do
begin
w[i]:=-;
g[i]:=-;
end;
root:=;
tot:=n;
getroot(,);
work(root);
writeln(ans::);
end.