8175: MooTube

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:14 通过:7

题目描述

In his spare time, Farmer John has created a new video-sharing service, which he names MooTube. On MooTube, Farmer John's cows can record, share, and discover many amusing videos. His cows already have posted N videos (1≤N≤5000), conveniently numbered 1…N. However, FJ can't quite figure out how to help his cows find new videos they might like.

FJ wants to create a list of "suggested videos" for every MooTube video. This way, cows will be recommended the videos most relevant to the ones they already watch.

FJ devises a metric of "relevance," which determines, as the name suggests, how relevant two videos are to each other. He picks N−1 pairs of videos and manually computes their pairwise relevance. Then, FJ visualizes his videos as a network, where each video is a node and the N−1 pairs of videos he manually considered are connected. Conveniently, FJ has picked his N−1 pairs so that any video can be reached from any other video along a path of connections in exactly one way. FJ decides that the relevance of any pair of videos should be defined as the minimum relevance of any connection along this path.

Farmer John wants to pick a value K so that next to any given MooTube video, all other videos with relevance at least K to that video will be suggested. However, FJ is worried that too many videos will be suggested to his cows, which could distract them from milk production! Therefore, he wants to carefully set an appropriate value of K. Farmer John would like your help answering a number of questions about the suggested videos for certain values of K.

题目描述

在业余时间,Farmer John 创建了一个新的视频共享服务,他将其命名为 MooTube。在 MooTube 上,Farmer John 的奶牛可以录制,分享和发现许多有趣的视频。他的奶牛已经发布了 NN 个视频(1 \leq N \leq 50001N5000),为了方便将其编号为 1 \ldots N1N 。然而,FJ 无法弄清楚如何帮助他的奶牛找到他们可能喜欢的新视频。
FJ 希望为每个 MooTube 视频创建一个“推荐视频”列表。这样,奶牛将被推荐与他们已经观看过的视频最相关的视频。
FJ 设计了一个“相关性”度量标准,顾名思义,它确定了两个视频相互之间的相关性。他选择 N-1N1 对视频并手动计算其之间的相关性。然后,FJ 将他的视频建成一棵树,其中每个视频是节点,并且他手动将 N-1N1 对视频连接。为了方便,FJ 选择了 N-1N1 对,这样任意视频都可以通过一条连通路径到达任意其他视频。 FJ 决定将任意一对视频的相关性定义为沿此路径的任何连接的最小相关性。
Farmer John 想要选择一个 KK 值,以便在任何给定的 MooTube 视频旁边,推荐所有其他与该视频至少有 KK 相关的视频。然而,FJ 担心会向他的奶牛推荐太多的视频,这可能会分散他们对产奶的注意力!因此,他想设定适当的 KK 值。 Farmer John希望得到您的帮助,回答有关 KK 值的推荐视频的一些问题。

输入格式

第一行输入包含 NN 和 QQ1 \leq Q \leq 50001Q5000)。
接下来的 N-1N1 行描述了 FJ 手动比较的一对视频。 每行包括三个整数 p_ipiq_iqi 和 r_iri1 \leq p_i, q_i \leq N1pi,qiN1 \leq r_i \leq 10^91ri109),表示视频 p_ipi 和 q_iqi 已连接并且相关性为 r_iri
接下来的 QQ 行描述了 Farmer John 的 QQ 个问题。每行包含两个整数,k_iki 和 v_ivi1 \leq k_i \leq 10^91ki1091 \leq v_i \leq N1viN),表示 FJ 的第 ii 个问题询问中当 K = k_iK=ki 时,第 v_ivi 个视频推荐列表中将推荐的视频数。

输出格式

输出 QQ 行。在第 ii 行,输出 FJ 的第 ii 个问题的答案。

输入格式

The first line of input contains N and Q (1≤Q≤5000).

The next N−1 lines each describe a pair of videos FJ manually compares. Each line includes three integers pi, qi, and ri (1≤pi,qi≤N,1≤ri≤1,000,000,000), indicating that videos pi and qi are connected with relevance ri.

The next Q lines describe Farmer John's Q questions. Each line contains two integers, ki and vi (1≤ki≤1,000,000,000,1≤vi≤N), indicating that FJ's i th question asks how many videos will be suggested to viewers of video vi if K=ki.

输出格式

Output Q lines. On line i, output the answer to FJ's th question.

输入样例 复制

4 3
1 2 3
2 3 2
2 4 4
1 2
4 1
3 1

输出样例 复制

3
0
2