Multi Ending Story
Time limit5sMemory limit512 MB
Find the least time to reach all N endings of a binary choice tree, where each step takes one minute and only one quick-save slot is kept.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming
- Solved
- No attempts yet
Problem
You are a programmer who loves bishojo games, a sub-genre of dating simulation games. A game titled 'I * C * P * C!' was released yesterday and has just arrived. The game has multiple endings. When you complete all of them, you get a special figure of the main heroine, Sakuya. You want to hurry and play, but first, think about how to complete all the endings in the shortest time.
Every branching point in this game is a choice between 'Yes' and 'No'. Once you take a different choice, the branched stories flow to different endings and never converge again, forming a binary tree. It takes exactly one minute to proceed from a branching point to another branching point or to an ending. Returning to the beginning of the game ('reset') and playing from the beginning to the first branching point take negligible time.
The game has a 'Quick Save' feature that can significantly reduce the time needed to complete it. It records the point where you are currently playing, and you can return to that point at any later time. You can record any number of times, but you can hold only the last recorded point. When you use Quick Save, it overwrites the previous record. If you want to return to an overwritten point, you must play from the beginning again.
Compute the shortest time needed to complete all the endings.
Input
The first line contains one integer (), the number of endings. The next lines describe the branching points. The -th line describes the branching point with ID and contains two integers and , the IDs of the next branching points when you select Yes or No. means that selecting Yes reaches an ending, and the same holds for . The branching point with ID 1 is the first one. For every , holds. Each branching point ID from 2 to appears exactly once among all the and values.
Output
Print the shortest time in a line.