A820 | MooTube--Silver
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
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 \leq
N \leq 5000$), conveniently numbered $1 \ldots 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$.
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 \leq
N \leq 5000$), conveniently numbered $1 \ldots 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$.
输入格式
The first line of input contains $N$ and $Q$ ($1 \leq Q \leq 5000$).
The next $N-1$ lines each describe a pair of videos FJ manually compares. Each
line includes three integers $p_i$, $q_i$, and $r_i$ ($1 \leq p_i, q_i \leq N,
1 \leq r_i \leq 1,000,000,000$), indicating that videos $p_i$ and $q_i$ are
connected with relevance $r_i$.
The next $Q$ lines describe Farmer John's $Q$ questions. Each line contains
two integers, $k_i$ and $v_i$ ($1 \leq k_i \leq 1,000,000,000, 1 \leq v_i \leq
N$), indicating that FJ's $i$th question asks how many videos will be
suggested to viewers of video $v_i$ if $K = k_i$.
The next $N-1$ lines each describe a pair of videos FJ manually compares. Each
line includes three integers $p_i$, $q_i$, and $r_i$ ($1 \leq p_i, q_i \leq N,
1 \leq r_i \leq 1,000,000,000$), indicating that videos $p_i$ and $q_i$ are
connected with relevance $r_i$.
The next $Q$ lines describe Farmer John's $Q$ questions. Each line contains
two integers, $k_i$ and $v_i$ ($1 \leq k_i \leq 1,000,000,000, 1 \leq v_i \leq
N$), indicating that FJ's $i$th question asks how many videos will be
suggested to viewers of video $v_i$ if $K = k_i$.
输出格式
Output $Q$ lines. On line $i$, output the answer to FJ's $i$th question.
输入输出样例
输入 #1
4 3 1 2 3 2 3 2 2 4 4 1 2 4 1 3 1
输出 #1
3 0 2
Farmer John finds that videos one and two have relevance three, that videos
two and three have relevance two, and that videos two and four have relevance
four. Based on this, videos one and three have relevance min(3, 2) = 2, videos
one and four have relevance min(3, 4) = 3, and videos three and four have
relevance min(2, 4) = 2.
Farmer John wants to know how many videos will be suggested from video two if
$K=1$, from video one if $K=3$, and from video one if $K=4$. We see that with
$K=1$, videos 1, 3, and 4 will be suggested on video two. With $K=4$, no
videos will be suggested from video one. With $K=3$, however, videos 2 and 4
will be suggested from video one.
two and three have relevance two, and that videos two and four have relevance
four. Based on this, videos one and three have relevance min(3, 2) = 2, videos
one and four have relevance min(3, 4) = 3, and videos three and four have
relevance min(2, 4) = 2.
Farmer John wants to know how many videos will be suggested from video two if
$K=1$, from video one if $K=3$, and from video one if $K=4$. We see that with
$K=1$, videos 1, 3, and 4 will be suggested on video two. With $K=4$, no
videos will be suggested from video one. With $K=3$, however, videos 2 and 4
will be suggested from video one.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted