#658
Unrated
Potion Farming
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

You are playing your favorite mobile game and you are 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 (2N105)(2 \leq N \leq 10^5) rooms labeled 1N1\dots N connected by N1N-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 one of its rooms is 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 will spawn at some room in the map. You can pick up the potion by visiting the room that the potion spawned at in the current traversal. If you do not pick up the potion, then it will disappear once the current traversal ends, so you cannot pick it up in future traversals.

As you are a smart programmer, after looking at the game files, you were able to figure out 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 amount of potions that you can farm from the map?

입력

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

Then follow NN space-separated integers p1p2pNp_1 \: p_2 \: \ldots \: p_N where 1piN1 \leq p_i \leq N, where pip_i is the room that a potion will appear at before the iith traversal.

Finally, N1N-1 lines follow with two space-separated integers aba \: b (1a,bN)(1 \leq a, b \leq N) representing an edge between rooms aa and bb. It is guaranteed that these edges form a tree.

출력

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

예제 입력 1

5
5 4 3 2 1
1 2
1 3
3 4
3 5

예제 출력 1

2

점수

Inputs 2-7: N1000N\le 1000Inputs 8-15: No additional constraints.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.