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
1 | 5 4 |
Sample Output
1 | 8 |
Solution
模板,
和洛谷的模板差不多,不过luogu的是询问树上距离为 k 的点对是否存在(因为范围不大直接开桶暴力就好)。这道题是给一颗树,求树上距离 <=k 的点对有多少个。
每个点去统答案,然后去掉两端都在同一颗子树的情况(luogu那题可以直接标记该树属于哪个子树)
1 |
|