All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef is playing a video game. In a video game, there's a advanced civilization that has a total of N planets under control. All of those planets are connected with N-1 teleports in such a way, that it's possible to travel between any two planets using those teleports.
There's a chance that some planet gets infected. In this case it takes 24 hours for civilization to find out infection and prevent it from spreading. During this time infection uses teleport one time and infect all the planets that can be achieved in one teleport jump. So, once infection is detected at planet V, scientists already know that all planets connected to V via teleport are also infected. All the neccessary teleports are disabled right away and medics start working on eliminating the infection.
Each planet has population. Planets are numbered from 1 to N and their populations are P1, P2, ..., PN. It is known that all the Pi are distinct.
There's a capital among all those planets. The capital is known to have the biggest population.
Once infection is detected at planet V, after disabling teleports on planet V and all connected to them, government has to establish a new capital if needed in the remaining not-infected planets. So, they list all the planets that are not connected to V and are not V. Then they pick the planet with biggest population. Your task is to find the number of this planet for every possible V.
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains one integer N.
Next line contains N space-separated integers P1, P2, ..., PN denoting the population of each planet.
Next N-1 lines contain two space-separated integers each V and U denoting that there's a teleport between planet V and U.
For each test case, output a single line containing N integers A1, A2, ..., AN separated by a space. Here Ai denotes the number of the planet picked to be new capital in case infection starts spreading from the planet i. In case infection affects all the planets output 0.
- 1 ≤ T ≤ 5
- 1 ≤ N ≤ 50000
- 1 ≤ Pi ≤ 109
- All Pi are distinct
- 1 ≤ V ≤ N
- 1 ≤ U ≤ N
- Subtask #1 (20 points): N ≤ 100
- Subtask #2 (30 points): N ≤ 10000
- Subtask #3 (50 points): No additional constraints
Input: 1 6 5 10 15 20 25 30 1 3 2 3 3 4 4 5 4 6 Output: 6 6 6 2 6 5
|Tags||cenadar, data-structure, heap, jan17, sets, simple, sorting, tree|
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.