相似题目:
https://www.luogu.com.cn/problem/P4178
https://www.luogu.com.cn/problem/P2634
https://www.luogu.com.cn/problem/P3806
Tree
Time Limit: 1000MS Memory Limit: 30M
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.
n(1≤n≤104)n(1\le n\le 10^4)n(1≤n≤104) 个点的树,每条边有边权 w(1≤w≤1000)w(1\le w\le 1000)w(1≤w≤1000) 。
求所有的 (x,y)(x,y)(x,y) 点对满足 dist(x,y)≤kdist(x,y)\le kdist(x,y)≤k 的个数。
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.
多组数据。对于每组数据,第一行输入 n,kn,kn,k 。(如果 n=k=0n=k=0n=k=0 则结束输入)。
接下来 n−1n-1n−1 行,每行描述树上的一条双向边。
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
题解
https://blog.csdn.net/STJqwq/article/details/121263084
详解在这里。。。
一开始我想打洛谷 P3806 ,但是打不动了。。。被我盯上的序列乱搞题目都基本不放它过,嘿嘿嘿。