F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

After a Sleepless Night

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 627    Accepted Submission(s): 149


Problem Description
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.
 

Input
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.
 

Output
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.
 

Sample Input
2 2 2 2 1 2 3 3 1 2 1 2 2 3
 

Sample Output
Case #1: 1 2 Case #2: Impossible
 

Hint

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.

 

Author
FZU
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-04-25 10:14:12, Gzip enabled