Stationing
InterviewTime limit1sMemory limit1024 MB
Place troops in regions to maximize the total number of neighboring regions without troops, where each region links to one other region d_i.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Graph, Tree
- Solved
- No attempts yet
Problem
Thanks to the driver Haechan, country A was able to win the war against country B. However, A also suffered heavy damage in the war, so A plans to reorganize its army to prepare for B, which may invade at any time.
Country A has regions. To restore the road network destroyed by the war, each region built one bidirectional road leading to another region. The road built in region is a bidirectional road that leads to region .
To prepare for an invasion by B, A plans to station troops in each region. However, stationing too many troops reduces their usefulness, so A wants to place the troops efficiently in each region to maximize usefulness. Here, the usefulness of a troop is defined as the number of distinct regions that the troop can reach by using only one road, among those regions where no troop is stationed.
Find the maximum total usefulness when the troops are placed so that the sum of all troops' usefulness is maximized.
Input
The first line contains the number of regions .
The second line contains integers , separated by spaces. Each is the region connected by the bidirectional road built in region .
Output
Print the maximum possible sum of usefulness over all troops.