双倍经验题
像这种方案太多不能全部求出来但求前k大一般有这样一个思路
将所有方案无重复不漏的分为若干类,每个类的元素满足单调性,然后我们用堆维护就行了!
对于这道题,可以想到用树的分治来处理路径,当处理根为x时
我们处理当前的子树每个点i和之前的子树上的组成的路径,
我们完全可以把访问顺序状态记录成一条链(状态是nlogn规模不会爆)
当前子树的每个点i在链上都有一段连续的可选区间,这个区间的点和i构成一条过根x的路径
这样链上的每个点就代表了一类路径
很明显,我们就可以noi超级钢琴的做法来解决,st表预处理+堆维护

 type way=record
po,num,next:longint;
end;
node=record
l,r,s,t:longint;
end; var e:array[..] of way;
h:array[..] of node;
w:array[..,..] of longint;
d:array[..] of longint;
a:array[..] of longint;
q:array[..,..] of longint;
f,p,s:array[..] of longint;
v:array[..] of boolean;
l,r,root,len,t,tot,i,n,k,x,y,z,j:longint;
m:node; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; function mx(x,y:longint):longint;
begin
if a[x]>a[y] then exit(x) else exit(y);
end; function ask(x,y:longint):longint;
var k:longint;
begin
k:=trunc(ln(y-x+)/ln());
exit(mx(w[x,k],w[y-d[k]+,k]));
end; procedure swap(var a,b:node);
var c:node;
begin
c:=a;
a:=b;
b:=c;
end; procedure add(x,y,z:longint);
begin
inc(len);
e[len].po:=y;
e[len].num:=z;
e[len].next:=p[x];
p[x]:=len;
end; procedure up(i:longint);
var j:longint;
begin
j:=i shr ;
while j> do
begin
if a[h[j].s]+a[h[j].t]<a[h[i].s]+a[h[i].t] then
begin
swap(h[i],h[j]);
i:=j;
j:=j shr ;
end
else break;
end;
end; procedure sift(i:longint);
var j:longint;
begin
j:=i shl ;
while j<=t do
begin
if (j<t) and (a[h[j+].s]+a[h[j+].t]>a[h[j].s]+a[h[j].t]) then inc(j);
if a[h[j].s]+a[h[j].t]>a[h[i].s]+a[h[i].t] then
begin
swap(h[i],h[j]);
i:=j;
j:=j shl ;
end
else break;
end;
end; procedure getroot(x,fa:longint);
var i,y:longint;
begin
s[x]:=;
f[x]:=;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if (y<>fa) and not v[y] 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; procedure get(x,fa,len:longint);
var i,y:longint;
begin
inc(t);
a[t]:=len;
q[t,]:=l; q[t,]:=r;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if (y<>fa) and not v[y] then get(y,x,len+e[i].num);
i:=e[i].next;
end;
end; procedure work(x:longint);
var i,y:longint;
begin
v[x]:=true;
i:=p[x];
inc(t);
l:=t;
r:=t;
q[t,]:=l; q[t,]:=r;
while i<> do
begin
y:=e[i].po;
if not v[y] then
begin
r:=t;
get(y,x,e[i].num);
end;
i:=e[i].next;
end;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if not v[y] then
begin
root:=;
tot:=s[y];
getroot(y,);
work(root);
end;
i:=e[i].next;
end;
end; begin
readln(n,k);
for i:= to n- do
begin
readln(x,y,z);
add(x,y,z);
add(y,x,z);
end;
f[]:=;
tot:=n;
getroot(,);
work(root);
tot:=trunc(ln(t)/ln());
d[]:=;
for i:= to tot do
d[i]:=d[i-]*; for i:= to t do
w[i,]:=i;
for j:= to tot do
for i:= to t do
if i+d[j]-<=t then
w[i,j]:=mx(w[i,j-],w[i+d[j-],j-])
else break; tot:=t; t:=;
for i:= to tot do
begin
inc(t);
with h[t] do
begin
l:=q[i,]; r:=q[i,];
s:=i;
t:=ask(l,r);
end;
up(t);
end;
for i:= to k do
begin
writeln(a[h[].s]+a[h[].t]);
swap(h[],h[t]);
m:=h[t];
dec(t);
sift();
if m.t>m.l then
begin
inc(t);
with h[t] do
begin
l:=m.l; r:=m.t-;
s:=m.s;
t:=ask(l,r);
end;
up(t);
end;
if m.t<m.r then
begin
inc(t);
with h[t] do
begin
l:=m.t+; r:=m.r;
s:=m.s;
t:=ask(l,r);
end;
up(t);
end;
end;
end.

 

bzoj1267 3784的更多相关文章

  1. BZOJ 3784: 树上的路径

    Description 问一棵树上前 \(k\) 大路径的边权. Sol 边分治. 非常感谢数据没有菊花图. 为了写写边分治试试然后就开了这道题. 边分治非常好想,选一条重边,分成两部分,然后分别求最 ...

  2. bzoj 3784: 树上的路径 堆维护第k大

    3784: 树上的路径 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 27[Submit][Status][Discuss] ...

  3. POJ 3784.Running Median

    2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...

  4. bzoj 3784

    第三道点分治. 首先找到黄学长的题解,他叫我参考XXX的题解,但已经没有了,然后找到另一个博客的简略题解,没看懂,最后看了一个晚上黄学长代码,写出来然后,写暴力都拍了小数据,但居然超时,....然后改 ...

  5. HDU 3784 继续xxx定律 & HDU 2578 Dating with girls(1)

    HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律  对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...

  6. 【POJ 3784】 Running Median

    [题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...

  7. POJ 3784 Running Median (动态中位数)

    题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...

  8. POJ 3784 Running Median【维护动态中位数】

    Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...

  9. Running Median POJ - 3784 (对顶堆/优先队列 | 链表)

    For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...

随机推荐

  1. CSS3的几个标签速记1

    border-radius:CSS3圆角   语法:border-radius:25px;     椭圆边角:语法-border-radius:xx%;或者15px/100px; box-shadow ...

  2. Java实战之04JavaWeb-07Listener和Filter

    一.监听器Listener javaEE的13们规范中 包括servlet技术和jsp技术 servlet规范中包括三门技术:(servlet的三大组件) servelt技术  Listener技术 ...

  3. win/linux 下使用 psutil 获取进程 CPU / memory / IO 占用信息

    psutil - A cross-platform process and system utilities module for Python 1. 安装 pip 安装即可. windows 下需要 ...

  4. Boot Petalinux Project Using a remote system

    通过jtag实现在远程服务器端下载petalinux image到连接在本地PC的开发板上的方法. 具体连接方式为 比如Host的系统为Windows,Remote system为运载在远程服务器上的 ...

  5. windows10和ubuntu16.04双系统下时间不对的问题

    最近装了windows10和ubuntu16.04双系统,仍然出现了喜闻乐见的老问题,装完后,在windows下时区不对,之前的老办法是: sudo gedit /etc/default/rcS ut ...

  6. Newtonsoft.Json.dll反序列化JSON字符串的方法

      1.直接反序列化JSON字符串 //引用序列化.反序列化JSON字符串用到的空间 using Newtonsoft.Json; using Newtonsoft.Json.Linq; //定义一个 ...

  7. C#基础(五)——类中私有构造函数作用

    如果类成员有private修饰符,就不允许在类范围以外访问这个类成员.对类构造函数应用private修饰符时,则禁止外部类创建该类的实例.尽管看上去有些不好理解(既然不能实例化,那么这个类还有什么用处 ...

  8. text与button上下不对齐解决方法

    火狐可以对齐,但是IE8不行,加上浮动就可以了 .search_right input[type=button] { float:right; }

  9. 简单重置Centos服务器中Mysql的root密码

    1.编辑MySQL配置文件my.cnf vi /etc/my.cnf #编辑文件,找到[mysqld],在下面添加一行skip-grant-tables [mysqld] skip-grant-tab ...

  10. 原生js在IE7下 向dom添加节点的一个bug, (本例为添加hidden input)

    需求是要用js向dom结构增加1个hidden用来存放要post到服务器的数据 var aspnetForm = document.getElementById("aspnetForm&qu ...