Under construction

Still being built. Things may change

Potion Farming

Time limit2sMemory limit1024 MB

Summary
Given a tree rooted at room 1 and a schedule of potion spawns, find the most potions collectible while covering every room with the fewest root-to-room paths.
Level

Medium7 of 10

Topics
Tree, Greedy, DFS
Solved
No attempts yet

Problem

You are playing your favorite mobile game and trying to farm potions so that you may have a chance at defeating the legendary cow boss. The game map is a series of NN (2≤N≤105)(2 \leq N \leq 10^5) rooms labeled 1…N1\dots N, connected by N−1N-1 edges that form a tree.

You can explore the map by making a series of "traversals". A traversal is a simple path from room 11 to any other room in the tree. Once you finish one traversal, you can start another traversal from room 11. The map is complete once every room has been visited by at least one traversal. Your main goal is to complete the map in the minimum number of traversals.

Your secondary goal is to farm as many potions as possible. Before a traversal begins, a potion spawns at some room in the map. You can pick up the potion by visiting that room during the current traversal. If you do not pick up the potion, it disappears when the current traversal ends, so you cannot pick it up in later traversals.

You have studied the game files and know where the potions will appear before your next NN traversals. If you complete the map in the minimum number of traversals, what is the maximum number of potions you can farm from the map?

Input

The first line contains an integer NN, the number of rooms in the map.

The next line contains NN space-separated integers p1 p2 … pNp_1 \: p_2 \: \ldots \: p_N where 1≤pi≤N1 \leq p_i \leq N, and pip_i is the room where a potion appears before the iith traversal.

The last N−1N-1 lines each contain two space-separated integers a ba \: b (1≤a,b≤N)(1 \leq a, b \leq N), an edge between rooms aa and bb. These edges form a tree.

Output

Output one line containing a single integer, the maximum number of potions you can farm from the map in the minimum number of traversals.

Examples1

  1. Example 1

    Input
    5
    5 4 3 2 1
    1 2
    1 3
    3 4
    3 5
    
    Expected output
    2