Answered step by step
Verified Expert Solution
Question
1 Approved Answer
USACO # 2 You are playing your favorite mobile game and you are trying to farm potions so that you may have a chance at
USACO # 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 N N rooms labeled N connected by N edges that form a tree.
You can explore the map by making a series of traversals A traversal is a simple path from room to any other room in the tree. Once you finish one traversal, you can start another traversal from room 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 N 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?
Input Format input arrives from the terminal stdin:
The first line of the input contains an integer N denoting the number of rooms in the map.
Then follow N spaceseparated integers p p pN where pi N where pi is the room that a potion will appear at before the ith traversal.
Finally, N lines follow with two spaceseparated integers a b a b N representing an edge between rooms a and b It is guaranteed that these edges form a tree.
Output Format print output to the terminal stdout:
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.
Sample Input:
Sample Output:
In this case, the minimum number of traversals required to complete the map is
One optimal plan that picks up two potions in three traversals is as follows:
Traversal : Pick up potion at
Traversal : Pick up potion at
Traversal : Forced to complete the map and ignore potion at
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started