Fringe Benefits I
The Adelaide Fringe is the largest arts festival in the Southern Hemisphere — a whirlwind of creativity that draws in performers, tourists, and families alike. But with so much going on, it's infamously tricky to market effectively. Many have tried. Most have failed. Now, as the Fringe's newest marketing hire, it's your time to shine, and all eyes are on you.
Adelaide can be modeled as a tree of parks, numbered from
to
, connected by
roads. Victoria Square, the beating heart of the city, is park
. Each park already has a Fringe poster, marked with some initial "swag value", but you know they could be so much cooler.
You begin your journey at park . Each day, your supervisor tells you to walk from here to park
, admiring every poster you pass. Then, using the inspiration from your walk, you must design a brand new poster. This poster will have a swag value equal to the maximum swag value you saw along the path, plus one (thanks to your creative genius). You'll then use this new poster to replace every poster along the walk from park
back to park
. Finally, you must report the swag value of your new poster design. The next day, your starting point is park
.
Can you carry Adelaide's advertising to unimaginable levels of swag?
Input
The first line contains an integer (
), the number of parks.
The next line contains integers
(
), representing that the
th park initially has a poster with a swag value of
.
The following lines each contain two integers
and
(
), indicating a bidirectional path between parks
and
. It is guaranteed that the graph forms a tree.
The next line contains an integer (
), the number of days you are working.
The next line contains integers
(
), representing that on the
th day, you must walk from park
to park
and update the posters along that path.
Output
Print lines. Each line should contain a single integer, the new swag value assigned to all posters along the path from park
to park
on that day.
Example
Input 1
10
2 5 4 3 1 7 2 4 10 3
1 2
1 7
2 3
2 4
2 5
4 6
7 8
7 9
8 10
3
10 4 9
Output 1
5
6
11
This is the initial state of the parks, including their posters and connections.
On day 1, walk from park to park
. The maximum "swag value" on this path is
.
Therefore, we make all posters on this path have a swag value of .
On day 2, walk from park to park
. The maximum "swag value" on this path is
.
Therefore, we make all posters on this path have a swag value of .
On day 3, walk from park to park
.
The maximum "swag value" on this path is .Therefore, we make all posters on this path have a swag value of
.
Input 2
4
1 1 5 1
2 1
3 1
4 1
4
2 3 4 2
Output 2
2
6
7
8
Comments