#P15613. [ICPC 2021 Jakarta R] White-Black Tree
[ICPC 2021 Jakarta R] White-Black Tree
题目描述
The White-Black Tree is a game played by two players on a rooted tree of nodes. The nodes are numbered from to and node is the root. Each node in the tree has a color of either (representing black) or (representing white) that can be changed according to the rule of the game.
Two opposing players play alternately. In their turn, the player chooses a white node in the tree; let the chosen node be . First, the color of node () is changed from white to black. Then, in the same turn, the player is allowed to change the color of zero or more nodes that are a descendant of node from white to black or black to white. A node is a descendant of a node if and only if the parent of node is either node or a descendant of node . The player who cannot make any move loses and the opposing player wins the game.
Your task in this problem is to determine who will win the game assuming both players play optimally; it means that if there exists a move that guarantees their win, then they will surely play that move.
For example, consider the following game with a rooted tree of nodes and let the initial colors be .
:::align{center}
:::
There are three white nodes (node , , and ) in which the first player can choose for their first move. In this example, there is a strategy for the first player to win the game. One of the optimal plays for the first player is to choose node , change into (black), and then change the color of node and node (both are node 's descendant), i.e. becomes (white) and becomes (black). When it's the second player's turn, there are only two white nodes to choose from (node and ) and both of them do not have any descendants. No matter what the second player chooses for their turn, the first player will simply choose the remaining one white node so that the second player will not be able to make any move. Therefore, the second player loses and the first player wins this example game.
输入格式
Input begins with a line containing an integer () representing the number of nodes in the given tree. The second line contains integers () for representing the parent of node . The third line contains integers () representing the initial color of node . The color black is represented by the integer while the color white is represented by the integer .
输出格式
Output a string "First" in a line if the first player will win the game assuming both players play the game optimally. Otherwise, output a string "Second" in a line.
7
1 1 1 3 3 3
0 1 1 0 0 0 1
First
5
1 1 2 3
0 1 1 0 0
Second
4
1 1 1
1 1 0 1
First
提示
Explanation for the sample input/output #2
The black root has two white children with the same subtree structure and colors. Whatever move the first player makes on one child, the second player simply mimics them on the other child.
Explanation for the sample input/output #3
The first player can make all nodes to be black in one move by choosing node .