[蓝书] POJ1741 点分治例题/[luogu] P4178/[GMOJ] 1166. Tree

本文解析了一道关于计算给定树中边长不超过k的节点对数量的编程题,通过实例演示了如何利用最小距离概念和数据结构来求解。适合理解树形数据结构和动态规划的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相似题目:
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(1n104) 个点的树,每条边有边权 w(1≤w≤1000)w(1\le w\le 1000)w(1w1000)
求所有的 (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-1n1 行,每行描述树上的一条双向边。

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 ,但是打不动了。。。被我盯上的序列乱搞题目都基本不放它过,嘿嘿嘿。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值