#P9133. [THUPC 2023 初赛] 大富翁
[THUPC 2023 初赛] 大富翁
Background
One day, Xiao W and Xiao H are playing Monopoly.
Problem Description
The rules of this version of Monopoly are quite special. Its map is a rooted tree with nodes, where node is the root. Each node on the tree has a price, and the price of node is denoted as .
For two different nodes on the tree, if is an ancestor of (that is, lies on the simple path from node to node ), then we say that dominates .
During the game, Xiao W and Xiao H take turns to buy an unbought node on the tree, until all nodes have been bought by either Xiao W or Xiao H. (Before the game starts, all nodes on the tree are unbought.)
For a purchase, suppose the buyer purchases node . First, they must pay game coins to the system. At this time, suppose dominates nodes that have already been bought by the buyer's opponent, and meanwhile is dominated by nodes that have already been bought by the opponent. If , then the opponent must pay game coins to the buyer; if , then the buyer must pay game coins to the opponent.
Both Xiao W and Xiao H are extremely smart. They will both use optimal strategies in the game to earn as many game coins as possible. Now Xiao W wants to test you: if he moves first, how many game coins can he finally earn? (That is, during the whole game, the number of game coins Xiao W receives from Xiao H minus the number of game coins he pays to the system and to Xiao H. You may assume that before the game starts, both Xiao W and Xiao H have enough game coins. Note that the answer may be negative.)
Input Format
The first line contains a positive integer .
The second line contains non-negative integers, where the -th integer is the price of node .
The third line contains positive integers, where the -th positive integer denotes the parent index of node .
Output Format
One line contains one integer, which is the answer.
7
0 0 1 0 0 0 0
1 1 2 2 3 3
2
见附件中的 2.in
见附件中的 2.ans
Hint
Sample Explanation 1
One possible game process is:
- First purchase: Xiao W buys node and pays game coins to the system.
- Second purchase: Xiao H buys node , pays game coins to the system, and pays game coin to Xiao W.
- Third purchase: Xiao W buys node and pays game coin to the system.
- Fourth purchase: Xiao H buys node , pays game coins to the system, and pays game coin to Xiao W.
- Fifth purchase: Xiao W buys node and pays game coins to the system.
- Sixth purchase: Xiao H buys node , pays game coins to the system, and pays game coin to Xiao W.
- Seventh purchase: Xiao W buys node and pays game coins to the system.
Subtasks
For all testdata, , . It is guaranteed that the input graph is a rooted tree with node as the root.
Source
From the preliminary round of the 2023 Tsinghua University Student Programming Contest and Collegiate Invitational Contest (THUPC2023).
Resources such as editorials can be found at https://github.com/THUSAAC/THUPC2023-Pre.
Translated by ChatGPT 5