CF1805E.There Should Be a Lot of Maximums
提高+/省选-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree (a connected graph without cycles). Each vertex of the tree contains an integer. Let's define the 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 MAD=0.
Note that if you remove an edge from the tree, it splits into two trees. Let's compute the 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.
给你一棵树(即一个无环的连通图)。树的每个顶点上有一个整数。我们定义该树的 MAD(最大重复值)参数为:在树的所有顶点中至少出现两次的最大整数。若树中所有整数均至多出现一次,则定义 MAD=0。
注意:若从树中删除一条边,树将分裂成两棵子树。我们分别计算这两棵子树的 MAD 参数,并取二者中的较大值。该较大值即为被删边的“值”。
对树中的每一条边,求出其对应的“值”。注意:我们并不实际从树中删除任何边,所有边的值均需独立计算。
输入格式
The first line contains one integer n (2≤n≤105) — the number of vertices in the tree.
Each of the next n−1 lines contains two integers u and v (1≤u,v≤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 a1,a2,…,an (1≤ai≤109) — the numbers in the vertices.
第一行包含一个整数 n(2≤n≤105)—— 树中顶点的数量。
接下来的 n−1 行每行包含两个整数 u 和 v(1≤u,v≤n)—— 树的一条边的两个端点。保证所给的边构成一棵合法的树。
最后一行包含 n 个整数 a1,a2,…,an(1≤ai≤109)—— 各顶点上的数值。
输出格式
For each edge in the input order, print one number — the maximum of the MAD parameters of the two trees obtained after removing the given edge from the initial tree.
对于输入中每条边,输出一个数字——即从初始树中移除该边后所得的两棵树各自的 MAD 参数的最大值。
输入输出样例
输入#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 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↔4 is not removed, then one of the subtrees will have two 1, so the answer — 1. And if edge 1↔4 is deleted, both subtrees have no repeating values, so the answer is 0.

在第一个样例中,删除边 (1,2) 后,任意一个生成的子树中均无数字出现恰好 2 次,因此答案为 max(0,0)=0。
删除边 (2,3) 后,在较大的子树中,数字 1 出现了两次,数字 2 也出现了两次,因此该子树的 MAD 为 2。
删除边 (2,4) 后,在较大的子树中仅有数字 1 出现重复;而在另一个子树中,仅有一个数字出现,因此答案为 1。
在第二个样例中,若不删除边 1↔4,则其中一个子树将包含两个 1,因此答案为 1;而若删除边 1↔4,则两个子树中均无重复数值,因此答案为 0。
输入解题思路,AI测评打分。不知道怎么写?