#Qua2304. TheBigFatDuck play Game
TheBigFatDuck play Game
Background
TheBigFatDuck: This class is too easy, there's nothing interesting to listen to. Hey, what are you playing? It looks fun.
TheHamster: I'm playing a new game called Tree Game. Do you want to join me for a PVP match?
TheBigFatDuck: Sure.
Description
There is a rooted tree with nodes, indexed from to . Node is the root. For a node , its parent is .
The rules of this game are as follows:
- Initially, each node has a coin, and node has a chess piece.
- The game is played in turns, with TheHamster going first and TheBigFatDuck going second.
- In each round:
- If there is a coin on the node where the chess piece is located (In the following text, we will use to represent this node), the current player collects the coin and ends the round.
- If there is no coin on the node :
- If some of 's child nodes have coins, the current player chooses one of those child nodes, moves the chess piece there, and ends the round.
- If none of 's child nodes have coins, the current player moves the chess piece to the parent node of and ends the round. If there is no parent, i.e. , the game ends.
Both TheBigFatDuck and TheHamster play this game using optimal strategies, which means they always aim to collect coins as much as possible. The task is to determine the maximum number of coins that TheHamster can get.
Format
Input
The first line contains an integer , representing the number of nodes in the tree.
The second line contains integers , representing the parent of node .
Output
One line containing a single integer, representing the maximum number of coins that TheHamster can get.
Sample
10
1 2 3 4 5 6 7 8 9
10
5
1 2 3 1
2
10
1 1 3 1 3 6 7 6 6
5
Limitations
1 second, 128 MB
TheHamster: I won, hahaha.
Teacher: Hamster, stand up and solve this problem.
TheHamster pushed up his round glasses and looked at the teacher with big curious eyes. She secretly glanced at TheBigFatDuck and lowered her head.
TheBigFatDuck's face showed a smile, and then he raised his hand and stood up: Teacher, I know how to solve this problem. Let me help her.
相关
在下列比赛中: