A15819 | There Should Be a Lot of Maximums
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree (a connected graph without cycles). Each vertex of the tree contains an integer. Let's define the $\mathrm{MAD}$ (maximum double) parameter of the tree as the maximum integer that occurs in the vertices of the tree at least $2$ times. If no number occurs in the tree more than once, then we assume $\mathrm{MAD}=0$ .
Note that if you remove an edge from the tree, it splits into two trees. Let's compute the $\mathrm{MAD}$ parameters of the two trees and take the maximum of the two values. Let the result be the value of the deleted edge.
For each edge, find its value. Note that we don't actually delete any edges from the tree, the values are to be found independently.
Note that if you remove an edge from the tree, it splits into two trees. Let's compute the $\mathrm{MAD}$ parameters of the two trees and take the maximum of the two values. Let the result be the value of the deleted edge.
For each edge, find its value. Note that we don't actually delete any edges from the tree, the values are to be found independently.
输入格式
The first line contains one integer $n$ ( $2 \le n \le 10^5$ ) — the number of vertices in the tree.
Each of the next $n - 1$ lines contains two integers $u$ and $v$ ( $1 \le u, v \le n$ ) — the ends of an edge of the tree. It's guaranteed that the given edges form a valid tree.
The last line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 10^9$ ) — the numbers in the vertices.
Each of the next $n - 1$ lines contains two integers $u$ and $v$ ( $1 \le u, v \le n$ ) — the ends of an edge of the tree. It's guaranteed that the given edges form a valid tree.
The last line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 10^9$ ) — the numbers in the vertices.
输出格式
For each edge in the input order, print one number — the maximum of the $\mathrm{MAD}$ parameters of the two trees obtained after removing the given edge from the initial tree.
输入输出样例
输入 #1
5 1 2 2 3 2 4 1 5 2 1 3 2 1
输出 #1
0 2 1 2
输入 #2
6 1 2 1 3 1 4 4 5 4 6 1 2 3 1 4 5
输出 #2
1 1 0 1 1
In the first example, after removing edge $(1, 2)$ no number repeats $2$ times in any of the resulting subtrees, so the answer is $\max(0, 0)=0$ .
After removing edge $(2, 3)$ , in the bigger subtree, $1$ is repeated twice, and $2$ is repeated twice, so the $\mathrm{MAD}$ of this tree is $2$ .
After removing edge $(2, 4)$ , in the bigger subtree, only the number $1$ is repeated, and in the second subtree, only one number appears, so the answer is $1$ .
In the second example, if edge $1 \leftrightarrow 4$ is not removed, then one of the subtrees will have two $1$ , so the answer — $1$ . And if edge $1 \leftrightarrow 4$ is deleted, both subtrees have no repeating values, so the answer is $0$ .
After removing edge $(2, 3)$ , in the bigger subtree, $1$ is repeated twice, and $2$ is repeated twice, so the $\mathrm{MAD}$ of this tree is $2$ .
After removing edge $(2, 4)$ , in the bigger subtree, only the number $1$ is repeated, and in the second subtree, only one number appears, so the answer is $1$ .
In the second example, if edge $1 \leftrightarrow 4$ is not removed, then one of the subtrees will have two $1$ , so the answer — $1$ . And if edge $1 \leftrightarrow 4$ is deleted, both subtrees have no repeating values, so the answer is $0$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted