当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

5764:After a Sleepless Night

题目描述
The yearly Multi-University Training Contest is coming, and ?? had already prepared an interesting and challenging problem for all those intelligent brains. However, when he told teammates about his “wonderful idea”, it really shocked him that all of them, except himself, had already seen an existing problem with the same trick.So he need to rack his brain again.
But nothing is difficult for a willing heart. After thinking about it all through the night, he came up with a new problem:
The original rooted tree has n nodes, numbered from 1 to n. Each node has a value, all of these values form a permutation of [1,n]. We reset these values by the following way:

new value(P) = max{previous value(Q)|Q is in the subtree with root P}

Given the new tree(each node’s number, it’s neighbors and it’s value), your job is to restore its original appearance.
Note that the root of the given tree is unspecified.
输入解释
First line gives the number of test cases T.
Each cases begins with n(the number of nodes).Next line gives n integers, the i’th integers means i-th node’s value. Then n-1 lines, each line has two integers a and b, describes the edge connecting node a and node b..
T<=20, 1<=n<=100000. All node’s values are in range [1,n]. The given edges construct a tree of nodes 1 to n.Sum of n in all cases<=200000.
There may be blank lines before each case.
输出解释
For each test case, first output "Case #x: ".If it’s impossible to restore the original tree, output “Impossible”. Otherwise, output original values of nodes 1,2,...n in order, separated by spaces.If there exist multiple answers, the lexicographically smallest one is needed.
输入样例
2

2
2 2
1 2

3
3 1 2
1 2
2 3
输出样例
Case #1: 1 2
Case #2: Impossible

提示
In first case, both nodes can be chosen as the root. If we choose node 1, its value must be 1, and node 2 have value 2. So the answer is ”1 2”.
In the second, by enumerating all situations, we can easily find that there is no proper original tree.

来自杭电HDUOJ的附加信息
Author FZU
Recommend wange2014

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-5764

最后修改于 2020-10-25T23:25:27+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
2000/1000MS(Java/Others) 65536/65536K(Java/Others)