Description
Give a tree with n vertices,each edge has a length(positive integer less than 1001).
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.

Input
The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l.
The last test case is followed by two zeros.

Output
For each test case output the answer on a single line.

Sample Input

5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0

Sample Output

8

题目大意:求树上距离小于k的点对个数

裸题,树的点分治

 const
maxn=;
var
n,k,cut,cuts,ans,num,tot:longint;
next,last,w:array[..maxn*]of longint;
first,size,a:array[..maxn]of longint;
flag:array[..maxn]of boolean; function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end; procedure dfs1(x:longint);
var
i,s:longint;
begin
size[x]:=;
i:=first[x];
flag[x]:=false;
s:=;
while i<> do
begin
if flag[last[i]] then
begin
dfs1(last[i]);
inc(size[x],size[last[i]]);
s:=max(s,size[last[i]]);
end;
i:=next[i];
end;
if max(num-size[x],s)<cuts then
begin
cut:=x;
cuts:=max(num-size[x],s);
end;
flag[x]:=true;
end; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure sort(l,r:longint);
var
i,j,y:longint;
begin
i:=l;
j:=r;
y:=a[(l+r)>>];
repeat
while a[i]<y do
inc(i);
while a[j]>y do
dec(j);
if i<=j then
begin
swap(a[i],a[j]);
inc(i);
dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end; procedure dfs2(x,d:longint);
var
i:longint;
begin
inc(a[]);
a[a[]]:=d;
flag[x]:=false;
i:=first[x];
while i<> do
begin
if flag[last[i]] then dfs2(last[i],d+w[i]);
i:=next[i];
end;
flag[x]:=true;
end; function calc(x,d:longint):longint;
var
l,r:longint;
begin
calc:=;
a[]:=;
dfs2(x,d);
sort(,a[]);
l:=a[];
for r:= to a[] do
begin
while (a[l]+a[r]>k) and (l>) do
dec(l);
if l<r then inc(calc,l)
else inc(calc,r-);
end;
end; procedure work;
var
i:longint;
begin
inc(ans,calc(cut,));
flag[cut]:=false;
i:=first[cut];
while i<> do
begin
if flag[last[i]] then dec(ans,calc(last[i],w[i]));
i:=next[i];
end;
i:=first[cut];
while i<> do
begin
if flag[last[i]] then
begin
dfs1(last[i]);
num:=last[i];
cuts:=num;
cut:=last[i];
dfs1(last[i]);
work;
end;
i:=next[i];
end;
end; procedure insert(x,y,z:longint);
begin
inc(tot);
last[tot]:=y;
next[tot]:=first[x];
first[x]:=tot;
w[tot]:=z;
end; procedure init;
var
i,x,y,z:longint;
begin
read(n,k);
if n= then halt;
tot:=;
for i:= to n do
begin
first[i]:=;
flag[i]:=true;
end;
ans:=;
for i:= to n- do
begin
read(x,y,z);
insert(x,y,z);
insert(y,x,z);
end;
cut:=;
cuts:=n;
dfs1();
work;
writeln(ans);
end; begin
while true do
init;
end.

POJ - 1741 Tree的更多相关文章

  1. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

  2. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  3. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  4. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

  5. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  6. poj 1741 Tree(点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15548   Accepted: 5054 Description ...

  7. ●POJ 1741 Tree

    题链: http://poj.org/problem?id=1741题解: 树上点分治. 入门题,不多说了. 代码: #include<cstdio> #include<cstrin ...

  8. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  9. POJ 1741 Tree (树分治入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8554   Accepted: 2545 Description ...

  10. POJ 1741 Tree (点分治)

                                                                        Tree Time Limit: 1000MS   Memory ...

随机推荐

  1. html,css所遇问题(一)

    html,css所遇问题(一) div中添加背景图片必须设置宽高 例如:下述代码没有设置icon的宽高值,那么网页中也不会显示出背景图片,因为div 里面有内容才会出现背景,没内容又没设置宽高,那di ...

  2. memcached和redis的区别和应用场景

    一:特性和对比 1.性能上:      性能上都很出色,具体到细节,由于Redis只使用单核,而Memcached可以使用多核,所以平均每一个核上Redis在存储小数据时比 Memcached性能更高 ...

  3. Linux 命令 - kill: 向进程发送信号

    kill 命令通常用来 “杀死”(终止)进程,它可以用来终止运行不正常的程序或者反过来拒绝终止的程序.kill 命令准确地说并不是 “杀死” 进程,而是给进程发送信号(signal). 和文件一样,进 ...

  4. C# 利用ffmpeg 对视频转换系类操作 (1) 基本分析

    最近公司做一个项目,开发一个视频站点.项目需求中有很多视频转换的需求,如:格式转换(flv,Mp4),视频水印,视频截图,视频合成,获取视频的基本信息(时间戳,视频大小等).经过网络的收集资料以及自己 ...

  5. 一个简单的Makefile的编写【用自己的话,解释清楚这些】

    用自己的话,解释清楚这些~ Makefile是程序员编写出来指导编译器编译程序源码为目标文件(可执行文件,或链接库) 这里只写一个简单的Makefile 作为例子 其需求如下: frank@ubunt ...

  6. Cocos2d-x优化中关于背景图片优化

    由于背景图片长时间在场景中保存,而且图片很多,我们可以对其进行一些优化.我们通过如下几个方面考虑优化:1.不要Alpha通道背景图片的特点是不需要透明的,所以纹理格式可以采用不带有Alpha通道格式, ...

  7. 相似度到大数据查找之Mysql 文章匹配的一些思路与提高查询速度

    文章相关度匹配的一些思路---"压缩"预料库,即提取用特征词或词频,量化后以“列向量”形式保存到数据库:按前N组词拼为向量组供查询使用,即组合为1到N字的组合,量化后以“行向量”形 ...

  8. c# DateTime时间格式和JAVA时间戳格式相互转换

    /// java时间戳格式时间戳转为C#格式时间 public static DateTime GetTime(long timeStamp) { DateTime dtStart = TimeZon ...

  9. 手机APP与原生APP设计的区别

    交互上可以按照原生App的设计方式,效果将越来越接近,主要区别在于: 1.设计中要考虑到浏览器地址栏和工具栏的占有空间,和其对App的操作存在一定的影响. 2.暂时不适合调用系统底层接口,更适合web ...

  10. What is a First Chance Exception?

    Refrences: http://blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx To be continued...