Time Limit: 1000MS | | Memory Limit: 30000K |
Total Submissions: 18205 | | Accepted: 5951 |
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 41 2 31 3 11 4 23 5 10 0
Sample Output
8
/*poj 1741 树的点分治(入门)problem:给一棵边带权树,问两点之间的距离小于等于K的点对有多少个solve: 随便找的博客学习下大致思路,对当前树先求其重心为根节点,然后找出所有点到根节点的距离. 然后就能计算出有多少的的和<= k. 但是这两个点有可能来自同一个子树,所以在后面计算中减去,就能计算出过当前根节点的所有对.以同样的方法计算子树的情况,递推下去就行.重心是为了让最大子树的节点数最小,从而增加效率.(个人理解)hhh-2016-08-22 20:00:18*/#pragma comment(linker,"/STACK:124000000,124000000")#include #include #include #include #include #include #include #include