A811 | New Barns--Platinum
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Farmer John notices that his cows tend to get into arguments if they are
packed too closely together, so he wants to open a series of new barns to help
spread them out.
Whenever FJ constructs a new barn, he connects it with at most one
bidirectional pathway to an existing barn. In order to make sure his cows are
spread sufficiently far apart, he sometimes wants to determine the distance
from a certain barn to the farthest possible barn reachable from it (the
distance between two barns is the number of paths one must traverse to go from
one barn to the other).
FJ will give a total of $Q$ ($1 \leq Q \leq 10^5$) queries, each either of the
form "build" or "distance". For a build query, FJ builds a barn and links it
with at most one previously built barn. For a distance query, FJ asks you the
distance from a certain barn to the farthest barn reachable from it via a
series of pathways. It is guaranteed that the queried barn has already been
built. Please help FJ answer all of these queries.
packed too closely together, so he wants to open a series of new barns to help
spread them out.
Whenever FJ constructs a new barn, he connects it with at most one
bidirectional pathway to an existing barn. In order to make sure his cows are
spread sufficiently far apart, he sometimes wants to determine the distance
from a certain barn to the farthest possible barn reachable from it (the
distance between two barns is the number of paths one must traverse to go from
one barn to the other).
FJ will give a total of $Q$ ($1 \leq Q \leq 10^5$) queries, each either of the
form "build" or "distance". For a build query, FJ builds a barn and links it
with at most one previously built barn. For a distance query, FJ asks you the
distance from a certain barn to the farthest barn reachable from it via a
series of pathways. It is guaranteed that the queried barn has already been
built. Please help FJ answer all of these queries.
输入格式
The first line contains the integer $Q$. Each of the next $Q$ lines contains a
query. Each query is of the form "B p" or "Q k", respectively telling you to
build a barn and connect it with barn $p$, or give the farthest distance, as
defined, from barn $k$. If $p = -1$, then the new barn will be connected to no
other barn. Otherwise, $p$ is the index of a barn that has already been built.
The barn indices start from $1$, so the first barn built is barn $1$, the
second is barn $2$, and so on.
query. Each query is of the form "B p" or "Q k", respectively telling you to
build a barn and connect it with barn $p$, or give the farthest distance, as
defined, from barn $k$. If $p = -1$, then the new barn will be connected to no
other barn. Otherwise, $p$ is the index of a barn that has already been built.
The barn indices start from $1$, so the first barn built is barn $1$, the
second is barn $2$, and so on.
输出格式
Please write one line of output for each distance query. Note that a barn
which is connected to no other barns has farthest distance $0$.
which is connected to no other barns has farthest distance $0$.
输入输出样例
输入 #1
7 B -1 Q 1 B 1 B 2 Q 3 B 2 Q 2
输出 #1
0 2
1
The example input corresponds to this network of barns:
(1)
\
(2)---(4)
/
(3)
In query 1, we build barn number 1. In query 2, we ask for the distance of 1
to the farthest connected barn. Since barn 1 is connected to no other barns,
the answer is 0. In query 3, we build barn number 2 and connect it to barn 1.
In query 4, we build barn number 3 and connect it to barn 2. In query 5, we
ask for the distance of 3 to the farthest connected barn. In this case, the
farthest is barn 1, which is 2 units away. In query 6, we build barn number 4
and connect it to barn 2. In query 7, we ask for the distance of 2 to the
farthest connected barn. All three barns 1, 3, 4 are the same distance away,
which is 1, so this is our answer.
The example input corresponds to this network of barns:
(1)
\
(2)---(4)
/
(3)
In query 1, we build barn number 1. In query 2, we ask for the distance of 1
to the farthest connected barn. Since barn 1 is connected to no other barns,
the answer is 0. In query 3, we build barn number 2 and connect it to barn 1.
In query 4, we build barn number 3 and connect it to barn 2. In query 5, we
ask for the distance of 3 to the farthest connected barn. In this case, the
farthest is barn 1, which is 2 units away. In query 6, we build barn number 4
and connect it to barn 2. In query 7, we ask for the distance of 2 to the
farthest connected barn. All three barns 1, 3, 4 are the same distance away,
which is 1, so this is our answer.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted